You are here

function javascript_libraries_page_attachments in JavaScript Libraries Manager 8

Implements hook_page_attachments(). Used to add the javascript files in the html head section.

Parameters

array $page:

File

./javascript_libraries.module, line 25
Toggle the inclusion of Drupal system libraries. Upload and reference custom libraries as well.

Code

function javascript_libraries_page_attachments(array &$page) {
  $external_lib = \Drupal::config('javascript_libraries.settings')
    ->get('javascript_libraries_custom_libraries');
  $externals = array();
  $count = 1;
  foreach ($external_lib as $key => $lib) {
    if ($lib['scope'] == 'header') {
      if ($lib['type'] == 'file') {
        $lib['uri'] = file_create_url($lib['uri']);
      }
      $description[$key] = [
        '#type' => 'html_tag',
        // The HTML tag to add, in this case a  tag.
        '#tag' => 'script',
        '#attributes' => array(
          'src' => $lib['uri'],
        ),
      ];
      $page['#attached']['html_head'][] = [
        $description[$key],
        'description' . $count++,
      ];
    }
  }
}