You are here

public function ListUsageController::listUsagePage in Entity Usage 8.3

Same name and namespace in other branches
  1. 8.4 src/Controller/ListUsageController.php \Drupal\entity_usage\Controller\ListUsageController::listUsagePage()
  2. 8 src/Controller/ListUsageController.php \Drupal\entity_usage\Controller\ListUsageController::listUsagePage()
  3. 8.2 src/Controller/ListUsageController.php \Drupal\entity_usage\Controller\ListUsageController::listUsagePage()

Lists the usage of a given entity.

Parameters

string $entity_type: The entity type.

int $entity_id: The entity ID.

Return value

array The page build to be rendered.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException

1 call to ListUsageController::listUsagePage()
LocalTaskUsageController::listUsageLocalTask in src/Controller/LocalTaskUsageController.php
Lists the usage of a given entity.
1 string reference to 'ListUsageController::listUsagePage'
entity_usage.routing.yml in ./entity_usage.routing.yml
entity_usage.routing.yml

File

src/Controller/ListUsageController.php, line 105

Class

ListUsageController
Controller for our pages.

Namespace

Drupal\entity_usage\Controller

Code

public function listUsagePage($entity_type, $entity_id) {

  // Alert users that there may be hidden info in past revisions, if that's
  // the case.
  $target_has_hidden_usages = $this
    ->checkHiddenUsages($entity_type, $entity_id);
  $items_per_group = $this->entityUsageConfig
    ->get('usage_controller_items_per_group') ?: self::ITEMS_PER_GROUP_DEFAULT;
  $total = $this
    ->getPageRows($entity_type, $entity_id, NULL, NULL, TRUE);
  $page = pager_default_initialize($total, $items_per_group);
  $value_rows = $this
    ->getPageRows($entity_type, $entity_id, $page * $items_per_group, $items_per_group);
  if (empty($value_rows)) {
    if ($target_has_hidden_usages) {
      $empty_message = $this
        ->t('There are no recorded usages for entity of type: @type with id: @id, but some usages were found in past versions (revisions) of some nodes.', [
        '@type' => $entity_type,
        '@id' => $entity_id,
      ]);
    }
    else {
      $empty_message = $this
        ->t('There are no recorded usages for entity of type: @type with id: @id', [
        '@type' => $entity_type,
        '@id' => $entity_id,
      ]);
    }
    return [
      '#markup' => $empty_message,
    ];
  }
  $header = [
    $this
      ->t('Entity'),
    $this
      ->t('Type'),
    $this
      ->t('Language'),
    $this
      ->t('Status'),
  ];
  $entity_types = $this->entityTypeManager
    ->getDefinitions();
  $languages = $this
    ->languageManager()
    ->getLanguages(LanguageInterface::STATE_ALL);
  foreach ($value_rows as $row) {
    $type_storage = $this->entityTypeManager
      ->getStorage($row['source_type']);
    $source_entity = $type_storage
      ->load($row['source_id']);
    if (!$source_entity) {

      // If for some reason this record is broken, just skip it.
      continue;
    }

    // Prepare the link to the source entity.
    $source_link = $this
      ->getSourceEntityCanonicalLink($source_entity);

    // Prepare the language name to display.
    $lang_label = !empty($languages[$row['source_langcode']]) ? $languages[$row['source_langcode']]
      ->getName() : '-';

    // Prepare the status text.
    $published = '-';
    if ($source_entity instanceof EntityPublishedInterface) {
      $published = $source_entity
        ->isPublished() ? $this
        ->t('Published') : $this
        ->t('Unpublished');
    }
    $rows[] = [
      $source_link,
      $entity_types[$row['source_type']]
        ->getLabel(),
      $lang_label,
      $published,
    ];
  }
  $build[] = [
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
  ];
  $build[] = [
    '#type' => 'pager',
  ];
  if ($target_has_hidden_usages) {
    $build[] = [
      '#markup' => $this
        ->t('Note: This page only includes usages in the current revisions of referencing entities. This entity is also being used in nodes that no longer reference it on their current revision.'),
    ];
  }
  return $build;
}