Issue #2704743 by yongt9412, amateescu, Berdir: Remove special case on EntityFormatter for new entities
parent
6d3fd71023
commit
fd33967358
|
@ -160,48 +160,42 @@ class EntityReferenceEntityFormatter extends EntityReferenceFormatterBase implem
|
|||
$elements = array();
|
||||
|
||||
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
|
||||
if ($entity->id()) {
|
||||
// Due to render caching and delayed calls, the viewElements() method
|
||||
// will be called 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.
|
||||
$recursive_render_id = $items->getFieldDefinition()->getTargetEntityTypeId()
|
||||
. $items->getFieldDefinition()->getTargetBundle()
|
||||
. $items->getName()
|
||||
. $entity->id();
|
||||
// Due to render caching and delayed calls, the viewElements() method
|
||||
// will be called 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.
|
||||
$recursive_render_id = $items->getFieldDefinition()->getTargetEntityTypeId()
|
||||
. $items->getFieldDefinition()->getTargetBundle()
|
||||
. $items->getName()
|
||||
. $entity->id();
|
||||
|
||||
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 entity %entity_type: %entity_id, using the %field_name field on the %bundle_name bundle. Aborting rendering.', [
|
||||
'%entity_type' => $entity->getEntityTypeId(),
|
||||
'%entity_id' => $entity->id(),
|
||||
'%field_name' => $items->getName(),
|
||||
'%bundle_name' => $items->getFieldDefinition()->getTargetBundle(),
|
||||
]);
|
||||
return $elements;
|
||||
}
|
||||
|
||||
$view_builder = $this->entityTypeManager->getViewBuilder($entity->getEntityTypeId());
|
||||
$elements[$delta] = $view_builder->view($entity, $view_mode, $entity->language()->getId());
|
||||
|
||||
// Add a resource attribute to set the mapping property's value to the
|
||||
// entity's url. Since we don't know what the markup of the entity will
|
||||
// be, we shouldn't rely on it for structured data such as RDFa.
|
||||
if (!empty($items[$delta]->_attributes)) {
|
||||
$items[$delta]->_attributes += array('resource' => $entity->url());
|
||||
}
|
||||
if (isset(static::$recursiveRenderDepth[$recursive_render_id])) {
|
||||
static::$recursiveRenderDepth[$recursive_render_id]++;
|
||||
}
|
||||
else {
|
||||
// This is an "auto_create" item.
|
||||
$elements[$delta] = array('#markup' => $entity->label());
|
||||
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 entity %entity_type: %entity_id, using the %field_name field on the %bundle_name bundle. Aborting rendering.', [
|
||||
'%entity_type' => $entity->getEntityTypeId(),
|
||||
'%entity_id' => $entity->id(),
|
||||
'%field_name' => $items->getName(),
|
||||
'%bundle_name' => $items->getFieldDefinition()->getTargetBundle(),
|
||||
]);
|
||||
return $elements;
|
||||
}
|
||||
|
||||
$view_builder = $this->entityTypeManager->getViewBuilder($entity->getEntityTypeId());
|
||||
$elements[$delta] = $view_builder->view($entity, $view_mode, $entity->language()->getId());
|
||||
|
||||
// Add a resource attribute to set the mapping property's value to the
|
||||
// entity's url. Since we don't know what the markup of the entity will
|
||||
// be, we shouldn't rely on it for structured data such as RDFa.
|
||||
if (!empty($items[$delta]->_attributes) && !$entity->isNew() && $entity->hasLinkTemplate('canonical')) {
|
||||
$items[$delta]->_attributes += array('resource' => $entity->toUrl()->toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -201,8 +201,18 @@ class EntityReferenceFormatterTest extends EntityKernelTestBase {
|
|||
$this->assertEqual($build[0]['#cache']['tags'], $expected_cache_tags, format_string('The @formatter formatter has the expected cache tags.', array('@formatter' => $formatter)));
|
||||
|
||||
// Test the second field item.
|
||||
$expected_rendered_name_field_2 = '
|
||||
<div class="field field--name-name field--type-string field--label-hidden field__item">' . $this->unsavedReferencedEntity->label() . '</div>
|
||||
';
|
||||
$expected_rendered_body_field_2 = '
|
||||
<div class="clearfix text-formatted field field--name-body field--type-text field--label-above">
|
||||
<div class="field__label">Body</div>
|
||||
<div class="field__item"><p>Hello, unsaved world!</p></div>
|
||||
</div>
|
||||
';
|
||||
|
||||
$renderer->renderRoot($build[1]);
|
||||
$this->assertEqual($build[1]['#markup'], $this->unsavedReferencedEntity->label(), sprintf('The markup returned by the %s formatter is correct for an item with a unsaved entity.', $formatter));
|
||||
$this->assertEqual($build[1]['#markup'], 'default | ' . $this->unsavedReferencedEntity->label() . $expected_rendered_name_field_2 . $expected_rendered_body_field_2, sprintf('The markup returned by the %s formatter is correct for an item with a unsaved entity.', $formatter));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue