You are here

public function AjaxExampleController::ajaxLinkCallback in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/ajax_example/src/Controller/AjaxExampleController.php \Drupal\ajax_example\Controller\AjaxExampleController::ajaxLinkCallback()

Callback for link example.

Takes different logic paths based on whether Javascript was enabled. If $type == 'ajax', it tells this function that ajax.js has rewritten the URL and thus we are doing an AJAX and can return an array of commands.

Parameters

string $nojs: Either 'ajax' or 'nojs. Type is simply the normal URL argument to this URL.

Return value

string|array If $type == 'ajax', returns an array of AJAX Commands. Otherwise, just returns the content, which will end up being a page.

1 string reference to 'AjaxExampleController::ajaxLinkCallback'
ajax_example.routing.yml in ajax_example/ajax_example.routing.yml
ajax_example/ajax_example.routing.yml

File

ajax_example/src/Controller/AjaxExampleController.php, line 95

Class

AjaxExampleController
Controller routines for AJAX example routes.

Namespace

Drupal\ajax_example\Controller

Code

public function ajaxLinkCallback($nojs = 'nojs') {

  // Determine whether the request is coming from AJAX or not.
  if ($nojs == 'ajax') {
    $output = $this
      ->t("This is some content delivered via AJAX");
    $response = new AjaxResponse();
    $response
      ->addCommand(new AppendCommand('#ajax-example-destination-div', $output));

    // See ajax_example_advanced.inc for more details on the available
    // commands and how to use them.
    // $page = array('#type' => 'ajax', '#commands' => $commands);
    // ajax_deliver($response);
    return $response;
  }
  $response = new Response($this
    ->t("This is some content delivered via a page load."));
  return $response;
}