From 071f067a47df8b93c6dcf86ba607fba354c04b20 Mon Sep 17 00:00:00 2001 From: Dave Reid Date: Fri, 4 Sep 2015 11:07:34 -0500 Subject: [PATCH 1/2] Added an interface for EntityEmbedDisplayManager. --- .../EntityEmbedDisplayManager.php | 29 ++------- .../EntityEmbedDisplayManagerInterface.php | 64 +++++++++++++++++++ src/EntityHelperTrait.php | 10 +-- src/Form/EntityEmbedDialog.php | 10 +-- 4 files changed, 78 insertions(+), 35 deletions(-) create mode 100644 src/EntityEmbedDisplay/EntityEmbedDisplayManagerInterface.php diff --git a/src/EntityEmbedDisplay/EntityEmbedDisplayManager.php b/src/EntityEmbedDisplay/EntityEmbedDisplayManager.php index 2bfa4088..1c49a25a 100644 --- a/src/EntityEmbedDisplay/EntityEmbedDisplayManager.php +++ b/src/EntityEmbedDisplay/EntityEmbedDisplayManager.php @@ -19,7 +19,7 @@ * @see \Drupal\entity_embed\Annotation\EntityEmbedDisplay * @see \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayInterface */ -class EntityEmbedDisplayManager extends DefaultPluginManager { +class EntityEmbedDisplayManager extends DefaultPluginManager implements EntityEmbedDisplayManagerInterface { /** * Constructs a new class instance. @@ -52,16 +52,7 @@ public function processDefinition(&$definition, $plugin_id) { } /** - * Determines plugins whose constraints are satisfied by a set of contexts. - * - * @param array $contexts - * An array of contexts. - * - * @return array - * An array of plugin definitions. - * - * @todo At some point convert this to use ContextAwarePluginManagerTrait - * @see https://drupal.org/node/2277981 + * @{inheritdoc} */ public function getDefinitionsForContexts(array $contexts = array()) { $definitions = $this->getDefinitions(); @@ -83,13 +74,7 @@ public function getDefinitionsForContexts(array $contexts = array()) { } /** - * Provides a list of plugins that can be used for a certain entity. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * An entity object. - * - * @return array - * An array of valid plugin labels, keyed by plugin ID. + * @{inheritdoc} */ public function getDefinitionOptionsForEntity(EntityInterface $entity) { $definitions = $this->getDefinitionsForContexts(array('entity' => $entity)); @@ -99,13 +84,7 @@ public function getDefinitionOptionsForEntity(EntityInterface $entity) { } /** - * Provides a list of plugins that can be used for a certain entity type. - * - * @param string $entity_type - * The entity type id. - * - * @return array - * An array of valid plugin labels, keyed by plugin ID. + * @{inheritdoc} */ public function getDefinitionOptionsForEntityType($entity_type) { $definitions = $this->getDefinitionsForContexts(array('entity_type' => $entity_type)); diff --git a/src/EntityEmbedDisplay/EntityEmbedDisplayManagerInterface.php b/src/EntityEmbedDisplay/EntityEmbedDisplayManagerInterface.php new file mode 100644 index 00000000..24a5acaa --- /dev/null +++ b/src/EntityEmbedDisplay/EntityEmbedDisplayManagerInterface.php @@ -0,0 +1,64 @@ +displayPluginManager = $display_plugin_manager; return $this; } diff --git a/src/Form/EntityEmbedDialog.php b/src/Form/EntityEmbedDialog.php index 9b2f5e4b..8f834bee 100644 --- a/src/Form/EntityEmbedDialog.php +++ b/src/Form/EntityEmbedDialog.php @@ -17,7 +17,7 @@ use Drupal\editor\Ajax\EditorDialogSave; use Drupal\editor\EditorInterface; use Drupal\embed\EmbedButtonInterface; -use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager; +use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManagerInterface; use Drupal\entity_embed\EntityHelperTrait; use Drupal\Component\Serialization\Json; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -38,12 +38,12 @@ class EntityEmbedDialog extends FormBase { /** * Constructs a EntityEmbedDialog object. * - * @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $plugin_manager - * The Module Handler. + * @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManagerInterface $plugin_manager + * The display plugin manager. * @param \Drupal\Core\Form\FormBuilderInterface $form_builder - * The Form Builder. + * The form builder. */ - public function __construct(EntityEmbedDisplayManager $plugin_manager, FormBuilderInterface $form_builder) { + public function __construct(EntityEmbedDisplayManagerInterface $plugin_manager, FormBuilderInterface $form_builder) { $this->setDisplayPluginManager($plugin_manager); $this->formBuilder = $form_builder; } From 650ea0eeab745a5f26c37d39742e83bb299ad822 Mon Sep 17 00:00:00 2001 From: Dave Reid Date: Fri, 4 Sep 2015 11:14:57 -0500 Subject: [PATCH 2/2] Move preparing the embed context to EntityHelperTrait::prepareEmbedContext. --- src/EntityHelperTrait.php | 41 ++++++++++++++++++---------------- src/Form/EntityEmbedDialog.php | 21 +++++------------ 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/EntityHelperTrait.php b/src/EntityHelperTrait.php index 119bffd0..2f68521b 100644 --- a/src/EntityHelperTrait.php +++ b/src/EntityHelperTrait.php @@ -162,25 +162,7 @@ protected function renderEntity(EntityInterface $entity, $view_mode, $langcode = * The HTML of the entity rendered with the display plugin. */ protected function renderEntityEmbed(EntityInterface $entity, array $context = array()) { - // Support the deprecated view-mode data attribute. - if (isset($context['data-view-mode']) && !isset($context['data-entity-embed-display']) && !isset($context['data-entity-embed-settings'])) { - $context['data-entity-embed-display'] = 'entity_reference:entity_reference_entity_view'; - $context['data-entity-embed-settings'] = ['view_mode' => &$context['data-view-mode']]; - } - - // Merge in default attributes. - $context += array( - 'data-entity-id' => $entity->id(), - 'data-entity-type' => $entity->getEntityTypeId(), - 'data-entity-embed-display' => 'entity_reference:entity_reference_entity_view', - 'data-entity-embed-settings' => array(), - ); - - // The default display plugin has been deprecated by the rendered entity - // field formatter. - if ($context['data-entity-embed-display'] === 'default') { - $context['data-entity-embed-display'] = 'entity_reference:entity_reference_entity_view'; - } + $this->prepareEmbedContext($context, $entity); // Allow modules to alter the entity prior to embed rendering. $this->moduleHandler()->alter(array("{$context['data-entity-type']}_embed_context", 'entity_embed_context'), $context, $entity); @@ -200,6 +182,27 @@ protected function renderEntityEmbed(EntityInterface $entity, array $context = a return $entity_output; } + protected function prepareEmbedContext(array &$context, EntityInterface $entity = null) { + // Merge in default attributes. + $context += array( + 'data-entity-embed-display' => EntityEmbedDisplayManagerInterface::DEFAULT_PLUGIN_ID, + 'data-entity-embed-settings' => array(), + 'data-entity-id' => $entity ? $entity->id() : '', + 'data-entity-uuid' => $entity ? $entity->uuid() : '', + 'data-entity-type' => $entity ? $entity->getEntityTypeId() : '', + ); + + if ($context['data-entity-embed-display'] === 'default') { + // The default display plugin has been deprecated by the rendered entity + // field formatter. + $context['data-entity-embed-display'] = EntityEmbedDisplayManagerInterface::DEFAULT_PLUGIN_ID; + } + elseif ($context['data-entity-embed-display'] === EntityEmbedDisplayManagerInterface::DEFAULT_PLUGIN_ID && isset($context['data-view-mode']) && empty($context['data-entity-embed-settings'])) { + // Support the deprecated view-mode data attribute. + $context['data-entity-embed-settings']['view_mode'] = &$context['data-view-mode']; + } + } + /** * Renders an entity using an EntityEmbedDisplay plugin. * diff --git a/src/Form/EntityEmbedDialog.php b/src/Form/EntityEmbedDialog.php index 8f834bee..12672243 100644 --- a/src/Form/EntityEmbedDialog.php +++ b/src/Form/EntityEmbedDialog.php @@ -76,10 +76,7 @@ public function getFormId() { public function buildForm(array $form, FormStateInterface $form_state, EditorInterface $editor = NULL, EmbedButtonInterface $embed_button = NULL) { $values = $form_state->getValues(); $input = $form_state->getUserInput(); - // Set embed button element in form state, so that it can be used later in - // validateForm() function. - $form_state->set('embed_button', $embed_button); - $form_state->set('editor', $editor); + // Initialize entity element with form attributes, if present. $entity_element = empty($values['attributes']) ? array() : $values['attributes']; // The default values are set directly from \Drupal::request()->request, @@ -90,14 +87,14 @@ public function buildForm(array $form, FormStateInterface $form_state, EditorInt $entity_element += $form_state->get('entity_element'); $entity_element += array( 'data-entity-type' => $embed_button->getTypeSetting('entity_type'), - 'data-entity-uuid' => '', - 'data-entity-id' => '', - 'data-entity-embed-display' => 'entity_reference:entity_reference_entity_view', - 'data-entity-embed-settings' => array(), 'data-align' => '', ); - $form_state->set('entity_element', $entity_element); + $this->prepareEmbedContext($entity_element, $form_state->get('entity')); + + $form_state->set('embed_button', $embed_button); + $form_state->set('editor', $editor); $form_state->set('entity', $this->loadEntity($entity_element['data-entity-type'], $entity_element['data-entity-uuid'] ?: $entity_element['data-entity-id'])); + $form_state->set('entity_element', $entity_element); if (!$form_state->get('step')) { // If an entity has been selected, then always skip to the embed options. @@ -250,12 +247,6 @@ public function buildEmbedStep(array $form, FormStateInterface $form_state) { $entity_element['data-entity-embed-display'] = key($display_plugin_options); } - // The default display plugin has been deprecated by the rendered entity - // field formatter. - if ($entity_element['data-entity-embed-display'] === 'default') { - $entity_element['data-entity-embed-display'] = 'entity_reference:entity_reference_entity_view'; - } - $form['attributes']['data-entity-embed-display'] = array( '#type' => 'select', '#title' => $this->t('Display as'),