Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / entity_embed / src / EntityEmbedBuilder.php
1 <?php
2
3 namespace Drupal\entity_embed;
4
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;
9
10 /**
11  * Builds embedded entities.
12  *
13  * @internal
14  */
15 class EntityEmbedBuilder implements EntityEmbedBuilderInterface {
16
17   /**
18    * The module handler service.
19    *
20    * @var \Drupal\Core\Extension\ModuleHandlerInterface
21    */
22   protected $moduleHandler;
23
24   /**
25    * The entity embed display plugin manager service.
26    *
27    * @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager
28    */
29   protected $displayPluginManager;
30
31   /**
32    * Constructs a EntityEmbedBuilder object.
33    *
34    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
35    *   The module handler service.
36    * @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $display_manager
37    */
38   public function __construct(ModuleHandlerInterface $module_handler, EntityEmbedDisplayManager $display_manager) {
39     $this->moduleHandler = $module_handler;
40     $this->displayPluginManager = $display_manager;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
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']];
51     }
52
53     // Merge in default attributes.
54     $context += [
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' => [],
59     ];
60
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';
65     }
66
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']);
70     }
71
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);
74
75     // Build and render the Entity Embed Display plugin, allowing modules to
76     // alter the result before rendering.
77     $build = [
78       '#theme_wrappers' => ['entity_embed_container'],
79       '#attributes' => ['class' => ['embedded-entity']],
80       '#entity' => $entity,
81       '#context' => $context,
82     ];
83     $build['entity'] = $this->buildEntityEmbedDisplayPlugin(
84       $entity,
85       $context['data-entity-embed-display'],
86       $context['data-entity-embed-display-settings'],
87       $context
88     );
89
90     // Maintain data-align if it is there.
91     if (isset($context['data-align'])) {
92       $build['#attributes']['data-align'] = $context['data-align'];
93     }
94     elseif ((isset($context['class']))) {
95       $build['#attributes']['class'][] = $context['class'];
96     }
97
98     // Maintain data-caption if it is there.
99     if (isset($context['data-caption'])) {
100       $build['#attributes']['data-caption'] = $context['data-caption'];
101     }
102
103     // Make sure that access to the entity is respected.
104     $build['#access'] = $entity->access('view', NULL, TRUE);
105
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);
108     return $build;
109   }
110
111   /**
112    * Builds the render array for an entity using an Entity Embed Display plugin.
113    *
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
122    *   tag's attributes.
123    *
124    * @return array
125    *   A render array for the Entity Embed Display plugin.
126    */
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);
133
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()) {
137       return [];
138     }
139
140     return $display->build();
141   }
142
143 }