diff --git a/entity_embed.api.php b/entity_embed.api.php index f420eb8d..6d5f1a66 100644 --- a/entity_embed.api.php +++ b/entity_embed.api.php @@ -50,7 +50,7 @@ function hook_entity_embed_display_plugins_for_context_alter(array &$definitions // For nodes, use the default option. if ($entity instanceof \Drupal\node\NodeInterface) { - $definitions = array_intersect_key($definitions, array_flip(['entity_reference:entity_reference_entity_view'])); + $definitions = array_intersect_key($definitions, array_flip([\Drupal\entity_embed\EntityEmbedInterface::DEFAULT_DISPLAY_ID])); } } diff --git a/entity_embed.services.yml b/entity_embed.services.yml index cc3e116e..f7449fce 100644 --- a/entity_embed.services.yml +++ b/entity_embed.services.yml @@ -2,3 +2,6 @@ services: plugin.manager.entity_embed.display: class: Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager arguments: ['@container.namespaces', '@cache.discovery', '@module_handler'] + entity_embed: + class: Drupal\entity_embed\EntityEmbed + arguments: ['@entity.manager', '@module_handler', '@renderer', '@plugin.manager.entity_embed.display'] diff --git a/src/EntityEmbed.php b/src/EntityEmbed.php new file mode 100644 index 00000000..faa7875d --- /dev/null +++ b/src/EntityEmbed.php @@ -0,0 +1,111 @@ +setEntityManager($entity_manager); + $this->setModuleHandler($module_handler); + $this->renderer = $renderer; + $this->displayPluginManager = $display_plugin_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager'), + $container->get('module_handler'), + $container->get('renderer'), + $container->get('plugin.manager.entity_embed.display') + ); + } + + /** + * {@inheritdoc} + */ + public function renderEntityEmbed(EntityInterface $entity, array $context = array()) { + // Merge in default attributes. + $context += array( + 'data-entity-id' => $entity->id(), + 'data-entity-type' => $entity->getEntityTypeId(), + 'data-entity-embed-display' => static::DEFAULT_DISPLAY_ID, + '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'] = static::DEFAULT_DISPLAY_ID; + } + + // Support the deprecated view-mode data attribute. + if (isset($context['data-view-mode']) && $context['data-entity-embed-display'] === static::DEFAULT_DISPLAY_ID && empty($context['data-entity-embed-settings'])) { + $context['data-entity-embed-settings']['view_mode'] = &$context['data-view-mode']; + } + + // 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); + + // Build and render the display plugin, allowing modules to alter the + // result before rendering. + $build = $this->renderEntityEmbedDisplayPlugin( + $entity, + $context['data-entity-embed-display'], + $context['data-entity-embed-settings'], + $context + ); + // @todo Should this hook get invoked if $build is an empty array? + $this->moduleHandler()->alter(array("{$context['data-entity-type']}_embed", 'entity_embed'), $build, $entity, $context); + $entity_output = $this->renderer->render($build); + + return $entity_output; + } + + /** + * {@inheritdoc} + */ + public function renderEntityEmbedDisplayPlugin(EntityInterface $entity, $plugin_id, array $plugin_configuration = array(), array $context = array()) { + // Build the display plugin. + /** @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayBase $display */ + $display = $this->displayPluginManager->createInstance($plugin_id, $plugin_configuration); + $display->setContextValue('entity', $entity); + $display->setAttributes($context); + + // Check if the display plugin is accessible. This also checks entity + // access, which is why we never call $entity->access() here. + if (!$display->access()) { + return array(); + } + + return $display->build(); + } +} diff --git a/src/EntityEmbedInterface.php b/src/EntityEmbedInterface.php new file mode 100644 index 00000000..6467f7cd --- /dev/null +++ b/src/EntityEmbedInterface.php @@ -0,0 +1,71 @@ +entityManager()->hasHandler($entity_type, 'view_builder'); } - /** - * Returns the render array for an entity. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity to be rendered. - * @param string $view_mode - * The view mode that should be used to display the entity. - * @param string $langcode - * (optional) For which language the entity should be rendered, defaults to - * the current content language. - * - * @return array - * A render array for the entity. - */ - protected function renderEntity(EntityInterface $entity, $view_mode, $langcode = NULL) { - $render_controller = $this->entityManager()->getViewBuilder($entity->getEntityTypeId()); - return $render_controller->view($entity, $view_mode, $langcode); - } - - /** - * Renders an embedded entity. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity to be rendered. - * @param array $context - * (optional) Array of context values, corresponding to the attributes on - * the embed HTML tag. - * - * @return string - * 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'; - } - - // 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); - - // Build and render the display plugin, allowing modules to alter the - // result before rendering. - $build = $this->renderEntityEmbedDisplayPlugin( - $entity, - $context['data-entity-embed-display'], - $context['data-entity-embed-settings'], - $context - ); - // @todo Should this hook get invoked if $build is an empty array? - $this->moduleHandler()->alter(array("{$context['data-entity-type']}_embed", 'entity_embed'), $build, $entity, $context); - $entity_output = $this->renderer()->render($build); - - return $entity_output; - } - - /** - * Renders an entity using an EntityEmbedDisplay plugin. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity to be rendered. - * @param string $plugin_id - * The EntityEmbedDisplay plugin ID. - * @param array $plugin_configuration - * (optional) Array of plugin configuration values. - * @param array $context - * (optional) Array of additional context values, usually the embed HTML - * tag's attributes. - * - * @return array - * A render array for the display plugin. - */ - protected function renderEntityEmbedDisplayPlugin(EntityInterface $entity, $plugin_id, array $plugin_configuration = array(), array $context = array()) { - // Build the display plugin. - /** @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayBase $display */ - $display = $this->displayPluginManager()->createInstance($plugin_id, $plugin_configuration); - $display->setContextValue('entity', $entity); - $display->setAttributes($context); - - // Check if the display plugin is accessible. This also checks entity - // access, which is why we never call $entity->access() here. - if (!$display->access()) { - return array(); - } - - return $display->build(); - } - /** * Returns the entity manager. * @@ -283,56 +167,4 @@ public function setModuleHandler(ModuleHandlerInterface $module_handler) { $this->moduleHandler = $module_handler; return $this; } - - /** - * Returns the display plugin manager. - * - * @return \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager - * The display plugin manager. - */ - protected function displayPluginManager() { - if (!isset($this->displayPluginManager)) { - $this->displayPluginManager = \Drupal::service('plugin.manager.entity_embed.display'); - } - return $this->displayPluginManager; - } - - /** - * Sets the display plugin manager service. - * - * @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $display_plugin_manager - * The display plugin manager service. - * - * @return self - */ - public function setDisplayPluginManager(EntityEmbedDisplayManager $display_plugin_manager) { - $this->displayPluginManager = $display_plugin_manager; - return $this; - } - - /** - * Returns the renderer. - * - * @return \Drupal\Core\Render\RendererInterface - * The renderer. - */ - protected function renderer() { - if (!isset($this->renderer)) { - $this->renderer = \Drupal::service('renderer'); - } - return $this->renderer; - } - - /** - * Sets the renderer. - * - * @param \Drupal\Core\Render\RendererInterface $renderer - * The renderer. - * - * @return self - */ - public function setRenderer(RendererInterface $renderer) { - $this->renderer = $renderer; - return $this; - } } diff --git a/src/Form/EntityEmbedDialog.php b/src/Form/EntityEmbedDialog.php index 9b2f5e4b..52ffb68d 100644 --- a/src/Form/EntityEmbedDialog.php +++ b/src/Form/EntityEmbedDialog.php @@ -18,6 +18,8 @@ use Drupal\editor\EditorInterface; use Drupal\embed\EmbedButtonInterface; use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager; +use Drupal\entity_embed\EntityEmbedInterface; +use Drupal\entity_embed\EntityEmbedService; use Drupal\entity_embed\EntityHelperTrait; use Drupal\Component\Serialization\Json; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -35,17 +37,24 @@ class EntityEmbedDialog extends FormBase { */ protected $formBuilder; + /** + * The display plugin manager. + * + * @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager. + */ + protected $displayPluginManager; + /** * Constructs a EntityEmbedDialog object. * - * @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $plugin_manager - * The Module Handler. * @param \Drupal\Core\Form\FormBuilderInterface $form_builder - * The Form Builder. + * The form builder. + * @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $display_plugin_manager + * The display plugin manager. */ - public function __construct(EntityEmbedDisplayManager $plugin_manager, FormBuilderInterface $form_builder) { - $this->setDisplayPluginManager($plugin_manager); + public function __construct(FormBuilderInterface $form_builder, EntityEmbedDisplayManager $display_plugin_manager) { $this->formBuilder = $form_builder; + $this->displayPluginManager = $display_plugin_manager; } /** @@ -53,8 +62,8 @@ public function __construct(EntityEmbedDisplayManager $plugin_manager, FormBuild */ public static function create(ContainerInterface $container) { return new static( - $container->get('plugin.manager.entity_embed.display'), - $container->get('form_builder') + $container->get('form_builder'), + $container->get('plugin.manager.entity_embed.display') ); } @@ -92,7 +101,7 @@ public function buildForm(array $form, FormStateInterface $form_state, EditorInt '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-display' => EntityEmbedInterface::DEFAULT_DISPLAY_ID, 'data-entity-embed-settings' => array(), 'data-align' => '', ); @@ -253,7 +262,7 @@ public function buildEmbedStep(array $form, FormStateInterface $form_state) { // 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'; + $entity_element['data-entity-embed-display'] = EntityEmbedInterface::DEFAULT_DISPLAY_ID; } $form['attributes']['data-entity-embed-display'] = array( @@ -288,7 +297,7 @@ public function buildEmbedStep(array $form, FormStateInterface $form_state) { if (is_string($entity_element['data-entity-embed-settings'])) { $entity_element['data-entity-embed-settings'] = Json::decode($entity_element['data-entity-embed-settings'], TRUE); } - $display = $this->displayPluginManager()->createInstance($plugin_id, $entity_element['data-entity-embed-settings']); + $display = $this->displayPluginManager->createInstance($plugin_id, $entity_element['data-entity-embed-settings']); $display->setContextValue('entity', $entity); $display->setAttributes($entity_element); $form['attributes']['data-entity-embed-settings'] += $display->buildConfigurationForm($form, $form_state); @@ -412,7 +421,7 @@ public function validateEmbedStep(array $form, FormStateInterface $form_state) { $entity = $this->loadEntity($entity_element['data-entity-type'], $entity_element['data-entity-uuid']); $plugin_id = $entity_element['data-entity-embed-display']; $plugin_settings = $entity_element['data-entity-embed-settings'] ?: array(); - $display = $this->displayPluginManager()->createInstance($plugin_id, $plugin_settings); + $display = $this->displayPluginManager->createInstance($plugin_id, $plugin_settings); $display->setContextValue('entity', $entity); $display->setAttributes($entity_element); $display->validateConfigurationForm($form, $form_state); @@ -520,7 +529,7 @@ public function submitEmbedStep(array &$form, FormStateInterface $form_state) { $entity = $this->loadEntity($entity_element['data-entity-type'], $entity_element['data-entity-uuid']); $plugin_id = $entity_element['data-entity-embed-display']; $plugin_settings = $entity_element['data-entity-embed-settings'] ?: array(); - $display = $this->displayPluginManager()->createInstance($plugin_id, $plugin_settings); + $display = $this->displayPluginManager->createInstance($plugin_id, $plugin_settings); $display->setContextValue('entity', $entity); $display->setAttributes($entity_element); $display->submitConfigurationForm($form, $form_state); diff --git a/src/Plugin/Filter/EntityEmbedFilter.php b/src/Plugin/Filter/EntityEmbedFilter.php index 112614a7..9379df21 100644 --- a/src/Plugin/Filter/EntityEmbedFilter.php +++ b/src/Plugin/Filter/EntityEmbedFilter.php @@ -12,6 +12,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Cache\CacheableMetadata; +use Drupal\entity_embed\EntityEmbedInterface; use Drupal\entity_embed\EntityHelperTrait; use Drupal\entity_embed\Exception\EntityNotFoundException; use Drupal\entity_embed\Exception\RecursiveRenderingException; @@ -34,6 +35,13 @@ class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInte use EntityHelperTrait; use DomHelperTrait; + /** + * The entity embed service. + * + * @var \Drupal\entity_embed\EntityEmbedInterface. + */ + protected $entityEmbed; + /** * Constructs a EntityEmbedFilter object. * @@ -45,13 +53,13 @@ class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInte * The plugin implementation definition. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager service. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The Module Handler. + * @param \Drupal\entity_embed\EntityEmbedInterface $entity_embed + * The entity embed service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, EntityEmbedInterface $entity_embed) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->setEntityManager($entity_manager); - $this->setModuleHandler($module_handler); + $this->entityEmbed = $entity_embed; } /** @@ -63,7 +71,7 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $container->get('entity.manager'), - $container->get('module_handler') + $container->get('entity_embed') ); } @@ -108,7 +116,7 @@ public function process($text, $langcode) { $context = $this->getNodeAttributesAsArray($node); $context += array('data-langcode' => $langcode); - $entity_output = $this->renderEntityEmbed($entity, $context); + $entity_output = $this->entityEmbed->renderEntityEmbed($entity, $context); $depth--; }