You are here

protected function ListUsageController::getSourceEntityLink in Entity Usage 8.2

Same name and namespace in other branches
  1. 8.4 src/Controller/ListUsageController.php \Drupal\entity_usage\Controller\ListUsageController::getSourceEntityLink()

Retrieve a link to the source entity.

Note that some entities are special-cased, since they don't have canonical template and aren't expected to be re-usable. For example, if the entity passed in is a paragraph or a block content, the link we produce will point to this entity's parent (host) entity instead.

Parameters

\Drupal\Core\Entity\EntityInterface $source_entity: The source entity.

string|null $text: (optional) The link text for the anchor tag as a translated string. If NULL, it will use the entity's label. Defaults to NULL.

Return value

\Drupal\Core\Link|string|false A link to the entity, or its non-linked label, in case it was impossible to correctly build a link. Will return FALSE if this item should not be shown on the UI (for example when dealing with an orphan paragraph).

1 call to ListUsageController::getSourceEntityLink()
ListUsageController::getRows in src/Controller/ListUsageController.php
Retrieve all usage rows for this target entity.

File

src/Controller/ListUsageController.php, line 333

Class

ListUsageController
Controller for our pages.

Namespace

Drupal\entity_usage\Controller

Code

protected function getSourceEntityLink(EntityInterface $source_entity, $text = NULL) {

  // Note that $paragraph_entity->label() will return a string of type:
  // "{parent label} > {parent field}", which is actually OK for us.
  $entity_label = $source_entity
    ->access('view label') ? $source_entity
    ->label() : $this
    ->t('- Restricted access -');
  $rel = NULL;
  if ($source_entity
    ->hasLinkTemplate('revision')) {
    $rel = 'revision';
  }
  elseif ($source_entity
    ->hasLinkTemplate('canonical')) {
    $rel = 'canonical';
  }

  // Block content likely used in Layout Builder inline blocks.
  if ($source_entity instanceof BlockContentInterface && !$source_entity
    ->isReusable()) {
    $rel = NULL;
  }
  $link_text = $text ?: $entity_label;
  if ($rel) {

    // Prevent 404s by exposing the text unlinked if the user has no access
    // to view the entity.
    return $source_entity
      ->access('view') ? $source_entity
      ->toLink($link_text, $rel) : $link_text;
  }

  // Treat paragraph entities in a special manner. Normal paragraph entities
  // only exist in the context of their host (parent) entity. For this reason
  // we will use the link to the parent's entity label instead.

  /** @var \Drupal\paragraphs\ParagraphInterface $source_entity */
  if ($source_entity
    ->getEntityTypeId() == 'paragraph') {
    $parent = $source_entity
      ->getParentEntity();
    if ($parent) {
      return $this
        ->getSourceEntityLink($parent, $link_text);
    }
  }
  elseif ($source_entity
    ->getEntityTypeId() === 'block_content') {
    $sources = $this->entityUsage
      ->listSources($source_entity, FALSE);
    $source = reset($sources);
    if ($source !== FALSE) {
      $parent = $this
        ->entityTypeManager()
        ->getStorage($source['source_type'])
        ->load($source['source_id']);
      if ($parent) {
        return $this
          ->getSourceEntityLink($parent);
      }
    }
  }

  // As a fallback just return a non-linked label.
  return $link_text;
}