Commit 7c6a23fe authored by alexpott's avatar alexpott

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
......@@ -8,6 +8,7 @@
namespace Drupal\entity_reference\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\String;
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
......@@ -61,12 +62,26 @@ public function settingsSummary() {
*/
public function viewElements(FieldItemListInterface $items) {
$elements = array();
$output_as_link = $this->getSetting('link');
foreach ($this->getEntitiesToView($items) as $delta => $entity) {
$label = $entity->label();
// If the link is to be displayed and the entity has a uri, display a
// 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] = [
'#type' => 'link',
'#title' => $label,
......
......@@ -9,6 +9,7 @@
use Drupal\Core\Cache\Cache;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Entity\FilterFormat;
use Drupal\system\Tests\Entity\EntityUnitTestBase;
......@@ -226,6 +227,21 @@ public function testLabelFormatter() {
$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[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));
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment