Skip to content
Snippets Groups Projects
Unverified Commit e1e7acf7 authored by Pieter Frenssen's avatar Pieter Frenssen Committed by Jibran Ijaz
Browse files

Issue #3152105 by pfrenssen: Backport improvements in infinite recursion...

Issue #3152105 by pfrenssen: Backport improvements in infinite recursion protection from the entity_reference field formatter
parent 127d20d7
No related branches found
No related tags found
No related merge requests found
......@@ -110,6 +110,10 @@ class DynamicEntityReferenceEntityFormatter extends EntityReferenceEntityFormatt
$recursive_render_id = $items->getFieldDefinition()->getTargetEntityTypeId()
. $items->getFieldDefinition()->getTargetBundle()
. $items->getName()
// We include the referencing entity, so we can render default images
// without hitting recursive protections.
. $items->getEntity()->id()
. $entity->getEntityTypeId()
. $entity->id();
if (isset(static::$recursiveRenderDepth[$recursive_render_id])) {
......
......@@ -338,6 +338,45 @@ class DynamicEntityReferenceFormatterTest extends EntityKernelTestBase {
$this->assertEquals($build[0]['#plain_text'], $referenced_entity_with_no_link_template->label(), sprintf('The markup returned by the %s formatter is correct for an entity type with no valid link template.', $formatter));
}
/**
* Renders the same entity referenced from different places.
*/
public function testEntityReferenceRecursiveProtectionWithManyRenderedEntities() {
$formatter = 'dynamic_entity_reference_entity_view';
$view_builder = $this->entityTypeManager->getViewBuilder($this->entityType);
// Set the default view mode to use the 'entity_reference_entity_view'
// formatter.
\Drupal::service('entity_display.repository')
->getViewDisplay($this->entityType, $this->bundle)
->setComponent($this->fieldName, [
'type' => $formatter,
])
->save();
$storage = $this->entityTypeManager->getStorage($this->entityType);
/** @var \Drupal\Core\Entity\ContentEntityInterface $referenced_entity */
$referenced_entity = $storage->create(['name' => $this->randomMachineName()]);
$referencing_entities = array_map(function () use ($storage, $referenced_entity) {
$referencing_entity = $storage->create([
'name' => $this->randomMachineName(),
$this->fieldName => $referenced_entity,
]);
$referencing_entity->save();
return $referencing_entity;
}, range(0, 29));
$build = $view_builder->viewMultiple($referencing_entities, 'default');
$output = $this->render($build);
// The title of entity_test entities is printed twice by default, so we have
// to multiply the formatter's recursive rendering protection limit by 2.
$expected_occurrences = 30 * 2;
$actual_occurrences = substr_count($output, $referenced_entity->get('name')->value);
$this->assertEquals($expected_occurrences, $actual_occurrences);
}
/**
* Sets field values and returns a render array as built by field list view.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment