View source
<?php
namespace Drupal\rng\Lists;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\rng\EventManagerInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RuleListBuilder extends EntityListBuilder {
protected $eventManager;
protected $redirectDestination;
protected $event;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EventManagerInterface $event_manager, RedirectDestinationInterface $redirect_destination) {
parent::__construct($entity_type, $storage);
$this->eventManager = $event_manager;
$this->redirectDestination = $redirect_destination;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity.manager')
->getStorage($entity_type
->id()), $container
->get('rng.event_manager'), $container
->get('redirect.destination'));
}
public function render(EntityInterface $rng_event = NULL) {
if (isset($rng_event)) {
$this->event = $rng_event;
}
$this
->messenger()
->addMessage($this
->t('This rule list is for advanced users. Take care when committing any actions from this page.'), 'warning');
$render = parent::render();
$render['table']['#empty'] = t('No rules found for this event.');
return $render;
}
public function load() {
if (isset($this->event)) {
return $this->eventManager
->getMeta($this->event)
->getRules(NULL, FALSE, NULL);
}
return parent::load();
}
public function getOperations(EntityInterface $entity) {
$operations = parent::getOperations($entity);
foreach ($operations as &$operation) {
$operation['query'] = $this->redirectDestination
->getAsArray();
}
return $operations;
}
public function buildHeader() {
$header['id'] = t('id');
$header['trigger'] = t('Trigger ID');
$header['conditions'] = t('Conditions');
$header['actions'] = t('Actions');
$header['status'] = t('Status');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row['id'] = $entity
->id();
$row['trigger'] = $entity
->getTriggerID();
$row['conditions']['data'] = [
'#theme' => 'links',
'#links' => [],
'#attributes' => [
'class' => [
'links',
'inline',
],
],
];
foreach ($entity
->getConditions() as $condition) {
$row['conditions']['data']['#links'][] = [
'title' => $this
->t('Edit', [
'@condition_id' => $condition
->id(),
'@condition' => $condition
->getPluginId(),
]),
'url' => $condition
->urlInfo('edit-form'),
'query' => $this->redirectDestination
->getAsArray(),
];
}
$row['actions']['data'] = [
'#theme' => 'links',
'#links' => [],
'#attributes' => [
'class' => [
'links',
'inline',
],
],
];
foreach ($entity
->getActions() as $action) {
$row['actions']['data']['#links'][] = [
'title' => $this
->t('Edit', [
'@action_id' => $action
->id(),
'@action' => $action
->getPluginId(),
]),
'url' => $action
->urlInfo('edit-form'),
'query' => $this->redirectDestination
->getAsArray(),
];
}
$row['status'] = $entity
->isActive() ? $this
->t('Active') : $this
->t('Inactive');
return $row + parent::buildRow($entity);
}
}