Unverified Commit 58cffff6 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3013029 by amateescu, dolu: StringFormatter should check whether an...

Issue #3013029 by amateescu, dolu: StringFormatter should check whether an entity type has a 'revision' link template
parent 78da9a1d
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -124,7 +124,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    $url = NULL;
    if ($this->getSetting('link_to_entity')) {
      // For the default revision this falls back to 'canonical'.
      $url = $this->getEntityUrl($items->getEntity());
    }

@@ -173,8 +172,11 @@ protected function viewValue(FieldItemInterface $item) {
   *   The URI elements of the entity.
   */
  protected function getEntityUrl(EntityInterface $entity) {
    // For the default revision this falls back to 'canonical'.
    return $entity->toUrl('revision');
    // For the default revision, the 'revision' link template falls back to
    // 'canonical'.
    // @see \Drupal\Core\Entity\Entity::toUrl()
    $rel = $entity->getEntityType()->hasLinkTemplate('revision') ? 'revision' : 'canonical';
    return $entity->toUrl($rel);
  }

}
+23 −1
Original line number Diff line number Diff line
@@ -24,6 +24,13 @@ class StringFormatterTest extends KernelTestBase {
   */
  public static $modules = ['field', 'text', 'entity_test', 'system', 'filter', 'user'];

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * @var string
   */
@@ -79,6 +86,8 @@ protected function setUp() {
        'settings' => [],
      ]);
    $this->display->save();

    $this->entityTypeManager = \Drupal::entityTypeManager();
  }

  /**
@@ -145,7 +154,7 @@ public function testStringFormatter() {
    $value2 = $this->randomMachineName();
    $entity->{$this->fieldName}->value = $value2;
    $entity->save();
    $entity_new_revision = \Drupal::entityManager()->getStorage('entity_test_rev')->loadRevision($old_revision_id);
    $entity_new_revision = $this->entityTypeManager->getStorage('entity_test_rev')->loadRevision($old_revision_id);

    $this->renderEntityFields($entity, $this->display);
    $this->assertLink($value2, 0);
@@ -154,6 +163,19 @@ public function testStringFormatter() {
    $this->renderEntityFields($entity_new_revision, $this->display);
    $this->assertLink($value, 0);
    $this->assertLinkByHref('/entity_test_rev/' . $entity_new_revision->id() . '/revision/' . $entity_new_revision->getRevisionId() . '/view');

    // Check that linking to a revisionable entity works if the entity type does
    // not specify a 'revision' link template.
    $entity_type = clone $this->entityTypeManager->getDefinition('entity_test_rev');
    $link_templates = $entity_type->getLinkTemplates();
    unset($link_templates['revision']);
    $entity_type->set('links', $link_templates);
    \Drupal::state()->set('entity_test_rev.entity_type', $entity_type);
    $this->entityTypeManager->clearCachedDefinitions();

    $this->renderEntityFields($entity_new_revision, $this->display);
    $this->assertLink($value, 0);
    $this->assertLinkByHref($entity->url('canonical'));
  }

}