You are here

function acquia_lift_page_attachments in Acquia Lift Connector 8

Same name and namespace in other branches
  1. 8.4 acquia_lift.module \acquia_lift_page_attachments()
  2. 8.3 acquia_lift.module \acquia_lift_page_attachments()

Implements hook_page_attachments().

File

./acquia_lift.module, line 33
Drupal Module: Acquia Lift

Code

function acquia_lift_page_attachments(array &$attachments) {

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

  // Prepare credential drupal settings.
  $credential_settings = \Drupal::config('acquia_lift.settings')
    ->get('credential');
  $drupal_settings['credential'] = SettingsHelper::getFrontEndCredentialSettings($credential_settings);

  // Prepare page context.
  $page_context = \Drupal::service('acquia_lift.service.context.page_context');
  $request = \Drupal::request();

  // Set page context by node.
  if ($request->attributes
    ->has('node')) {
    $node = $request->attributes
      ->get('node');
    if ($node instanceof NodeInterface) {
      $page_context
        ->setByNode($node);
    }
  }

  // Set page context's title.
  $route_object = \Drupal::routeMatch()
    ->getRouteObject();
  $title_resolver = \Drupal::service('title_resolver');
  $title = $title_resolver
    ->getTitle($request, $route_object);
  $page_context
    ->setPageContextTitle($title);
  $drupal_settings['pageContext'] = $page_context
    ->getAll();

  // Attach identity settings.
  $identity = \Drupal::service('acquia_lift.service.context.path_context')
    ->getIdentity();
  if ($identity) {
    $drupal_settings['identity'] = $identity;
  }

  // Attach JavaScript library and settings.
  $attachments['#attached']['library'][] = 'acquia_lift/acquia_lift';
  $attachments['#attached']['drupalSettings']['acquia_lift'] = $drupal_settings;
}