You are here

public function ListUsageController::listUsagePage in Entity Usage 8

Same name and namespace in other branches
  1. 8.4 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 $type: The entity type.

int $id: The entity ID.

Return value

array The page build to be rendered.

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 66

Class

ListUsageController
Controller for our pages.

Namespace

Drupal\entity_usage\Controller

Code

public function listUsagePage($type, $id) {
  $entity_types = array_keys($this->entityTypeManager
    ->getDefinitions());
  if (!is_string($type) || !is_numeric($id) || !in_array($type, $entity_types)) {
    throw new NotFoundHttpException();
  }
  $entity = $this->entityTypeManager
    ->getStorage($type)
    ->load($id);
  if ($entity) {
    $usages = $this->entityUsage
      ->listUsage($entity, TRUE);
    if (empty($usages)) {

      // Entity exists but not used.
      $build = [
        '#markup' => $this
          ->t('There are no recorded usages for entity of type: @type with id: @id', [
          '@type' => $type,
          '@id' => $id,
        ]),
      ];
    }
    else {

      // Entity is being used.
      $header = [
        $this
          ->t('Referencing entity'),
        $this
          ->t('Referencing entity type'),
        $this
          ->t('Referencing method'),
        $this
          ->t('Count'),
      ];
      $rows = [];
      foreach ($usages as $method => $method_usages) {
        foreach ($method_usages as $re_type => $type_usages) {
          foreach ($type_usages as $re_id => $count) {
            $referencing_entity = $this->entityTypeManager
              ->getStorage($re_type)
              ->load($re_id);
            if ($referencing_entity) {
              $link = $this
                ->getReferencingEntityLink($referencing_entity);
              $rows[] = [
                $link,
                $re_type,
                $method,
                $count,
              ];
            }
          }
        }
      }
      $build = [
        '#theme' => 'table',
        '#rows' => $rows,
        '#header' => $header,
      ];
    }
  }
  else {

    // Non-existing entity in database.
    $build = [
      '#markup' => $this
        ->t('Could not find the entity of type: @type with id: @id', [
        '@type' => $type,
        '@id' => $id,
      ]),
    ];
  }
  return $build;
}