You are here

acquia_lift.module in Acquia Lift Connector 8.4

File

acquia_lift.module
View source
<?php

/**
 * Drupal Module: Acquia Lift
 *
 * Acquia Lift lets you track customers' behavior throughout their buying
 * journey — from anonymous visitor through to loyal customer. Acquia Lift
 * allows you to create a unified customer profile for each individual, based
 * on their interactions with your website. Using these profiles, Acquia Lift
 * adaptively segments customers in real-time, letting you deliver personalized
 * content that furthers your website visitors' engagement with your brand.
 */
use Drupal\Core\Access\AccessResult;

/**
 * Implements hook_help().
 */
function acquia_lift_help($route_name) {
  return \Drupal::service('acquia_lift.service.helper.help_message_helper')
    ->getMessage($route_name);
}

/**
 * Implements hook_page_attachments().
 */
function acquia_lift_page_attachments(array &$page) {

  // Create and attach settings and library only when path context agrees on attaching.
  $path_context = \Drupal::service('acquia_lift.service.context.path_context');
  if (!$path_context
    ->shouldAttach()) {
    return;
  }
  $page_context = \Drupal::service('acquia_lift.service.context.page_context');

  // Populate contexts.
  $path_context
    ->populate($page);
  $page_context
    ->populate($page);

  // Attach Toolbar CSS Adjustment to all pages if the toolbar is present. This
  // is because we want the toolbar to shrink along with the lift experience
  // tools.
  if (\Drupal::currentUser()
    ->hasPermission('access toolbar')) {
    $page['#attached']['library'][] = 'acquia_lift/acquia-lift-toolbar';
  }
}

/**
 * Implements hook_toolbar().
 */
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;
}