You are here

function acquia_lift_toolbar in Acquia Lift Connector 8.4

Same name and namespace in other branches
  1. 8.3 acquia_lift.module \acquia_lift_toolbar()

Implements hook_toolbar().

File

./acquia_lift.module, line 49

Code

function acquia_lift_toolbar() {
  $user = \Drupal::currentUser();

  // show link only when path context agrees on attaching.

  /** @var \Drupal\acquia_lift\Service\Context\PathContext $path_context */
  $path_context = \Drupal::service('acquia_lift.service.context.path_context');
  $items = [];

  // We are varying our cache by path and by permission.
  $items['acquia_lift'] = [
    '#cache' => [
      'keys' => [
        'toolbar',
      ],
      'contexts' => [
        'user.permissions',
        'url.path',
      ],
    ],
  ];

  // Clear the cache of this render array if the output of the $path_context
  // cache properties change.
  \Drupal::service('renderer')
    ->addCacheableDependency($items['acquia_lift'], $path_context);
  if ($user
    ->hasPermission('access acquia lift links') && $path_context
    ->shouldAttach()) {
    $items['acquia_lift'] += [
      '#type' => 'toolbar_item',
      'tab' => [
        '#type' => 'html_tag',
        '#tag' => 'button',
        '#value' => t('Acquia Lift'),
        '#attributes' => [
          'title' => t('Acquia Lift'),
          'class' => [
            'toolbar-icon',
            'toolbar-icon-acquia-lift',
          ],
          // This particular id will make sure the JS behavior is attached to the
          // link.
          'id' => [
            'openLiftLink',
          ],
          'aria-pressed' => 'false',
        ],
      ],
      '#wrapper_attributes' => [
        'class' => [
          'acquia-lift-toolbar-tab',
        ],
      ],
      '#attached' => [
        'library' => [
          'acquia_lift/acquia-lift-toolbar-button',
        ],
      ],
    ];
  }
  return $items;
}