ToolbarController.php 1.61 KB
Newer Older
1 2
<?php

3
namespace Drupal\toolbar\Controller;
4

5
use Drupal\Core\Access\AccessResult;
6
use Drupal\Core\Ajax\AjaxResponse;
7
use Drupal\Core\Controller\ControllerBase;
8
use Drupal\toolbar\Ajax\SetSubtreesCommand;
9 10 11 12

/**
 * Defines a controller for the toolbar module.
 */
13
class ToolbarController extends ControllerBase {
14 15

  /**
16
   * Returns an AJAX response to render the toolbar subtrees.
17
   *
18
   * @return \Drupal\Core\Ajax\AjaxResponse
19
   */
20
  public function subtreesAjax() {
21
    list($subtrees, $cacheability) = toolbar_get_rendered_subtrees();
22 23
    $response = new AjaxResponse();
    $response->addCommand(new SetSubtreesCommand($subtrees));
24 25 26 27 28 29 30 31 32 33 34 35 36

    // The Expires HTTP header is the heart of the client-side HTTP caching. The
    // additional server-side page cache only takes effect when the client
    // accesses the callback URL again (e.g., after clearing the browser cache
    // or when force-reloading a Drupal page).
    $max_age = 365 * 24 * 60 * 60;
    $response->setPrivate();
    $response->setMaxAge($max_age);

    $expires = new \DateTime();
    $expires->setTimestamp(REQUEST_TIME + $max_age);
    $response->setExpires($expires);

37 38 39
    return $response;
  }

40 41
  /**
   * Checks access for the subtree controller.
42
   *
43 44
   * @param string $hash
   *   The hash of the toolbar subtrees.
45
   *
46 47
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
48
   */
49
  public function checkSubTreeAccess($hash) {
50
    $expected_hash = _toolbar_get_subtrees_hash()[0];
51
    return AccessResult::allowedIf($this->currentUser()->hasPermission('access toolbar') && hash_equals($expected_hash, $hash))->cachePerPermissions();
52 53
  }

54
}