View source
<?php
namespace Drupal\entity_embed\Plugin\Filter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\entity_embed\EntityEmbedBuilderInterface;
use Drupal\entity_embed\Exception\EntityNotFoundException;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\embed\DomHelperTrait;
class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInterface {
use DomHelperTrait;
const RECURSIVE_RENDER_LIMIT = 20;
protected $renderer;
protected $entityTypeManager;
protected $builder;
protected $loggerFactory;
protected static $recursiveRenderDepth = [];
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, EntityEmbedBuilderInterface $builder, LoggerChannelFactoryInterface $logger_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
$this->builder = $builder;
$this->loggerFactory = $logger_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('renderer'), $container
->get('entity_embed.builder'), $container
->get('logger.factory'));
}
public function process($text, $langcode) {
$result = new FilterProcessResult($text);
if (strpos($text, 'data-entity-type') !== FALSE && (strpos($text, 'data-entity-embed-display') !== FALSE || strpos($text, 'data-view-mode') !== FALSE)) {
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
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) {
$entity_type = $node
->getAttribute('data-entity-type');
$entity = NULL;
$entity_output = '';
if (($settings = $node
->getAttribute('data-entity-embed-settings')) && !$node
->hasAttribute('data-entity-embed-display-settings')) {
$node
->setAttribute('data-entity-embed-display-settings', $settings);
$node
->removeAttribute('data-entity-embed-settings');
}
$entity = NULL;
try {
$id = NULL;
if ($id = $node
->getAttribute('data-entity-uuid')) {
$entity = $this->entityTypeManager
->getStorage($entity_type)
->loadByProperties([
'uuid' => $id,
]);
$entity = current($entity);
}
else {
$id = $node
->getAttribute('data-entity-id');
$entity = $this->entityTypeManager
->getStorage($entity_type)
->load($id);
}
if (!$entity instanceof EntityInterface) {
$missing_text = $this
->t('Missing @type.', [
'@type' => $this->entityTypeManager
->getDefinition($entity_type)
->getSingularLabel(),
]);
$entity_output = '<img src="' . file_url_transform_relative(file_create_url('core/modules/media/images/icons/no-thumbnail.png')) . '" width="180" height="180" alt="' . $missing_text . '" title="' . $missing_text . '"/>';
throw new EntityNotFoundException(sprintf('Unable to load embedded %s entity %s.', $entity_type, $id));
}
} catch (EntityNotFoundException $e) {
watchdog_exception('entity_embed', $e);
}
if ($entity instanceof EntityInterface) {
if (!$node
->getAttribute('data-entity-uuid') && ($uuid = $entity
->uuid())) {
$node
->setAttribute('data-entity-uuid', $uuid);
}
$context = $this
->getNodeAttributesAsArray($node);
$context += [
'data-langcode' => $langcode,
];
$recursive_render_id = $entity
->uuid() . json_encode($context);
if (isset(static::$recursiveRenderDepth[$recursive_render_id])) {
static::$recursiveRenderDepth[$recursive_render_id]++;
}
else {
static::$recursiveRenderDepth[$recursive_render_id] = 1;
}
if (static::$recursiveRenderDepth[$recursive_render_id] > static::RECURSIVE_RENDER_LIMIT) {
$this->loggerFactory
->get('entity')
->error('Recursive rendering detected when rendering embedded entity %entity_type: %entity_id. Aborting rendering.', [
'%entity_type' => $entity
->getEntityTypeId(),
'%entity_id' => $entity
->id(),
]);
$entity_output = '';
}
else {
$build = $this->builder
->buildEntityEmbed($entity, $context);
$entity_output = $this->renderer
->executeInRenderContext(new RenderContext(), function () use (&$build) {
return $this->renderer
->render($build);
});
$result = $result
->merge(BubbleableMetadata::createFromRenderArray($build));
}
}
$this
->replaceNodeContent($node, $entity_output);
}
$result
->setProcessedText(Html::serialize($dom));
}
return $result;
}
public function tips($long = FALSE) {
if ($long) {
return $this
->t('
<p>You can embed entities. Additional properties can be added to the embed tag like data-caption and data-align if supported. Example:</p>
<code><drupal-entity data-entity-type="node" data-entity-uuid="07bf3a2e-1941-4a44-9b02-2d1d7a41ec0e" data-view-mode="teaser" /></code>');
}
else {
return $this
->t('You can embed entities.');
}
}
}