|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * Contains \Drupal\entity_embed\Twig\EntityEmbedTwigExtension. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Drupal\entity_embed\Twig; |
| 9 | + |
| 10 | +use Drupal\Core\Entity\EntityManagerInterface; |
| 11 | +use Drupal\entity_embed\EntityHelperTrait; |
| 12 | +use Drupal\Core\Extension\ModuleHandlerInterface; |
| 13 | +use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager; |
| 14 | + |
| 15 | +/** |
| 16 | + * Provide entity embedding function within Twig templates. |
| 17 | + */ |
| 18 | +class EntityEmbedTwigExtension extends \Twig_Extension { |
| 19 | + use EntityHelperTrait; |
| 20 | + |
| 21 | + /** |
| 22 | + * Constructs a new EntityEmbedTwigExtension. |
| 23 | + * |
| 24 | + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager |
| 25 | + * The entity manager service. |
| 26 | + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler |
| 27 | + * The module handler. |
| 28 | + * @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $plugin_manager |
| 29 | + * The plugin manager. |
| 30 | + */ |
| 31 | + public function __construct(EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, EntityEmbedDisplayManager $plugin_manager) { |
| 32 | + $this->setEntityManager($entity_manager); |
| 33 | + $this->setModuleHandler($module_handler); |
| 34 | + $this->setDisplayPluginManager($plugin_manager); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * {@inheritdoc} |
| 39 | + */ |
| 40 | + public function getName() { |
| 41 | + return 'entity_embed.twig.entity_embed_twig_extension'; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * {@inheritdoc} |
| 46 | + */ |
| 47 | + public function getFunctions() { |
| 48 | + return array( |
| 49 | + new \Twig_SimpleFunction('entity_embed', array($this, 'getRenderArray')), |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Return the render array for an entity. |
| 55 | + * |
| 56 | + * @param string $entity_type |
| 57 | + * The machine name of an entity_type like 'node'. |
| 58 | + * @param string $entity_id |
| 59 | + * The entity ID or entity UUID. |
| 60 | + * @param string $display_plugin |
| 61 | + * (optional) The display plugin to be used to render the entity. |
| 62 | + * @param array $display_settings |
| 63 | + * (optional) A list of settings for the display plugin. |
| 64 | + * |
| 65 | + * @return array |
| 66 | + * A render array from entity_view(). |
| 67 | + */ |
| 68 | + public function getRenderArray($entity_type, $entity_id, $display_plugin = 'default', array $display_settings = []) { |
| 69 | + $entity = $this->loadEntity($entity_type, $entity_id); |
| 70 | + $context = array( |
| 71 | + 'data-entity-type' => $entity_type, |
| 72 | + 'data-entity-id' => $entity_id, |
| 73 | + 'data-entity-embed-display' => $display_plugin, |
| 74 | + 'data-entity-embed-settings' => $display_settings, |
| 75 | + ); |
| 76 | + return $this->renderEntityEmbed($entity, $context); |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments