You are here

protected function ListUsageController::getPageRows in Entity Usage 8.3

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

Query the DB for the next page of items to display.

Parameters

string $target_type: The target entity type.

string $target_id: The target entity ID.

int $start: The initial position to start the query range.

int $items_per_page: The number of items per page to use in the query range.

bool $count_only: (optional) Whether to return an integer with the total number of rows in the query, which can be used when calculating the pager output. Defaults to FALSE.

Return value

array|int An indexed array of source entities info, where values are:

  • source_type: The source entity type.
  • source_id: The ID of the source entity.
  • source_langcode: The langcode of the source entity.

Will return an integer with the total rows for this query if the flag $count_only is passed in.

1 call to ListUsageController::getPageRows()
ListUsageController::listUsagePage in src/Controller/ListUsageController.php
Lists the usage of a given entity.

File

src/Controller/ListUsageController.php, line 210

Class

ListUsageController
Controller for our pages.

Namespace

Drupal\entity_usage\Controller

Code

protected function getPageRows($target_type, $target_id, $start, $items_per_page, $count_only = FALSE) {
  $rows = [];
  $top_level_types = EntityUsageSourceLevel::getTopLevelEntityTypes();
  foreach ($top_level_types as $source_type) {
    $source_definition = \Drupal::entityTypeManager()
      ->getDefinition($source_type);
    $source_base_table = $source_definition
      ->getBaseTable();
    $source_revision_key = $source_definition
      ->hasKey('revision') ? $source_definition
      ->getKey('revision') : NULL;
    $query = $this->database
      ->select('entity_usage', 'eu')
      ->fields('eu', [
      'source_id',
      'source_id_string',
      'source_langcode',
    ])
      ->orderBy('source_id', 'DESC')
      ->condition('target_type', $target_type)
      ->condition('target_id', $target_id);

    // If the source is revisionable, join so we only include default
    // revisions in the results.
    if (!empty($source_base_table) && !empty($source_revision_key)) {
      $query
        ->innerJoin($source_base_table, 'sbt', "sbt.{$source_revision_key} = eu.source_vid");
    }
    if ($count_only) {
      return (int) $query
        ->countQuery()
        ->execute()
        ->fetchField();
    }
    $db_rows = $query
      ->range($start, $items_per_page)
      ->execute()
      ->fetchAll();
    foreach ($db_rows as $db_row) {
      $rows[] = [
        'source_type' => $source_type,
        'source_id' => $db_row->source_id ?? $db_row->source_id_string,
        'source_langcode' => $db_row->source_langcode,
      ];
    }
  }

  // Sort by entity type ASC and then by entity ID DESC.
  array_multisort(array_column($rows, 'source_type'), SORT_ASC, $rows);
  array_multisort(array_column($rows, 'source_id'), SORT_DESC, $rows);
  return $rows;
}