diff --git a/src/EntityEmbedHelperTrait.php b/src/EntityEmbedHelperTrait.php index 37d63bb..180dd36 100644 --- a/src/EntityEmbedHelperTrait.php +++ b/src/EntityEmbedHelperTrait.php @@ -4,83 +4,20 @@ namespace Drupal\entity_embed; use Drupal\Component\Utility\Html; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager; /** * Wrapper methods for entity rendering. * + * Trait depends on EntityEmbedDisplayManager and ModuleHandlerInterface being + * available as class members ($displayPluginManager and $moduleHandler). Any + * classes using this trait should make sure that both dependencies are + * provided. + * * @internal */ trait EntityEmbedHelperTrait { /** - * Teh Entity Embed Display plugin manager. - * - * @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager - */ - protected $displayPluginManager; - - /** - * The module handler service. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface. - */ - protected $moduleHandler; - - /** - * Returns the Entity Embed Display plugin manager. - * - * @return \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager - * The Entity Embed Display plugin manager. - */ - protected function displayPluginManager() { - if (!isset($this->displayPluginManager)) { - $this->displayPluginManager = \Drupal::service('plugin.manager.entity_embed.display'); - } - return $this->displayPluginManager; - } - - /** - * Sets the Entity Embed Display plugin manager. - * - * @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $display_plugin_manager - * The Entity Embed Display plugin manager. - * - * @return self - */ - public function setDisplayPluginManager(EntityEmbedDisplayManager $display_plugin_manager) { - $this->displayPluginManager = $display_plugin_manager; - return $this; - } - - /** - * Returns the module handler. - * - * @return \Drupal\Core\Extension\ModuleHandlerInterface - * The module handler. - */ - protected function moduleHandler() { - if (!isset($this->moduleHandler)) { - $this->moduleHandler = \Drupal::moduleHandler(); - } - return $this->moduleHandler; - } - - /** - * Sets the module handler service. - * - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler service. - * - * @return self - */ - public function setModuleHandler(ModuleHandlerInterface $module_handler) { - $this->moduleHandler = $module_handler; - return $this; - } - - /** * Builds the render array for an embedded entity. * * @param \Drupal\Core\Entity\EntityInterface $entity @@ -121,7 +58,7 @@ trait EntityEmbedHelperTrait { } // 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); + $this->moduleHandler->alter(array("{$context['data-entity-type']}_embed_context", 'entity_embed_context'), $context, $entity); // Build and render the Entity Embed Display plugin, allowing modules to // alter the result before rendering. @@ -155,7 +92,7 @@ trait EntityEmbedHelperTrait { $build['#access'] = $entity->access('view', NULL, TRUE); // @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); + $this->moduleHandler->alter(array("{$context['data-entity-type']}_embed", 'entity_embed'), $build, $entity, $context); return $build; } @@ -178,7 +115,7 @@ trait EntityEmbedHelperTrait { protected function buildEntityEmbedDisplayPlugin(EntityInterface $entity, $plugin_id, array $plugin_configuration = array(), array $context = array()) { // Build the Entity Embed Display plugin. /** @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayBase $display */ - $display = $this->displayPluginManager()->createInstance($plugin_id, $plugin_configuration); + $display = $this->displayPluginManager->createInstance($plugin_id, $plugin_configuration); $display->setContextValue('entity', $entity); $display->setAttributes($context); diff --git a/src/Plugin/Filter/EntityEmbedFilter.php b/src/Plugin/Filter/EntityEmbedFilter.php index 7dac555..755d746 100644 --- a/src/Plugin/Filter/EntityEmbedFilter.php +++ b/src/Plugin/Filter/EntityEmbedFilter.php @@ -9,6 +9,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Render\RenderContext; use Drupal\Core\Render\RendererInterface; +use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager; use Drupal\entity_embed\EntityEmbedHelperTrait; use Drupal\entity_embed\Exception\EntityNotFoundException; use Drupal\entity_embed\Exception\RecursiveRenderingException; @@ -28,6 +29,7 @@ use Drupal\embed\DomHelperTrait; * ) */ class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInterface { + use EntityEmbedHelperTrait; use DomHelperTrait; @@ -46,6 +48,20 @@ class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInte protected $entityTypeManager; /** + * The module handler service. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * The entity embed display plugin manager service. + * + * @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager + */ + protected $displayPluginManager; + + /** * Constructs a EntityEmbedFilter object. * * @param array $configuration @@ -59,10 +75,12 @@ class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInte * @param \Drupal\Core\Render\RendererInterface $renderer * The renderer. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, ModuleHandlerInterface $module_handler, EntityEmbedDisplayManager $display_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityTypeManager = $entity_type_manager; $this->renderer = $renderer; + $this->moduleHandler = $module_handler; + $this->displayPluginManager = $display_manager; } /** @@ -74,7 +92,9 @@ class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInte $plugin_id, $plugin_definition, $container->get('entity_type.manager'), - $container->get('renderer') + $container->get('renderer'), + $container->get('module_handler'), + $container->get('plugin.manager.entity_embed.display') ); } diff --git a/src/Twig/EntityEmbedTwigExtension.php b/src/Twig/EntityEmbedTwigExtension.php index 16a0988..d188bc6 100644 --- a/src/Twig/EntityEmbedTwigExtension.php +++ b/src/Twig/EntityEmbedTwigExtension.php @@ -4,6 +4,8 @@ namespace Drupal\entity_embed\Twig; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager; use Drupal\entity_embed\EntityEmbedHelperTrait; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -11,6 +13,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * Provide entity embedding function within Twig templates. */ class EntityEmbedTwigExtension extends \Twig_Extension implements ContainerInjectionInterface { + use EntityEmbedHelperTrait; /** @@ -21,13 +24,29 @@ class EntityEmbedTwigExtension extends \Twig_Extension implements ContainerInjec protected $entityTypeManager; /** + * The module handler service. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * The entity embed display plugin manager service. + * + * @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager + */ + protected $displayPluginManager; + + /** * Constructs a new EntityEmbedTwigExtension. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager service. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager) { + public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, EntityEmbedDisplayManager $display_manager) { $this->entityTypeManager = $entity_type_manager; + $this->moduleHandler = $module_handler; + $this->displayPluginManager = $display_manager; } /** @@ -35,7 +54,9 @@ class EntityEmbedTwigExtension extends \Twig_Extension implements ContainerInjec */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity_type.manager') + $container->get('entity_type.manager'), + $container->get('module_handler'), + $container->get('plugin.manager.entity_embed.display') ); }