You are here

public function EntityQueueUIController::subqueueAjaxOperation in Entityqueue 8

Calls a method on an entity subqueue page and reloads the page.

Parameters

\Drupal\entityqueue\EntitySubqueueInterface $entity_subqueue: The subqueue being acted upon.

string $op: The operation to perform, e.g., 'addItem' or 'removeItem'.

\Symfony\Component\HttpFoundation\Request $request: The current request.

Return value

\Drupal\Core\Ajax\AjaxResponse|\Symfony\Component\HttpFoundation\RedirectResponse Either returns a rebuilt listing page as an AJAX response, or redirects back to the current page.

1 string reference to 'EntityQueueUIController::subqueueAjaxOperation'
entityqueue.routing.yml in ./entityqueue.routing.yml
entityqueue.routing.yml

File

src/Controller/EntityQueueUIController.php, line 209

Class

EntityQueueUIController
Returns responses for Entityqueue UI routes.

Namespace

Drupal\entityqueue\Controller

Code

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);

  // Perform the operation.
  $entity_subqueue
    ->{$op}($entity);

  // Run validation.
  $violations = $entity_subqueue
    ->validate();

  // Save subqueue.
  if (count($violations) === 0) {
    $entity_subqueue
      ->save();
  }

  // If the request is via AJAX, return the rendered list as JSON.
  if ($this
    ->isAjax()) {
    $route_match = RouteMatch::createFromRequest($request);
    $content = $this
      ->subqueueListForEntity($route_match, $entity
      ->getEntityTypeId(), $entity);

    // Also display the validation errors if there are any.
    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;
  }

  // Otherwise, redirect back to the page.
  return $this
    ->redirect('<current>');
}