You are here

public function EntityQueueUIController::subqueueListForEntity in Entityqueue 8

Provides a list of subqueues where an entity can be added.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match.

string $entity_type_id: (optional) The entity type ID.

\Drupal\Core\Entity\EntityInterface $entity: (optional) An entity object.

Return value

array Array of page elements to render.

1 call to EntityQueueUIController::subqueueListForEntity()
EntityQueueUIController::subqueueAjaxOperation in src/Controller/EntityQueueUIController.php
Calls a method on an entity subqueue page and reloads the page.

File

src/Controller/EntityQueueUIController.php, line 83

Class

EntityQueueUIController
Returns responses for Entityqueue UI routes.

Namespace

Drupal\entityqueue\Controller

Code

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.'),
  ];

  /** @var \Drupal\entityqueue\EntitySubqueueInterface $subqueue */
  foreach ($subqueues as $subqueue_id => $subqueue) {
    $row = $list_builder
      ->buildRow($subqueue);

    // Check if entity is in queue.
    $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',
            ],
          ],
        ],
      ];
    }

    // Add an operation for editing the subqueue items.
    // First, compute the destination to send the user back to the
    // entityqueue tab they're currently on. We can't rely on <current>
    // since if any of the AJAX links are used and the page is rebuilt,
    // <current> will point to the most recent AJAX callback, not the
    // original entityqueue tab.
    $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;
}