You are here

public function ListUsageController::listUsagePage in Entity Usage 8.4

Same name and namespace in other branches
  1. 8 src/Controller/ListUsageController.php \Drupal\entity_usage\Controller\ListUsageController::listUsagePage()
  2. 8.2 src/Controller/ListUsageController.php \Drupal\entity_usage\Controller\ListUsageController::listUsagePage()
  3. 8.3 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 120

Class

ListUsageController
Controller for our pages.

Namespace

Drupal\entity_usage\Controller

Code

public function listUsagePage($entity_type, $entity_id) {
  $all_rows = $this
    ->getRows($entity_type, $entity_id);
  if (empty($all_rows)) {
    return [
      '#markup' => $this
        ->t('There are no recorded usages for entity of type: @type with id: @id', [
        '@type' => $entity_type,
        '@id' => $entity_id,
      ]),
    ];
  }
  $header = [
    $this
      ->t('Entity'),
    $this
      ->t('Type'),
    $this
      ->t('Language'),
    $this
      ->t('Field name'),
    $this
      ->t('Status'),
    $this
      ->t('Used in'),
  ];
  $total = count($all_rows);
  $pager = $this->pagerManager
    ->createPager($total, $this->itemsPerPage);
  $page = $pager
    ->getCurrentPage();
  $page_rows = $this
    ->getPageRows($page, $this->itemsPerPage, $entity_type, $entity_id);

  // If all rows on this page are of entities that have usage on their default
  // revision, we don't need the "Used in" column.
  $used_in_previous_revisions = FALSE;
  foreach ($page_rows as $row) {
    if ($row[5] == $this
      ->t('Translations or previous revisions')) {
      $used_in_previous_revisions = TRUE;
      break;
    }
  }
  if (!$used_in_previous_revisions) {
    unset($header[5]);
    array_walk($page_rows, function (&$row, $key) {
      unset($row[5]);
    });
  }
  $build[] = [
    '#theme' => 'table',
    '#rows' => $page_rows,
    '#header' => $header,
  ];
  $build[] = [
    '#type' => 'pager',
    '#route_name' => '<current>',
  ];
  return $build;
}