pm-4.1.x-dev/modules/pm_priority/src/PmPriorityListBuilder.php
modules/pm_priority/src/PmPriorityListBuilder.php
<?php
namespace Drupal\pm_priority;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a list controller for the Priority entity type.
*/
class PmPriorityListBuilder extends EntityListBuilder {
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Constructs a new PmPriorityListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter) {
parent::__construct($entity_type, $storage);
$this->dateFormatter = $date_formatter;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
/* @phpstan-ignore new.static */
return new static(
$entity_type,
$container->get('entity_type.manager')->getStorage($entity_type->id()),
$container->get('date.formatter')
);
}
/**
* {@inheritdoc}
*/
public function render() {
$build = [];
$build['table'] = parent::render();
$total = $this->getStorage()
->getQuery()
->accessCheck(FALSE)
->count()
->execute();
$build['summary']['#markup'] = $this->t('Total Priorities: @total', ['@total' => $total]);
return $build;
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header = [];
$header['id'] = $this->t('ID');
$header['label'] = $this->t('Label');
$header['status'] = $this->t('Status');
$header['weight'] = $this->t('Weight');
$header['color'] = $this->t('Color');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/** @var \Drupal\pm_priority\PmPriorityInterface $entity */ $row = [];
$row['id'] = $entity->id();
$row['label'] = $entity->label();
$row['status'] = $entity->get('status')->value ? $this->t('Enabled') : $this->t('Disabled');
$row['weight'] = $entity->get('weight')->value;
$row['color'] = $entity->get('color')->value;
return $row + parent::buildRow($entity);
}
}
