BrightcovePlaylistListBuilder.php in Brightcove Video Connect 3.x
File
src/BrightcovePlaylistListBuilder.php
View source
<?php
namespace Drupal\brightcove;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Routing\LinkGeneratorTrait;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BrightcovePlaylistListBuilder extends EntityListBuilder {
use LinkGeneratorTrait;
protected $accountProxy;
protected $dateFormatter;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, AccountProxy $account_proxy, DateFormatter $date_formatter) {
parent::__construct($entity_type, $storage);
$this->accountProxy = $account_proxy;
$this->dateFormatter = $date_formatter;
}
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('current_user'), $container
->get('date.formatter'));
}
protected function getEntityIds() {
$query = $this
->getStorage()
->getQuery()
->sort('changed', 'DESC');
if ($this->limit) {
$query
->pager($this->limit);
}
return $query
->execute();
}
public function buildHeader() {
$header = [
'name' => $this
->t('Name'),
'updated' => $this
->t('Updated'),
'reference_id' => $this
->t('Reference ID'),
];
if ($this->accountProxy
->hasPermission('edit brightcove playlists') || $this->accountProxy
->hasPermission('delete brightcove playlists')) {
$header += parent::buildHeader();
}
return $header;
}
public function buildRow(EntityInterface $entity) {
if ($entity
->isPublished() && $this->accountProxy
->hasPermission('view published brightcove playlists') || !$entity
->isPublished() && $this->accountProxy
->hasPermission('view unpublished brightcove playlists')) {
$name = $this
->l($entity
->label(), new Url('entity.brightcove_playlist.canonical', [
'brightcove_playlist' => $entity
->id(),
]));
}
else {
$name = $entity
->label();
}
$row = [
'name' => $name,
'updated' => $this->dateFormatter
->format($entity
->getChangedTime(), 'short'),
'reference_id' => $entity
->getReferenceId(),
];
if ($this->accountProxy
->hasPermission('edit brightcove playlists') || $this->accountProxy
->hasPermission('delete brightcove playlists')) {
$row += parent::buildRow($entity);
}
return $row;
}
}