You are here

FlaggingCollectionListBuilder.php in Flag Lists 4.0.x

Same filename and directory in other branches
  1. 8 src/FlaggingCollectionListBuilder.php

Namespace

Drupal\flag_lists

File

src/FlaggingCollectionListBuilder.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 Flagging collection entities.
 *
 * @ingroup flag_lists
 */
class FlaggingCollectionListBuilder extends EntityListBuilder {

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['name'] = $this
      ->t('Flagging Collection');
    $header['id'] = $this
      ->t('ID');
    $header['bundle']['data'] = $this
      ->t('Bundle');
    $header['templateflag']['data'] = $this
      ->t('Flag for List');
    $header['relatedflag']['data'] = $this
      ->t('The related flag');
    $header['base_flag']['data'] = $this
      ->t('Template Flag');
    $header['owner']['data'] = $this
      ->t('Owner');
    $header['scope']['data'] = $this
      ->t('Scope');
    $header['operations']['data'] = $this
      ->t('Operations');

    // Don't call parent as we add the Operations header ourselves
    // $header[] = parent::buildHeader();
    return $header;
  }

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

    /**  @var \Drupal\flag_lists\Entity\FlaggingCollectioni $entity */
    $baseflag = $entity
      ->getBaseFlag();
    $relatedflag = $entity
      ->getRelatedFlag();
    $flagListService = \Drupal::service('flaglists');
    $flag = $flagListService
      ->getFlagForListById($baseflag
      ->id());
    $account = \Drupal::currentUser()
      ->getAccount();
    if ($entity
      ->access('view', $account)) {
      $row = [];
      $row['name'] = Link::createFromRoute($entity
        ->label(), 'entity.flagging_collection.edit_form', [
        'flagging_collection' => $entity
          ->id(),
      ]);
      $row['id'] = $entity
        ->id();
      $bundle = Link::createFromRoute($flagListService
        ->getFlaggingCollectionTypeById($entity
        ->bundle())
        ->label(), 'entity.flagging_collection_type.collection');
      if ($bundle
        ->getUrl()
        ->access($account)) {
        $row['bundle'] = $bundle;
      }
      else {
        $row['bundle'] = $flagListService
          ->getFlaggingCollectionTypeById($entity
          ->bundle())
          ->label();
      }
      $templateFlag = Link::createFromRoute($flagListService
        ->getFlagForListById($baseflag
        ->id())
        ->label(), 'entity.flag_for_list.collection');
      if ($templateFlag
        ->getUrl()
        ->access($account)) {
        $row['templateflag'] = $templateFlag;
      }
      else {
        $row['templateFlag'] = $flagListService
          ->getFlagForListById($baseflag
          ->id())
          ->label();
      }
      $row['relatedflag'] = $relatedflag
        ->label();
      $row['base_flag'] = $baseflag
        ->label();
      $row['owner'] = $entity
        ->getOwner()
        ->label();
      $row['scope'] = $baseflag
        ->isGlobal() ? $this
        ->t('Global') : $this
        ->t('Personal');
      return $row + parent::buildRow($entity);
    }
  }

  /**
   * {@inheritdoc}
   *
   * Do sorting based on number of operations.
   */
  public function render() {
    $build = parent::render();
    if (!empty($build['table']['#rows'])) {
      uasort($build['table']['#rows'], [
        $this,
        'numberOfOperations',
      ]);
    }
    return $build;
  }

  /**
   * Check how many operations links there are.
   *
   * This is a helper to sort Flagging Collections with less
   * operation items, normally indicating collection not owned
   * by "me".
   */
  private function numberOfOperations($a, $b) {
    if (count($a['operations']['data']['#links']) == count($b['operations']['data']['#links'])) {
      return 0;
    }

    // Note the reverse sort order!
    return count($a['operations']['data']['#links']) < count($b['operations']['data']['#links']) ? 1 : -1;
  }

}

Classes

Namesort descending Description
FlaggingCollectionListBuilder Defines a class to build a listing of Flagging collection entities.