1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\entity_embed\Twig; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
6
|
|
|
use Drupal\entity_embed\EntityEmbedBuilderInterface; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Provide entity embedding function within Twig templates. |
11
|
|
|
*/ |
12
|
|
|
class EntityEmbedTwigExtension extends \Twig_Extension { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The entity type manager service. |
16
|
|
|
* |
17
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
18
|
|
|
*/ |
19
|
|
|
protected $entityTypeManager; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The entity embed builder service. |
23
|
|
|
* |
24
|
|
|
* @var \Drupal\entity_embed\EntityEmbedBuilderInterface |
25
|
|
|
*/ |
26
|
|
|
protected $builder; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructs a new EntityEmbedTwigExtension. |
30
|
|
|
* |
31
|
|
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
32
|
|
|
* The entity type manager service. |
33
|
|
|
*/ |
34
|
|
|
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityEmbedBuilderInterface $builder) { |
35
|
|
|
$this->entityTypeManager = $entity_type_manager; |
36
|
|
|
$this->builder = $builder; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public static function create(ContainerInterface $container) { |
43
|
|
|
return new static( |
44
|
|
|
$container->get('entity_type.manager'), |
45
|
|
|
$container->get('entity_embed.builder') |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function getName() { |
53
|
|
|
return 'entity_embed.twig.entity_embed_twig_extension'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function getFunctions() { |
60
|
|
|
return array( |
61
|
|
|
new \Twig_SimpleFunction('entity_embed', [$this, 'getRenderArray']), |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return the render array for an entity. |
67
|
|
|
* |
68
|
|
|
* @param string $entity_type |
69
|
|
|
* The machine name of an entity_type like 'node'. |
70
|
|
|
* @param string $entity_id |
71
|
|
|
* The entity ID. |
72
|
|
|
* @param string $display_plugin |
73
|
|
|
* (optional) The Entity Embed Display plugin to be used to render the |
74
|
|
|
* entity. |
75
|
|
|
* @param array $display_settings |
76
|
|
|
* (optional) A list of settings for the Entity Embed Display plugin. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
* A render array from entity_view(). |
80
|
|
|
*/ |
81
|
|
|
public function getRenderArray($entity_type, $entity_id, $display_plugin = 'default', array $display_settings = []) { |
82
|
|
|
$entity = $this->entityTypeManager->getStorage($entity_type)->load($entity_id); |
83
|
|
|
$context = [ |
84
|
|
|
'data-entity-type' => $entity_type, |
85
|
|
|
'data-entity-uuid' => $entity->uuid(), |
86
|
|
|
'data-entity-embed-display' => $display_plugin, |
87
|
|
|
'data-entity-embed-settings' => $display_settings, |
88
|
|
|
]; |
89
|
|
|
return $this->builder->buildEntityEmbed($entity, $context); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|