knowledge-8.x-1.x-dev/src/WaveListBuilder.php
src/WaveListBuilder.php
<?php
declare(strict_types=1);
namespace Drupal\knowledge;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a list controller for the wave entity type.
*/
final class WaveListBuilder extends EntityListBuilder {
/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('entity_type.manager')->getStorage($entity_type->id()),
$container->get('state')
);
}
/**
* Constructs a new EntityListBuilder 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\State\StateInterface $state
* The state service.
*/
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, StateInterface $state) {
parent::__construct($entity_type, $storage);
$this->state = $state;
}
/**
* {@inheritdoc}
*/
public function buildHeader(): array {
$header['id'] = $this->t('ID');
$header['label'] = $this->t('Label');
$header['default'] = $this->t('Default');
$header['start'] = $this->t('Start');
$header['created'] = $this->t('Created');
$header['changed'] = $this->t('Updated');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity): array {
/** @var \Drupal\knowledge\WaveInterface $entity */
$default = $this->state->get('knowledge.wave_default', 1);
$row['id'] = $entity->id();
$row['label'] = $entity->toLink();
$row['default'] = $entity->id() == $default ? $this->t('Yes') : $this->t('No');
$row['start']['data'] = $entity->get('start')->view(['label' => 'hidden']);
$row['created']['data'] = $entity->get('created')->view(['label' => 'hidden']);
$row['changed']['data'] = $entity->get('changed')->view(['label' => 'hidden']);
return $row + parent::buildRow($entity);
}
}
