You are here

public function WorkspaceViewBuilder::buildComponents in Drupal 10

Same name and namespace in other branches
  1. 9 core/modules/workspaces/src/WorkspaceViewBuilder.php \Drupal\workspaces\WorkspaceViewBuilder::buildComponents()

Builds the component fields and properties of a set of entities.

Parameters

&$build: The renderable array representing the entity content.

\Drupal\Core\Entity\EntityInterface[] $entities: The entities whose content is being built.

\Drupal\Core\Entity\Display\EntityViewDisplayInterface[] $displays: The array of entity view displays holding the display options configured for the entity components, keyed by bundle name.

string $view_mode: The view mode in which the entity is being viewed.

Overrides EntityViewBuilder::buildComponents

File

core/modules/workspaces/src/WorkspaceViewBuilder.php, line 60

Class

WorkspaceViewBuilder
Provides a Workspace view builder.

Namespace

Drupal\workspaces

Code

public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
  parent::buildComponents($build, $entities, $displays, $view_mode);
  $bundle_info = $this->bundleInfo
    ->getAllBundleInfo();
  $header = [
    'title' => $this
      ->t('Title'),
    'type' => $this
      ->t('Type'),
    'changed' => $this
      ->t('Last changed'),
    'owner' => $this
      ->t('Author'),
    'operations' => $this
      ->t('Operations'),
  ];
  foreach ($entities as $build_id => $entity) {
    $all_tracked_entities = $this->workspaceAssociation
      ->getTrackedEntities($entity
      ->id());
    $build[$build_id]['changes']['overview'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Workspace changes'),
    ];
    $build[$build_id]['changes']['list'] = [
      '#type' => 'table',
      '#header' => $header,
      '#empty' => $this
        ->t('This workspace has no changes.'),
    ];
    $changes_count = [];
    foreach ($all_tracked_entities as $entity_type_id => $tracked_entities) {

      // Ensure that newest revisions are displayed at the top.
      krsort($tracked_entities);
      $changes_count[$entity_type_id] = $this->entityTypeManager
        ->getDefinition($entity_type_id)
        ->getCountLabel(count($tracked_entities));
      $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);
      if ($this->entityTypeManager
        ->hasHandler($entity_type_id, 'list_builder')) {
        $list_builder = $this->entityTypeManager
          ->getListBuilder($entity_type_id);
      }
      else {
        $list_builder = $this->entityTypeManager
          ->createHandlerInstance(EntityListBuilder::class, $entity_type);
      }
      $revisions = $this->entityTypeManager
        ->getStorage($entity_type_id)
        ->loadMultipleRevisions(array_keys($tracked_entities));

      // Load all users at once.
      $user_ids = [];
      foreach ($revisions as $revision) {
        if ($revision instanceof EntityOwnerInterface) {
          $user_ids[$revision
            ->getOwnerId()] = $revision
            ->getOwnerId();
        }
      }
      if ($user_ids = array_filter($user_ids)) {
        $revision_owners = $this->entityTypeManager
          ->getStorage('user')
          ->loadMultiple($user_ids);
      }
      foreach ($revisions as $revision) {
        if ($revision
          ->getEntityType()
          ->hasLinkTemplate('canonical')) {
          $title = [
            '#type' => 'link',
            '#title' => $revision
              ->label(),
            '#url' => $revision
              ->toUrl(),
          ];
        }
        else {
          $title = [
            '#markup' => $revision
              ->label(),
          ];
        }
        if (count($bundle_info[$entity_type_id]) > 1) {
          $type = [
            '#markup' => $this
              ->t('@entity_type_label: @entity_bundle_label', [
              '@entity_type_label' => $entity_type
                ->getLabel(),
              '@entity_bundle_label' => $bundle_info[$entity_type_id][$revision
                ->bundle()]['label'],
            ]),
          ];
        }
        else {
          $type = [
            '#markup' => $bundle_info[$entity_type_id][$revision
              ->bundle()]['label'],
          ];
        }
        $changed = $revision instanceof EntityChangedInterface ? $this->dateFormatter
          ->format($revision
          ->getChangedTime()) : '';
        if ($revision instanceof EntityOwnerInterface && isset($revision_owners[$revision
          ->getOwnerId()])) {
          $author = [
            '#theme' => 'username',
            '#account' => $revision_owners[$revision
              ->getOwnerId()],
          ];
        }
        else {
          $author = [
            '#markup' => '',
          ];
        }
        $build[$build_id]['changes']['list'][$entity_type_id . ':' . $revision
          ->id()] = [
          '#entity' => $revision,
          'title' => $title,
          'type' => $type,
          'changed' => [
            '#markup' => $changed,
          ],
          'owner' => $author,
          'operations' => [
            '#type' => 'operations',
            '#links' => $list_builder
              ->getOperations($revision),
          ],
        ];
      }
    }
    if ($changes_count) {
      $build[$build_id]['changes']['overview']['#markup'] = implode(', ', $changes_count);
    }
  }
}