3 namespace Drupal\entity_embed;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager;
11 * Builds embedded entities.
15 class EntityEmbedBuilder implements EntityEmbedBuilderInterface {
18 * The module handler service.
20 * @var \Drupal\Core\Extension\ModuleHandlerInterface
22 protected $moduleHandler;
25 * The entity embed display plugin manager service.
27 * @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager
29 protected $displayPluginManager;
32 * Constructs a EntityEmbedBuilder object.
34 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
35 * The module handler service.
36 * @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $display_manager
38 public function __construct(ModuleHandlerInterface $module_handler, EntityEmbedDisplayManager $display_manager) {
39 $this->moduleHandler = $module_handler;
40 $this->displayPluginManager = $display_manager;
46 public function buildEntityEmbed(EntityInterface $entity, array $context = []) {
47 // Support the deprecated view-mode data attribute.
48 if (isset($context['data-view-mode']) && !isset($context['data-entity-embed-display']) && !isset($context['data-entity-embed-display-settings'])) {
49 $context['data-entity-embed-display'] = 'entity_reference:entity_reference_entity_view';
50 $context['data-entity-embed-display-settings'] = ['view_mode' => &$context['data-view-mode']];
53 // Merge in default attributes.
55 'data-entity-type' => $entity->getEntityTypeId(),
56 'data-entity-uuid' => $entity->uuid(),
57 'data-entity-embed-display' => 'entity_reference:entity_reference_entity_view',
58 'data-entity-embed-display-settings' => [],
61 // The default Entity Embed Display plugin has been deprecated by the
62 // rendered entity field formatter.
63 if ($context['data-entity-embed-display'] === 'default') {
64 $context['data-entity-embed-display'] = 'entity_reference:entity_reference_entity_view';
67 // The caption text is double-encoded, so decode it here.
68 if (isset($context['data-caption'])) {
69 $context['data-caption'] = Html::decodeEntities($context['data-caption']);
72 // Allow modules to alter the entity prior to embed rendering.
73 $this->moduleHandler->alter(["{$context['data-entity-type']}_embed_context", 'entity_embed_context'], $context, $entity);
75 // Build and render the Entity Embed Display plugin, allowing modules to
76 // alter the result before rendering.
78 '#theme_wrappers' => ['entity_embed_container'],
79 '#attributes' => ['class' => ['embedded-entity']],
81 '#context' => $context,
83 $build['entity'] = $this->buildEntityEmbedDisplayPlugin(
85 $context['data-entity-embed-display'],
86 $context['data-entity-embed-display-settings'],
90 // Maintain data-align if it is there.
91 if (isset($context['data-align'])) {
92 $build['#attributes']['data-align'] = $context['data-align'];
94 elseif ((isset($context['class']))) {
95 $build['#attributes']['class'][] = $context['class'];
98 // Maintain data-caption if it is there.
99 if (isset($context['data-caption'])) {
100 $build['#attributes']['data-caption'] = $context['data-caption'];
103 // Make sure that access to the entity is respected.
104 $build['#access'] = $entity->access('view', NULL, TRUE);
106 // @todo Should this hook get invoked if $build is an empty array?
107 $this->moduleHandler->alter(["{$context['data-entity-type']}_embed", 'entity_embed'], $build, $entity, $context);
112 * Builds the render array for an entity using an Entity Embed Display plugin.
114 * @param \Drupal\Core\Entity\EntityInterface $entity
115 * The entity to be rendered.
116 * @param string $plugin_id
117 * The Entity Embed Display plugin ID.
118 * @param array $plugin_configuration
119 * (optional) Array of plugin configuration values.
120 * @param array $context
121 * (optional) Array of additional context values, usually the embed HTML
125 * A render array for the Entity Embed Display plugin.
127 protected function buildEntityEmbedDisplayPlugin(EntityInterface $entity, $plugin_id, array $plugin_configuration = [], array $context = []) {
128 // Build the Entity Embed Display plugin.
129 /** @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayBase $display */
130 $display = $this->displayPluginManager->createInstance($plugin_id, $plugin_configuration);
131 $display->setContextValue('entity', $entity);
132 $display->setAttributes($context);
134 // Check if the Entity Embed Display plugin is accessible. This also checks
135 // entity access, which is why we never call $entity->access() here.
136 if (!$display->access()) {
140 return $display->build();