FlaggingCollectionListBuilder.php in Flag Lists 4.0.x
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;
class FlaggingCollectionListBuilder extends EntityListBuilder {
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');
return $header;
}
public function buildRow(EntityInterface $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);
}
}
public function render() {
$build = parent::render();
if (!empty($build['table']['#rows'])) {
uasort($build['table']['#rows'], [
$this,
'numberOfOperations',
]);
}
return $build;
}
private function numberOfOperations($a, $b) {
if (count($a['operations']['data']['#links']) == count($b['operations']['data']['#links'])) {
return 0;
}
return count($a['operations']['data']['#links']) < count($b['operations']['data']['#links']) ? 1 : -1;
}
}