diff --git a/config/optional/embed.button.node.yml b/config/optional/embed.button.node.yml index bbadf1a..9dacba2 100644 --- a/config/optional/embed.button.node.yml +++ b/config/optional/embed.button.node.yml @@ -11,4 +11,5 @@ type_settings: entity_type: node bundles: { } display_plugins: { } + preview_display_plugin: '' icon_uuid: null diff --git a/config/schema/entity_embed.schema.yml b/config/schema/entity_embed.schema.yml index 014e705..166954c 100644 --- a/config/schema/entity_embed.schema.yml +++ b/config/schema/entity_embed.schema.yml @@ -19,6 +19,9 @@ embed.embed_type_settings.entity: sequence: type: string label: 'Entity Embed Display plugin' + preview_display_plugin: + type: string + label: 'Entity Embed Preview Display plugin' entity_browser: type: string label: 'Entity browser' diff --git a/src/EntityEmbedBuilder.php b/src/EntityEmbedBuilder.php index 24e716a..735b9d7 100644 --- a/src/EntityEmbedBuilder.php +++ b/src/EntityEmbedBuilder.php @@ -6,6 +6,7 @@ use Drupal\Component\Utility\Html; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager; +use Drupal\embed\Entity\EmbedButton; /** * Builds embedded entities. @@ -58,6 +59,14 @@ class EntityEmbedBuilder implements EntityEmbedBuilderInterface { 'data-entity-embed-display-settings' => [], ]; + // Override the display during embed preview based on embed button settings. + if (!empty($context['data-embed-button']) && + ($embed_button = EmbedButton::load($context['data-embed-button'])) && + ($preview_display_plugin = $embed_button->getTypeSetting('preview_display_plugin', '')) && + (\Drupal::routeMatch()->getRouteName() === 'embed.preview')) { + $context['data-entity-embed-display'] = $preview_display_plugin; + } + // The default Entity Embed Display plugin has been deprecated by the // rendered entity field formatter. if ($context['data-entity-embed-display'] === 'default') { diff --git a/src/Plugin/EmbedType/Entity.php b/src/Plugin/EmbedType/Entity.php index a532b64..a6c5ce2 100644 --- a/src/Plugin/EmbedType/Entity.php +++ b/src/Plugin/EmbedType/Entity.php @@ -101,6 +101,7 @@ class Entity extends EmbedTypeBase implements ContainerFactoryPluginInterface { 'entity_type' => 'node', 'bundles' => [], 'display_plugins' => [], + 'preview_display_plugin' => '', 'entity_browser' => '', 'entity_browser_settings' => [], ]; @@ -139,14 +140,26 @@ class Entity extends EmbedTypeBase implements ContainerFactoryPluginInterface { $form['bundles']['#access'] = !empty($form['bundles']['#options']); // Allow option to limit Entity Embed Display plugins. + $display_plugin_options = $this->displayPluginManager->getDefinitionOptionsForEntityType($entity_type_id); $form['display_plugins'] = array( '#type' => 'checkboxes', '#title' => $this->t('Allowed Entity Embed Display plugins'), - '#options' => $this->displayPluginManager->getDefinitionOptionsForEntityType($entity_type_id), + '#options' => $display_plugin_options, '#default_value' => $this->getConfigurationValue('display_plugins'), '#description' => $this->t('If none are selected, all are allowed. Note that these are the plugins which are allowed for this entity type, all of these might not be available for the selected entity.'), ); - $form['display_plugins']['#access'] = !empty($form['display_plugins']['#options']); + $form['display_plugins']['#access'] = !empty($display_plugin_options); + + // Entity Embed Preview Display plugin. + $form['preview_display_plugin'] = array( + '#type' => 'select', + '#title' => $this->t('Entity Embed Preview Display plugin'), + '#options' => $display_plugin_options, + '#empty_option' => $this->t('- None -'), + '#default_value' => $this->getConfigurationValue('preview_display_plugin'), + '#description' => $this->t('Select an alternate display plugin used in the embed preview. This can be used to configure a separate display to use for the preview within the WYSIWYG editor. If none is selected, then the selected display plugin is used. Note that these are the plugins which are allowed for this entity type, all of these might not be available for the selected entity.'), + ); + $form['preview_display_plugin']['#access'] = !empty($display_plugin_options); /** @var \Drupal\entity_browser\EntityBrowserInterface[] $browsers */ if ($this->entityTypeManager->hasDefinition('entity_browser') && ($browsers = $this->entityTypeManager->getStorage('entity_browser')->loadMultiple())) { @@ -292,6 +305,12 @@ class Entity extends EmbedTypeBase implements ContainerFactoryPluginInterface { $this->calculatePluginDependencies($instance); } + // Calculate preview display Entity Embed Display dependencies. + if ($preview_display_plugin = $this->getConfigurationValue('preview_display_plugin')) { + $instance = $this->displayPluginManager->createInstance($preview_display_plugin); + $this->calculatePluginDependencies($instance); + } + return $this->dependencies; }