Skip to content
Snippets Groups Projects
Commit 7c6a23fe authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2355245 by amateescu: ER's label formatter needs to take into account...

Issue #2355245 by amateescu: ER's label formatter needs to take into account that $entity->urlInfo() might throw an exception
parent b175ecc4
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Drupal\entity_reference\Plugin\Field\FieldFormatter; namespace Drupal\entity_reference\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\String; use Drupal\Component\Utility\String;
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
...@@ -61,12 +62,26 @@ public function settingsSummary() { ...@@ -61,12 +62,26 @@ public function settingsSummary() {
*/ */
public function viewElements(FieldItemListInterface $items) { public function viewElements(FieldItemListInterface $items) {
$elements = array(); $elements = array();
$output_as_link = $this->getSetting('link');
foreach ($this->getEntitiesToView($items) as $delta => $entity) { foreach ($this->getEntitiesToView($items) as $delta => $entity) {
$label = $entity->label(); $label = $entity->label();
// If the link is to be displayed and the entity has a uri, display a // If the link is to be displayed and the entity has a uri, display a
// link. // link.
if ($this->getSetting('link') && !$entity->isNew() && $uri = $entity->urlInfo()) { if ($output_as_link && !$entity->isNew()) {
try {
$uri = $entity->urlInfo();
}
catch (UndefinedLinkTemplateException $e) {
// This exception is thrown by \Drupal\Core\Entity\Entity::urlInfo()
// and it means that the entity type doesn't have a link template nor
// a valid "uri_callback", so don't bother trying to output a link for
// the rest of the referenced entities.
$output_as_link = FALSE;
}
}
if ($output_as_link && isset($uri) && !$entity->isNew()) {
$elements[$delta] = [ $elements[$delta] = [
'#type' => 'link', '#type' => 'link',
'#title' => $label, '#title' => $label,
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Cache;
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Entity\FilterFormat; use Drupal\filter\Entity\FilterFormat;
use Drupal\system\Tests\Entity\EntityUnitTestBase; use Drupal\system\Tests\Entity\EntityUnitTestBase;
...@@ -226,6 +227,21 @@ public function testLabelFormatter() { ...@@ -226,6 +227,21 @@ public function testLabelFormatter() {
$build = $this->buildRenderArray([$this->referencedEntity, $this->unsavedReferencedEntity], $formatter, array('link' => FALSE)); $build = $this->buildRenderArray([$this->referencedEntity, $this->unsavedReferencedEntity], $formatter, array('link' => FALSE));
$this->assertEqual($build[0]['#markup'], $this->referencedEntity->label(), sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter)); $this->assertEqual($build[0]['#markup'], $this->referencedEntity->label(), sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter));
$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'], $this->unsavedReferencedEntity->label(), sprintf('The markup returned by the %s formatter is correct for an item with a unsaved entity.', $formatter));
// Test an entity type that doesn't have any link templates, which means
// \Drupal\Core\Entity\EntityInterface::urlInfo() will throw an exception
// and the label formatter will output only the label instead of a link.
$field_storage_config = FieldStorageConfig::loadByName($this->entityType, $this->fieldName);
$field_storage_config->settings['target_type'] = 'entity_test_label';
$field_storage_config->save();
$referenced_entity_with_no_link_template = entity_create('entity_test_label', array(
'name' => $this->randomMachineName(),
));
$referenced_entity_with_no_link_template->save();
$build = $this->buildRenderArray([$referenced_entity_with_no_link_template], $formatter, array('link' => TRUE));
$this->assertEqual($build[0]['#markup'], $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));
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment