class EntityEmbedFilter in Entity Embed 8
Provides a filter to display embedded entities based on data attributes.
Plugin annotation
@Filter(
id = "entity_embed",
title = @Translation("Display embedded entities"),
description = @Translation("Embeds entities using data attributes: data-entity-type, data-entity-uuid, and data-view-mode. Should usually run as the last filter, since it does not contain user input."),
type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
weight = 100,
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\filter\Plugin\FilterBase implements FilterInterface
- class \Drupal\entity_embed\Plugin\Filter\EntityEmbedFilter implements ContainerFactoryPluginInterface uses DomHelperTrait
- class \Drupal\filter\Plugin\FilterBase implements FilterInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of EntityEmbedFilter
1 file declares its use of EntityEmbedFilter
- RecursionProtectionTest.php in tests/
src/ Functional/ RecursionProtectionTest.php
File
- src/
Plugin/ Filter/ EntityEmbedFilter.php, line 31
Namespace
Drupal\entity_embed\Plugin\FilterView source
class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInterface {
use DomHelperTrait;
/**
* The number of times this formatter allows rendering the same entity.
*
* @var int
*/
const RECURSIVE_RENDER_LIMIT = 20;
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity embed builder service.
*
* @var \Drupal\entity_embed\EntityEmbedBuilderInterface
*/
protected $builder;
/**
* The logger factory.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;
/**
* An array of counters for the recursive rendering protection.
*
* Each counter takes into account all the relevant information about the
* field and the referenced entity that is being rendered.
*
* @var array
*
* @see \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter::$recursiveRenderDepth
*/
protected static $recursiveRenderDepth = [];
/**
* Constructs a EntityEmbedFilter object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param \Drupal\entity_embed\EntityEmbedBuilderInterface $builder
* The entity embed builder service.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger factory.
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
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) {
/** @var \DOMElement $node */
$entity_type = $node
->getAttribute('data-entity-type');
$entity = NULL;
$entity_output = '';
// data-entity-embed-settings is deprecated, make sure we convert it to
// data-entity-embed-display-settings.
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 {
// Load the entity either by UUID (preferred) or ID.
$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 a UUID was not used, but is available, add it to the HTML.
if (!$node
->getAttribute('data-entity-uuid') && ($uuid = $entity
->uuid())) {
$node
->setAttribute('data-entity-uuid', $uuid);
}
$context = $this
->getNodeAttributesAsArray($node);
$context += [
'data-langcode' => $langcode,
];
// Due to render caching and delayed calls, filtering happens later
// in the rendering process through a '#pre_render' callback, so we
// need to generate a counter that takes into account all the
// relevant information about this field and the referenced entity
// that is being rendered.
// @see \Drupal\filter\Element\ProcessedText::preRenderText()
$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;
}
// Protect ourselves from recursive rendering.
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);
// We need to render the embedded entity:
// - without replacing placeholders, so that the placeholders are
// only replaced at the last possible moment. Hence we cannot use
// either renderPlain() or renderRoot(), so we must use render().
// - without bubbling beyond this filter, because filters must
// ensure that the bubbleable metadata for the changes they make
// when filtering text makes it onto the FilterProcessResult
// object that they return ($result). To prevent that bubbling, we
// must wrap the call to render() in a render 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;
}
/**
* {@inheritdoc}
*/
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.');
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DomHelperTrait:: |
protected | function | Rename a DOMNode tag. | |
DomHelperTrait:: |
public | function | Convert the attributes on a DOMNode object to an array. | |
DomHelperTrait:: |
protected | function | Replace the contents of a DOMNode. | |
DomHelperTrait:: |
protected | function | Set the contents of a DOMNode. | |
EntityEmbedFilter:: |
protected | property | The entity embed builder service. | |
EntityEmbedFilter:: |
protected | property | The entity type manager. | |
EntityEmbedFilter:: |
protected | property | The logger factory. | |
EntityEmbedFilter:: |
protected static | property | An array of counters for the recursive rendering protection. | |
EntityEmbedFilter:: |
protected | property | The renderer service. | |
EntityEmbedFilter:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
EntityEmbedFilter:: |
public | function |
Performs the filter processing. Overrides FilterInterface:: |
|
EntityEmbedFilter:: |
constant | The number of times this formatter allows rendering the same entity. | ||
EntityEmbedFilter:: |
public | function |
Generates a filter's tip. Overrides FilterBase:: |
|
EntityEmbedFilter:: |
public | function |
Constructs a EntityEmbedFilter object. Overrides FilterBase:: |
|
FilterBase:: |
public | property | The name of the provider that owns this filter. | |
FilterBase:: |
public | property | An associative array containing the configured settings of this filter. | |
FilterBase:: |
public | property | A Boolean indicating whether this filter is enabled. | |
FilterBase:: |
public | property | The weight of this filter compared to others in a filter collection. | |
FilterBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
1 |
FilterBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
|
FilterBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
FilterBase:: |
public | function |
Returns the administrative description for this filter plugin. Overrides FilterInterface:: |
|
FilterBase:: |
public | function |
Returns HTML allowed by this filter's configuration. Overrides FilterInterface:: |
4 |
FilterBase:: |
public | function |
Returns the administrative label for this filter plugin. Overrides FilterInterface:: |
|
FilterBase:: |
public | function |
Returns the processing type of this filter plugin. Overrides FilterInterface:: |
|
FilterBase:: |
public | function |
Prepares the text for processing. Overrides FilterInterface:: |
|
FilterBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
1 |
FilterBase:: |
public | function |
Generates a filter's settings form. Overrides FilterInterface:: |
3 |
FilterInterface:: |
constant | HTML tag and attribute restricting filters to prevent XSS attacks. | ||
FilterInterface:: |
constant | Non-HTML markup language filters that generate HTML. | ||
FilterInterface:: |
constant | Irreversible transformation filters. | ||
FilterInterface:: |
constant | Reversible transformation filters. | ||
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |