podcast_publisher-1.0.0-alpha3/src/PodcastEpisodeListBuilder.php
src/PodcastEpisodeListBuilder.php
<?php
namespace Drupal\podcast_publisher;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a list controller for the podcast episode entity type.
*/
class PodcastEpisodeListBuilder extends EntityListBuilder {
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Id of podcast entity.
*
* @var int
*/
protected $podcastId;
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
$instance = new static(
$entity_type,
$container->get('entity_type.manager')->getStorage($entity_type->id()),
);
$instance->dateFormatter = $container->get('date.formatter');
$instance->podcastId = $container->get('current_route_match')
->getRawParameter('podcast');
return $instance;
}
/**
* {@inheritdoc}
*/
public function render() {
$build['table'] = parent::render();
$total = $this->getStorage()
->getQuery()
->condition('podcast', $this->podcastId)
->accessCheck(TRUE)
->count()
->execute();
$build['summary']['#markup'] = $this->t('Total podcast episodes: @total', ['@total' => $total]);
return $build;
}
/**
* Loads entity IDs using a pager sorted by the entity id.
*
* @return array
* An array of entity IDs.
*/
protected function getEntityIds() {
$query = $this->getStorage()->getQuery()
->condition('podcast', $this->podcastId)
->accessCheck(TRUE)
->sort($this->entityType->getKey('id'));
// Only add the pager if a limit is specified.
if ($this->limit) {
$query->pager($this->limit);
}
return $query->execute();
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['id'] = $this->t('ID');
$header['label'] = $this->t('Label');
$header['status'] = $this->t('Status');
$header['uid'] = $this->t('Author');
$header['created'] = $this->t('Created');
$header['changed'] = $this->t('Updated');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/** @var \Drupal\podcast_publisher\PodcastEpisodeInterface $entity */
$row['id'] = $entity->id();
$row['label'] = $entity->toLink();
$row['status'] = $entity->get('status')->value ? $this->t('Enabled') : $this->t('Disabled');
$row['uid']['data'] = [
'#theme' => 'username',
'#account' => $entity->getOwner(),
];
$row['created'] = $this->dateFormatter->format($entity->get('created')->value);
$row['changed'] = $this->dateFormatter->format($entity->getChangedTime());
return $row + parent::buildRow($entity);
}
}
