You are here

function siteimprove_page_attachments in Siteimprove 8

Implements hook_page_attachments().

File

./siteimprove.module, line 227
Drupal module: Siteimprove Plugin.

Code

function siteimprove_page_attachments(array &$attachments) {

  // Check if user has access.
  if (\Drupal::currentUser()
    ->hasPermission('use siteimprove')) {

    // Add Siteimprove js.
    $siteimprove = \Drupal::service('siteimprove.utils');
    $attachments['#attached']['drupalSettings']['siteimprove']['token'] = $siteimprove
      ->getSiteimproveToken();
    $attachments['#attached']['library'][] = $siteimprove
      ->getSiteimproveOverlayLibrary();
    $attachments['#attached']['library'][] = $siteimprove
      ->getSiteimproveLibrary();

    // Get the active frontend domain plugin.
    $config = \Drupal::service('config.factory')
      ->get('siteimprove.settings');

    // If node pages or taxonomy term pages, add input method, else domain method.
    $enabled_route_names = [
      'entity.node.canonical',
      'entity.node.latest_version',
      'entity.node.edit_form',
      'entity.taxonomy_term.canonical',
      'entity.taxonomy_term.latest_version',
      'entity.taxonomy_term.edit_form',
    ];
    $route_match = \Drupal::routeMatch();
    $current_route_name = $route_match
      ->getRouteName();
    if (in_array($current_route_name, $enabled_route_names)) {
      $method = 'input';
    }
    else {
      $method = 'domain';
    }
    $type = '';
    $bundle = '';
    $parameters = $route_match
      ->getParameters();
    foreach ($parameters as $parameter) {
      if (is_object($parameter) && $parameter instanceof \Drupal\node\Entity\Node) {
        $type = 'enabled_content_types';
        $bundle = $parameter
          ->bundle();
      }
      elseif (is_object($parameter) && $parameter instanceof \Drupal\taxonomy\Entity\Term) {
        $type = 'enabled_taxonomies';
        $bundle = $parameter
          ->bundle();
      }
    }
    $conf = $type ? $config
      ->get($type) : [];
    $enabled = $type && isset($conf[$bundle]) && $conf[$bundle];
    if (\Drupal::currentUser()
      ->hasPermission('use siteimprove prepublish') && $bundle && $enabled) {
      $attachments['#attached']['library'][] = 'siteimprove/siteimprove.prepublish';
    }
    if (!empty($method)) {
      $parameters = \Drupal::routeMatch()
        ->getParameters();
      foreach ($parameters as $param) {
        if (is_object($param) && $param instanceof \Drupal\Core\Entity\ContentEntityInterface) {
          if ($param->in_preview !== TRUE) {

            /** @var \Drupal\Core\Entity\ContentEntityType $type */
            $entity_type = $param
              ->getEntityType()
              ->id();
            $id = $param
              ->id();
            $entity = \drupal::entityTypeManager()
              ->getStorage($entity_type)
              ->load($id);
            $urls = \drupal::service('siteimprove.utils')
              ->getEntityUrls($entity);
            $attachments['#attached']['drupalSettings']['siteimprove'][$method] = $siteimprove
              ->getSiteimproveSettings($urls, $method);
          }
        }
      }
    }

    // If siteimprove_url exists in SESSION, send to Siteimprove.
    if (!empty($_SESSION['siteimprove_url'])) {
      $urls = $_SESSION['siteimprove_url'];
      $method = 'recheck';
      $attachments['#attached']['drupalSettings']['siteimprove'][$method] = $siteimprove
        ->getSiteimproveSettings($urls, $method);
      unset($_SESSION['siteimprove_url']);
    }
  }
}