You are here

function linkedin_insights_tag_page_attachments in LinkedIn Insights Tag 8

Implements hook_page_attachments().

Insert JavaScript to the appropriate scope/region of the page.

File

./linkedin_insights_tag.module, line 19
LinkedIn Insights Module..

Code

function linkedin_insights_tag_page_attachments(array &$page) {
  $config = \Drupal::config('linkedin_insights_tag.settings');
  $noscript = $config
    ->get('image_only');

  // Load linkedin_insights_tag library if noscript is not enforced.
  if (empty($noscript)) {
    $roles = $config
      ->get('user_role_roles');
    $user = \Drupal::currentUser();
    $current_user_roles = $user
      ->getRoles();

    // Make sure the visibility user roles are configured.
    if (!empty($roles) && !empty($current_user_roles)) {
      $intersect = array_intersect($roles, $current_user_roles);
      if (!empty($intersect)) {
        $page['#attached']['drupalSettings']['linkedin_insights_tag']['partner_id'] = $config
          ->get('partner_id');
        $page['#attached']['library'][] = 'linkedin_insights_tag/linkedin_insights_tag_variables';
        $page['#attached']['library'][] = 'linkedin_insights_tag/linkedin_insights_tag';
      }
    }
  }
}