You are here

FlagListItemListBuilder.php in Flag Lists 8

Same filename and directory in other branches
  1. 4.0.x src/FlagListItemListBuilder.php

Namespace

Drupal\flag_lists

File

src/FlagListItemListBuilder.php
View source
<?php

namespace Drupal\flag_lists;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Link;

/**
 * Defines a class to build a listing of Flag list item entities.
 *
 * @ingroup flag_lists
 */
class FlagListItemListBuilder extends EntityListBuilder {

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['id'] = $this
      ->t('Flag list item ID');
    $header['name'] = $this
      ->t('Name');
    $header['entity_id'] = $this
      ->t('Entity ID');
    $header['entity_exist'] = $this
      ->t('Entity exist?');
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {

    /* @var $entity \Drupal\flag_lists\Entity\FlagListItem */
    $account = \Drupal::currentUser()
      ->getAccount();
    $entity_id = $entity
      ->getConnectedEntityId();
    $connectedEntity = \Drupal::entityTypeManager()
      ->getStorage('node')
      ->load($entity_id);
    if ($entity
      ->access('view', $account)) {
      $row['id'] = $entity
        ->id();
      $row['name'] = Link::createFromRoute($entity
        ->label(), 'entity.flag_list_item.edit_form', [
        'flag_list_item' => $entity
          ->id(),
      ]);
      $row['entity_id'] = $entity_id;
      $row['entity_exist']['data'] = empty($connectedEntity) ? "Doesn't exist" : 'Exist';
      $row['entity_exist']['class'] = empty($connectedEntity) ? "entity-missing" : 'entity-exist';
      return $row + parent::buildRow($entity);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $build = parent::render();
    $build['table']['#attached']['library'][] = 'flag_lists/flag_lists.table';
    return $build;
  }

}

Classes

Namesort descending Description
FlagListItemListBuilder Defines a class to build a listing of Flag list item entities.