View source
<?php
namespace Drupal\entityqueue\Controller;
use Drupal\Core\Ajax\AjaxHelperTrait;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatch;
use Drupal\entityqueue\EntityQueueInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\entityqueue\EntitySubqueueInterface;
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\entityqueue\EntityQueueRepositoryInterface;
class EntityQueueUIController extends ControllerBase {
use AjaxHelperTrait;
protected $entityQueueRepository;
public function __construct(EntityQueueRepositoryInterface $entityqueue_respository) {
$this->entityQueueRepository = $entityqueue_respository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entityqueue.repository'));
}
public function subqueueList(EntityQueueInterface $entity_queue) {
$list_builder = $this
->entityTypeManager()
->getListBuilder('entity_subqueue');
$list_builder
->setQueueId($entity_queue
->id());
return $list_builder
->render();
}
public function subqueueListForEntity(RouteMatchInterface $route_match, $entity_type_id = NULL, EntityInterface $entity = NULL) {
if (!$entity) {
$entity = $route_match
->getParameter($entity_type_id);
}
$queues = $this->entityQueueRepository
->getAvailableQueuesForEntity($entity);
$subqueues = $this
->entityTypeManager()
->getStorage('entity_subqueue')
->loadByProperties([
'queue' => array_keys($queues),
]);
$list_builder = $this
->entityTypeManager()
->getListBuilder('entity_subqueue');
$build['#title'] = $this
->t('Entityqueues for %title', [
'%title' => $entity
->label(),
]);
$build['#type'] = 'container';
$build['#attributes']['id'] = 'entity-subqueue-list';
$build['#attached']['library'][] = 'core/drupal.ajax';
$build['table'] = [
'#type' => 'table',
'#header' => $list_builder
->buildHeader(),
'#rows' => [],
'#cache' => [],
'#empty' => $this
->t('There are no queues available.'),
];
foreach ($subqueues as $subqueue_id => $subqueue) {
$row = $list_builder
->buildRow($subqueue);
$subqueue_items = $subqueue
->get('items')
->getValue();
if (in_array($entity
->id(), array_column($subqueue_items, 'target_id'), TRUE)) {
$row['operations']['data']['#links'] = [
'remove-item' => [
'title' => $this
->t('Remove from queue'),
'url' => Url::fromRoute('entity.entity_subqueue.remove_item', [
'entity_queue' => $queues[$subqueue
->bundle()]
->id(),
'entity_subqueue' => $subqueue_id,
'entity' => $entity
->id(),
]),
'attributes' => [
'class' => [
'use-ajax',
],
],
],
];
}
else {
$row['operations']['data']['#links'] = [
'add-item' => [
'title' => $this
->t('Add to queue'),
'url' => Url::fromRoute('entity.entity_subqueue.add_item', [
'entity_queue' => $queues[$subqueue
->bundle()]
->id(),
'entity_subqueue' => $subqueue_id,
'entity' => $entity
->id(),
]),
'attributes' => [
'class' => [
'use-ajax',
],
],
],
];
}
$destination = Url::fromRoute("entity.{$entity_type_id}.entityqueue", [
$entity_type_id => $entity
->id(),
])
->toString();
$row['operations']['data']['#links']['edit-subqueue-items'] = [
'title' => $this
->t('Edit subqueue items'),
'url' => $subqueue
->toUrl('edit-form', [
'query' => [
'destination' => $destination,
],
]),
];
$build['table']['#rows'][$subqueue
->id()] = $row;
}
return $build;
}
public function addForm(EntityQueueInterface $entity_queue) {
$subqueue = $this
->entityTypeManager()
->getStorage('entity_subqueue')
->create([
'queue' => $entity_queue
->id(),
]);
return $this
->entityFormBuilder()
->getForm($subqueue);
}
public function ajaxOperation(EntityQueueInterface $entity_queue, $op, Request $request) {
$entity_queue
->{$op}()
->save();
if ($request->request
->get('js')) {
$list = $this
->entityTypeManager()
->getListBuilder('entity_queue')
->render();
$response = new AjaxResponse();
$response
->addCommand(new ReplaceCommand('#entity-queue-list', $list));
return $response;
}
return $this
->redirect('entity.entity_queue.collection');
}
public function subqueueAjaxOperation(EntitySubqueueInterface $entity_subqueue, $op, Request $request) {
$entity_id = $request
->get('entity');
$entity = $this
->entityTypeManager()
->getStorage($entity_subqueue
->getQueue()
->getTargetEntityTypeId())
->load($entity_id);
$entity_subqueue
->{$op}($entity);
$violations = $entity_subqueue
->validate();
if (count($violations) === 0) {
$entity_subqueue
->save();
}
if ($this
->isAjax()) {
$route_match = RouteMatch::createFromRequest($request);
$content = $this
->subqueueListForEntity($route_match, $entity
->getEntityTypeId(), $entity);
if (count($violations)) {
$content['errors'] = [
'#theme' => 'status_messages',
'#message_list' => [
'error' => [
$this
->t('The operation could not be performed for the following reasons:'),
],
],
'#status_headings' => [
'error' => $this
->t('Error message'),
],
'#weight' => -10,
];
foreach ($violations as $violation) {
$content['errors']['#message_list']['error'][] = $violation
->getMessage();
}
}
$response = new AjaxResponse();
$response
->addCommand(new ReplaceCommand('#entity-subqueue-list', $content));
return $response;
}
return $this
->redirect('<current>');
}
public function access(RouteMatchInterface $route_match, $entity_type_id = NULL) {
$entity = $route_match
->getParameter($entity_type_id);
if ($entity && $this->entityQueueRepository
->getAvailableQueuesForEntity($entity)) {
return AccessResult::allowed();
}
return AccessResult::forbidden();
}
}