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 Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BrightcoveVideoListBuilder 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 = [
'video' => $this
->t('Video'),
'name' => $this
->t('Name'),
'status' => $this
->t('Status'),
'updated' => $this
->t('Updated'),
'reference_id' => $this
->t('Reference ID'),
'created' => $this
->t('Created'),
];
if ($this->accountProxy
->hasPermission('edit brightcove videos') || $this->accountProxy
->hasPermission('delete brightcove videos')) {
$header += parent::buildHeader();
}
return $header;
}
public function buildRow(EntityInterface $entity) {
if ($entity
->isPublished() && $this->accountProxy
->hasPermission('view published brightcove videos') || !$entity
->isPublished() && $this->accountProxy
->hasPermission('view unpublished brightcove videos')) {
$name = $this
->l($entity
->label(), new Url('entity.brightcove_video.canonical', [
'brightcove_video' => $entity
->id(),
]));
}
else {
$name = $entity
->label();
}
$thumbnail = $entity
->getThumbnail();
$thumbnail_image = '';
if (!empty($thumbnail['target_id'])) {
$thumbnail_file = File::load($thumbnail['target_id']);
$image_style = ImageStyle::load('brightcove_videos_list_thumbnail');
if (!empty($thumbnail_file) && !is_null($image_style)) {
$image_uri = $image_style
->buildUrl($thumbnail_file
->getFileUri());
$thumbnail_image = "<img src='{$image_uri}' alt='{$entity->getName()}'>";
}
}
$row = [
'video' => [
'data' => [
'#markup' => $thumbnail_image,
],
],
'name' => $name,
'status' => $entity
->isPublished() ? $this
->t('Active') : $this
->t('Inactive'),
'updated' => $this->dateFormatter
->format($entity
->getChangedTime(), 'short'),
'reference_id' => $entity
->getReferenceId(),
'created' => $this->dateFormatter
->format($entity
->getCreatedTime(), 'short'),
];
if ($this->accountProxy
->hasPermission('edit brightcove videos') || $this->accountProxy
->hasPermission('delete brightcove videos')) {
$row += parent::buildRow($entity);
}
return $row;
}
}