You are here

public function AnalyticsController::ajaxOperation in Analytics 8

Calls a method on a view and reloads the listing page.

Parameters

\Drupal\analytics\Entity\AnalyticsServiceInterface $analytics_service: The analytics service being acted on.

string $op: The operation to perform, e.g., 'enable' or 'disable'.

\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 listing page.

1 string reference to 'AnalyticsController::ajaxOperation'
analytics.routing.yml in ./analytics.routing.yml
analytics.routing.yml

File

src/Controller/AnalyticsController.php, line 30

Class

AnalyticsController
Returns responses for Views UI routes.

Namespace

Drupal\analytics\Controller

Code

public function ajaxOperation(AnalyticsServiceInterface $analytics_service, $op, Request $request) {

  // Perform the operation.
  $analytics_service
    ->{$op}()
    ->save();

  // Display a message and log the action.
  if ($op === 'enable') {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Enabled analytics service @label.', [
      '@label' => $analytics_service
        ->label(),
    ]));
    $this
      ->getLogger('analytics')
      ->info('Enabled analytics service @label.', [
      '@label' => $analytics_service
        ->label(),
    ]);
  }
  elseif ($op === 'disable') {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Disabled analytics service @label.', [
      '@label' => $analytics_service
        ->label(),
    ]));
    $this
      ->getLogger('analytics')
      ->info('Disabled analytics service @label.', [
      '@label' => $analytics_service
        ->label(),
    ]);
  }

  // If the request is via AJAX, return the rendered list as JSON.
  if ($request->request
    ->get('js')) {
    $list = $this
      ->entityTypeManager()
      ->getListBuilder('analytics_service')
      ->render();
    $response = new AjaxResponse();
    $response
      ->addCommand(new ReplaceCommand('#analytics-entity-list', $list));
    return $response;
  }

  // Otherwise, redirect back to the page.
  return $this
    ->redirect('entity.analytics_service.collection');
}