You are here

public function RelationListBuilder::buildRow in Relation 8

Same name and namespace in other branches
  1. 8.2 src/RelationListBuilder.php \Drupal\relation\RelationListBuilder::buildRow()

Builds a row for an entity in the entity listing.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for this row of the list.

Return value

array A render array structure of fields for this entity.

Overrides EntityListBuilder::buildRow

See also

\Drupal\Core\Entity\EntityListBuilder::render()

File

src/RelationListBuilder.php, line 40
Contains \Drupal\relation\RelationListBuilder.

Class

RelationListBuilder
Provides a listing of relation types.

Namespace

Drupal\relation

Code

public function buildRow(EntityInterface $entity) {
  $row['label']['data'] = array(
    '#type' => 'link',
    '#title' => $entity
      ->label(),
  ) + $entity
    ->toUrl()
    ->toRenderArray();
  $storage_handler = \Drupal::entityTypeManager()
    ->getStorage($entity
    ->getEntityType()
    ->getBundleEntityType());
  $bundle = $storage_handler
    ->load($entity
    ->bundle());
  $row['relation_type']['data'] = array(
    '#type' => 'link',
    '#title' => $bundle
      ->label(),
  ) + $bundle
    ->toUrl()
    ->toRenderArray();

  // Sort entities by their type.
  foreach ($entity->endpoints as $endpoint) {
    $entities[$endpoint->entity_type][] = $endpoint->entity_id;
  }
  $relation_entities = array();
  $entity_count_total = 0;
  $entity_count = 0;
  foreach ($entities as $type => $ids) {
    $entity_count_total += count(array_unique($ids));
    $storage_handler = \Drupal::entityTypeManager()
      ->getStorage($type);
    $endpoint_entities = $storage_handler
      ->loadMultiple($ids);
    foreach ($endpoint_entities as $endpoint_entity) {
      $entity_count++;
      $relation_entities[] = array(
        '#type' => 'link',
        '#title' => $endpoint_entity
          ->label(),
      ) + $endpoint_entity
        ->toUrl()
        ->toRenderArray();
    }
  }
  if ($entity_count_total != $entity_count) {
    $relation_entities[] = \Drupal::translation()
      ->formatPlural($entity_count_total - $entity_count, 'Missing @count entity', 'Missing @count entities');
  }
  $row['endpoints']['data']['list'] = array(
    '#theme' => 'item_list',
    '#items' => $relation_entities,
  );
  return $row + parent::buildRow($entity);
}