diff --git a/README.md b/README.md index f37d043..a3cc01c 100644 --- a/README.md +++ b/README.md @@ -49,18 +49,18 @@ Entity Embed can be installed via the * Click on the 'E' button in the text editor. * Enter part of the title of the entity you're looking for and select one of the search results. -* For **Display as**, choose one of the following options: - * Rendered Entity +* If the entity you select is a node entity, for **Display as** you can choose one of the following options: * Entity ID * Label -* If chosen **Rendered Entity**, choose one of the following options for - **View mode**: - * Default * Full content * RSS * Search index * Search result highlighting input + * Teaser +* The last five options depend on the view modes you have on the entity. * Optionally, choose to align left, center or right. +**Rendered Entity** was available before but now the view modes are + available as entity embed display plugins. ## Embedding entities without WYSIWYG diff --git a/src/EntityEmbedDisplay/FieldFormatterEntityEmbedDisplayBase.php b/src/EntityEmbedDisplay/FieldFormatterEntityEmbedDisplayBase.php index 1ef8333..4fa7253 100644 --- a/src/EntityEmbedDisplay/FieldFormatterEntityEmbedDisplayBase.php +++ b/src/EntityEmbedDisplay/FieldFormatterEntityEmbedDisplayBase.php @@ -117,6 +117,9 @@ abstract class FieldFormatterEntityEmbedDisplayBase extends EntityEmbedDisplayBa /** * Returns the field formatter id. + * + * @return string|null + * Returns field formatter id or null. */ public function getFieldFormatterId() { return $this->getDerivativeId(); @@ -158,7 +161,7 @@ abstract class FieldFormatterEntityEmbedDisplayBase extends EntityEmbedDisplayBa * {@inheritdoc} */ public function defaultConfiguration() { - return $this->formatterPluginManager->getDefaultSettings($this->getDerivativeId()); + return $this->formatterPluginManager->getDefaultSettings($this->getFieldFormatterId()); } /** @@ -177,7 +180,7 @@ abstract class FieldFormatterEntityEmbedDisplayBase extends EntityEmbedDisplayBa public function getFieldFormatter() { if (!isset($this->fieldFormatter)) { $display = array( - 'type' => $this->getDerivativeId(), + 'type' => $this->getFieldFormatterId(), 'settings' => $this->getConfiguration(), 'label' => 'hidden', ); @@ -218,7 +221,7 @@ abstract class FieldFormatterEntityEmbedDisplayBase extends EntityEmbedDisplayBa public function calculateDependencies() { $this->addDependencies(parent::calculateDependencies()); - $definition = $this->formatterPluginManager->getDefinition($this->getDerivativeId()); + $definition = $this->formatterPluginManager->getDefinition($this->getFieldFormatterId()); $this->addDependency('module', $definition['provider']); // @todo Investigate why this does not work currently. //$this->calculatePluginDependencies($this->getFieldFormatter()); diff --git a/src/Plugin/Derivative/ViewModeDeriver.php b/src/Plugin/Derivative/ViewModeDeriver.php index 60e83a2..56be677 100644 --- a/src/Plugin/Derivative/ViewModeDeriver.php +++ b/src/Plugin/Derivative/ViewModeDeriver.php @@ -16,14 +16,14 @@ use Symfony\Component\DependencyInjection\ContainerInterface; class ViewModeDeriver extends DeriverBase implements ContainerDeriverInterface { /** - * The entity type manager. + * The entity display repository. * * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface */ protected $entityDisplayRepository; /** - * Constructs new FieldFormatterEntityEmbedDisplayBase. + * Constructs a ViewModeDeriver object. * * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository * The entity display repository. @@ -45,7 +45,7 @@ class ViewModeDeriver extends DeriverBase implements ContainerDeriverInterface { * {@inheritdoc} */ public function getDerivativeDefinitions($base_plugin_definition) { - foreach ($this->entityDisplayRepository->getAllViewModes() as $entity_type => $view_modes) { + foreach ($this->entityDisplayRepository->getAllViewModes() as $view_modes) { foreach ($view_modes as $view_mode => $definition) { $this->derivatives[$definition['id']] = $base_plugin_definition; $this->derivatives[$definition['id']]['label'] = new TranslatableMarkup($definition['label']); diff --git a/src/Plugin/entity_embed/EntityEmbedDisplay/ViewModeFieldFormatter.php b/src/Plugin/entity_embed/EntityEmbedDisplay/ViewModeFieldFormatter.php index aa86c47..14a8a6a 100644 --- a/src/Plugin/entity_embed/EntityEmbedDisplay/ViewModeFieldFormatter.php +++ b/src/Plugin/entity_embed/EntityEmbedDisplay/ViewModeFieldFormatter.php @@ -3,7 +3,6 @@ namespace Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplay; use Drupal\Core\Form\FormStateInterface; -use Drupal\entity_embed\EntityEmbedDisplay\FieldFormatterEntityEmbedDisplayBase; /** * Entity Embed Display reusing entity reference field formatters. @@ -12,23 +11,12 @@ use Drupal\entity_embed\EntityEmbedDisplay\FieldFormatterEntityEmbedDisplayBase; * * @EntityEmbedDisplay( * id = "view_mode", - * label = @Translation("View Mode Field Formatter"), + * label = @Translation("View Mode"), * deriver = "Drupal\entity_embed\Plugin\Derivative\ViewModeDeriver", * field_type = "entity_reference" * ) */ -class ViewModeFieldFormatter extends FieldFormatterEntityEmbedDisplayBase { - - /** - * {@inheritdoc} - */ - public function getFieldDefinition() { - if (!isset($this->fieldDefinition)) { - $this->fieldDefinition = parent::getFieldDefinition(); - $this->fieldDefinition->setSetting('target_type', $this->getEntityTypeFromContext()); - } - return $this->fieldDefinition; - } +class ViewModeFieldFormatter extends EntityReferenceFieldFormatter { /** * {@inheritdoc} @@ -60,8 +48,7 @@ class ViewModeFieldFormatter extends FieldFormatterEntityEmbedDisplayBase { * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - // Remove the configuration from entity_reference_entity_view. The view mode - // is already set in getFieldFormatter. + // Configuration form is not needed as the view mode is defined implicitly. return []; } @@ -72,11 +59,4 @@ class ViewModeFieldFormatter extends FieldFormatterEntityEmbedDisplayBase { return 'entity_reference_entity_view'; } - /** - * {@inheritdoc} - */ - public function getFieldValue() { - return ['target_id' => $this->getContextValue('entity')->id()]; - } - } diff --git a/src/Tests/ViewModeFieldFormatterTest.php b/src/Tests/ViewModeFieldFormatterTest.php index 73e3677..0891901 100644 --- a/src/Tests/ViewModeFieldFormatterTest.php +++ b/src/Tests/ViewModeFieldFormatterTest.php @@ -5,7 +5,7 @@ namespace Drupal\entity_embed\Tests; use Drupal\Core\Form\FormState; /** - * Tests the view mode field formatters provided by entity_embed. + * Tests the view mode entity embed display provided by entity_embed. * * @group entity_embed */ @@ -20,7 +20,7 @@ class ViewModeFieldFormatterTest extends EntityEmbedTestBase { ]; /** - * Tests view mode field formatters. + * Tests view mode entity embed display. */ public function testViewModeFieldFormatter() { // Ensure that view mode plugins have no configuration form. @@ -35,7 +35,7 @@ class ViewModeFieldFormatterTest extends EntityEmbedTestBase { } /** - * Tests filter using view mode Entity Embed Display plugins. + * Tests filter using view mode entity embed display plugins. */ public function testFilterViewModePlugins() { foreach ($this->plugins as $plugin) {