You are here

protected function ListUsageController::getRows in Entity Usage 8.2

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

Retrieve all usage rows for this target entity.

Parameters

string $entity_type: The type of the target entity.

int|string $entity_id: The ID of the target entity.

Return value

array An indexed array of rows that should be displayed as sources for this target entity.

2 calls to ListUsageController::getRows()
ListUsageController::getPageRows in src/Controller/ListUsageController.php
Get rows for a given page.
ListUsageController::listUsagePage in src/Controller/ListUsageController.php
Lists the usage of a given entity.

File

src/Controller/ListUsageController.php, line 182

Class

ListUsageController
Controller for our pages.

Namespace

Drupal\entity_usage\Controller

Code

protected function getRows($entity_type, $entity_id) {
  if (!empty($this->allRows)) {
    return $this->allRows;

    // @todo Cache this based on the target entity, invalidating the cached
    // results every time records are added/removed to the same target entity.
  }
  $rows = [];
  $entity = $this->entityTypeManager
    ->getStorage($entity_type)
    ->load($entity_id);
  if (!$entity) {
    return $rows;
  }
  $entity_types = $this->entityTypeManager
    ->getDefinitions();
  $languages = $this
    ->languageManager()
    ->getLanguages(LanguageInterface::STATE_ALL);
  $all_usages = $this->entityUsage
    ->listSources($entity);
  foreach ($all_usages as $source_type => $ids) {
    $type_storage = $this->entityTypeManager
      ->getStorage($source_type);
    foreach ($ids as $source_id => $records) {

      // We will show a single row per source entity. If the target is not
      // referenced on its default revision on the default language, we will
      // just show indicate that in a specific column.
      $source_entity = $type_storage
        ->load($source_id);
      if (!$source_entity) {

        // If for some reason this record is broken, just skip it.
        continue;
      }
      $field_definitions = $this->entityFieldManager
        ->getFieldDefinitions($source_type, $source_entity
        ->bundle());
      if ($source_entity instanceof RevisionableInterface) {
        $default_revision_id = $source_entity
          ->getRevisionId();
        $default_langcode = $source_entity
          ->language()
          ->getId();
        $used_in_default = FALSE;
        $default_key = 0;
        foreach ($records as $key => $record) {
          if ($record['source_vid'] == $default_revision_id && $record['source_langcode'] == $default_langcode) {
            $default_key = $key;
            $used_in_default = TRUE;
            break;
          }
        }
        $used_in_text = $used_in_default ? $this
          ->t('Default') : $this
          ->t('Translations or previous revisions');
      }
      $link = $this
        ->getSourceEntityLink($source_entity);

      // If the label is empty it means this usage shouldn't be shown
      // on the UI, just skip this row.
      if (empty($link)) {
        continue;
      }
      $published = $this
        ->getSourceEntityStatus($source_entity);
      $field_label = isset($field_definitions[$records[$default_key]['field_name']]) ? $field_definitions[$records[$default_key]['field_name']]
        ->getLabel() : $this
        ->t('Unknown');
      $rows[] = [
        $link,
        $entity_types[$source_type]
          ->getLabel(),
        $languages[$default_langcode]
          ->getName(),
        $field_label,
        $published,
        $used_in_text,
      ];
    }
  }
  $this->allRows = $rows;
  return $this->allRows;
}