3 namespace Drupal\entity_embed\Twig;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\entity_embed\EntityEmbedBuilderInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
10 * Provide entity embedding function within Twig templates.
12 class EntityEmbedTwigExtension extends \Twig_Extension {
15 * The entity type manager service.
17 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
19 protected $entityTypeManager;
22 * The entity embed builder service.
24 * @var \Drupal\entity_embed\EntityEmbedBuilderInterface
29 * Constructs a new EntityEmbedTwigExtension.
31 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
32 * The entity type manager service.
34 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityEmbedBuilderInterface $builder) {
35 $this->entityTypeManager = $entity_type_manager;
36 $this->builder = $builder;
42 public static function create(ContainerInterface $container) {
44 $container->get('entity_type.manager'),
45 $container->get('entity_embed.builder')
52 public function getName() {
53 return 'entity_embed.twig.entity_embed_twig_extension';
59 public function getFunctions() {
61 new \Twig_SimpleFunction('entity_embed', [$this, 'getRenderArray']),
66 * Return the render array for an entity.
68 * @param string $entity_type
69 * The machine name of an entity_type like 'node'.
70 * @param string $entity_id
72 * @param string $display_plugin
73 * (optional) The Entity Embed Display plugin to be used to render the
75 * @param array $display_settings
76 * (optional) A list of settings for the Entity Embed Display plugin.
79 * A render array from entity_view().
81 public function getRenderArray($entity_type, $entity_id, $display_plugin = 'default', array $display_settings = []) {
82 $entity = $this->entityTypeManager->getStorage($entity_type)->load($entity_id);
84 'data-entity-type' => $entity_type,
85 'data-entity-uuid' => $entity->uuid(),
86 'data-entity-embed-display' => $display_plugin,
87 'data-entity-embed-display-settings' => $display_settings,
89 return $this->builder->buildEntityEmbed($entity, $context);