3 namespace Drupal\entity_embed\Plugin\Filter;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\Core\Render\BubbleableMetadata;
9 use Drupal\Core\Render\RenderContext;
10 use Drupal\Core\Render\RendererInterface;
11 use Drupal\entity_embed\EntityEmbedBuilderInterface;
12 use Drupal\entity_embed\Exception\EntityNotFoundException;
13 use Drupal\entity_embed\Exception\RecursiveRenderingException;
14 use Drupal\filter\FilterProcessResult;
15 use Drupal\filter\Plugin\FilterBase;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Drupal\embed\DomHelperTrait;
20 * Provides a filter to display embedded entities based on data attributes.
23 * id = "entity_embed",
24 * title = @Translation("Display embedded entities"),
25 * description = @Translation("Embeds entities using data attributes: data-entity-type, data-entity-uuid, and data-view-mode."),
26 * type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE
29 class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInterface {
34 * The renderer service.
36 * @var \Drupal\Core\Render\RendererInterface
41 * The entity type manager.
43 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
45 protected $entityTypeManager;
48 * The entity embed builder service.
50 * @var \Drupal\entity_embed\EntityEmbedBuilderInterface
55 * Constructs a EntityEmbedFilter object.
57 * @param array $configuration
58 * A configuration array containing information about the plugin instance.
59 * @param string $plugin_id
60 * The plugin ID for the plugin instance.
61 * @param mixed $plugin_definition
62 * The plugin implementation definition.
63 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
64 * The entity type manager.
65 * @param \Drupal\Core\Render\RendererInterface $renderer
67 * @param \Drupal\entity_embed\EntityEmbedBuilderInterface $builder
68 * The entity embed builder service.
70 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, EntityEmbedBuilderInterface $builder) {
71 parent::__construct($configuration, $plugin_id, $plugin_definition);
72 $this->entityTypeManager = $entity_type_manager;
73 $this->renderer = $renderer;
74 $this->builder = $builder;
80 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
85 $container->get('entity_type.manager'),
86 $container->get('renderer'),
87 $container->get('entity_embed.builder')
94 public function process($text, $langcode) {
95 $result = new FilterProcessResult($text);
97 if (strpos($text, 'data-entity-type') !== FALSE && (strpos($text, 'data-entity-embed-display') !== FALSE || strpos($text, 'data-view-mode') !== FALSE)) {
98 $dom = Html::load($text);
99 $xpath = new \DOMXPath($dom);
101 foreach ($xpath->query('//drupal-entity[@data-entity-type and (@data-entity-uuid or @data-entity-id) and (@data-entity-embed-display or @data-view-mode)]') as $node) {
102 /** @var \DOMElement $node */
103 $entity_type = $node->getAttribute('data-entity-type');
107 // data-entity-embed-settings is deprecated, make sure we convert it to
108 // data-entity-embed-display-settings.
109 if (($settings = $node->getAttribute('data-entity-embed-settings')) && !$node->hasAttribute('data-entity-embed-display-settings')) {
110 $node->setAttribute('data-entity-embed-display-settings', $settings);
111 $node->removeAttribute('data-entity-embed-settings');
115 // Load the entity either by UUID (preferred) or ID.
118 if ($id = $node->getAttribute('data-entity-uuid')) {
119 $entity = $this->entityTypeManager->getStorage($entity_type)
120 ->loadByProperties(['uuid' => $id]);
121 $entity = current($entity);
124 $id = $node->getAttribute('data-entity-id');
125 $entity = $this->entityTypeManager->getStorage($entity_type)->load($id);
129 // Protect ourselves from recursive rendering.
133 throw new RecursiveRenderingException(sprintf('Recursive rendering detected when rendering embedded %s entity %s.', $entity_type, $entity->id()));
136 // If a UUID was not used, but is available, add it to the HTML.
137 if (!$node->getAttribute('data-entity-uuid') && $uuid = $entity->uuid()) {
138 $node->setAttribute('data-entity-uuid', $uuid);
141 $context = $this->getNodeAttributesAsArray($node);
142 $context += array('data-langcode' => $langcode);
143 $build = $this->builder->buildEntityEmbed($entity, $context);
144 // We need to render the embedded entity:
145 // - without replacing placeholders, so that the placeholders are
146 // only replaced at the last possible moment. Hence we cannot use
147 // either renderPlain() or renderRoot(), so we must use render().
148 // - without bubbling beyond this filter, because filters must
149 // ensure that the bubbleable metadata for the changes they make
150 // when filtering text makes it onto the FilterProcessResult
151 // object that they return ($result). To prevent that bubbling, we
152 // must wrap the call to render() in a render context.
153 $entity_output = $this->renderer->executeInRenderContext(new RenderContext(), function () use (&$build) {
154 return $this->renderer->render($build);
156 $result = $result->merge(BubbleableMetadata::createFromRenderArray($build));
161 throw new EntityNotFoundException(sprintf('Unable to load embedded %s entity %s.', $entity_type, $id));
164 catch (\Exception $e) {
165 watchdog_exception('entity_embed', $e);
168 $this->replaceNodeContent($node, $entity_output);
171 $result->setProcessedText(Html::serialize($dom));
180 public function tips($long = FALSE) {
183 <p>You can embed entities. Additional properties can be added to the embed tag like data-caption and data-align if supported. Example:</p>
184 <code><drupal-entity data-entity-type="node" data-entity-uuid="07bf3a2e-1941-4a44-9b02-2d1d7a41ec0e" data-view-mode="teaser" /></code>');
187 return $this->t('You can embed entities.');