WebhookListBuilder.php in Webhooks 8
File
modules/webhook/src/WebhookListBuilder.php
View source
<?php
namespace Drupal\webhook;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebhookListBuilder extends EntityListBuilder {
protected $dateFormatter;
protected $redirectDestination;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination) {
parent::__construct($entity_type, $storage);
$this->dateFormatter = $date_formatter;
$this->redirectDestination = $redirect_destination;
}
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('date.formatter'), $container
->get('redirect.destination'));
}
public function render() {
$build['table'] = parent::render();
$total = $this
->getStorage()
->getQuery()
->count()
->execute();
$build['summary']['#markup'] = $this
->t('Total webhooks: @total', [
'@total' => $total,
]);
return $build;
}
public function buildHeader() {
$header['id'] = $this
->t('ID');
$header['title'] = $this
->t('Title');
$header['created'] = $this
->t('Created');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row['id'] = $entity
->id();
$row['title'] = $entity
->toLink();
$row['created'] = $this->dateFormatter
->format($entity
->getCreatedTime());
return $row + parent::buildRow($entity);
}
protected function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
$destination = $this->redirectDestination
->getAsArray();
foreach ($operations as $key => $operation) {
$operations[$key]['query'] = $destination;
}
return $operations;
}
}