You are here

function sitewide_js_page_attachments in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/custom/sitewide_js/sitewide_js.module \sitewide_js_page_attachments()
  2. 8 modules/custom/sitewide_js/sitewide_js.module \sitewide_js_page_attachments()
  3. 8.2 modules/custom/sitewide_js/sitewide_js.module \sitewide_js_page_attachments()
  4. 8.3 modules/custom/sitewide_js/sitewide_js.module \sitewide_js_page_attachments()
  5. 8.4 modules/custom/sitewide_js/sitewide_js.module \sitewide_js_page_attachments()
  6. 8.5 modules/custom/sitewide_js/sitewide_js.module \sitewide_js_page_attachments()
  7. 8.6 modules/custom/sitewide_js/sitewide_js.module \sitewide_js_page_attachments()
  8. 8.7 modules/custom/sitewide_js/sitewide_js.module \sitewide_js_page_attachments()
  9. 8.8 modules/custom/sitewide_js/sitewide_js.module \sitewide_js_page_attachments()
  10. 10.3.x modules/custom/sitewide_js/sitewide_js.module \sitewide_js_page_attachments()
  11. 10.1.x modules/custom/sitewide_js/sitewide_js.module \sitewide_js_page_attachments()
  12. 10.2.x modules/custom/sitewide_js/sitewide_js.module \sitewide_js_page_attachments()

Implements hook_page_attachments().

File

modules/custom/sitewide_js/sitewide_js.module, line 11
The sitewide_js module file.

Code

function sitewide_js_page_attachments(array &$attachments) {

  // Get settings.
  $config = Drupal::config('sitewide_js.settings');

  // Check if it's enabled.
  if ($config
    ->get('swjs_enabled') === 1) {

    // Check if it should be placed in the header.
    if ($config
      ->get('swjs_location') === '0') {

      // Attach the JS.
      $attachments['#attached']['html_head'][] = [
        [
          '#type' => 'inline_template',
          '#template' => '{{ js|raw }}',
          '#context' => [
            'js' => '<script>' . $config
              ->get('swjs_javascript') . '</script>',
          ],
        ],
        'sitewide_js',
      ];
    }
  }
}