1

I have created a custom module and created a config page which we need to input the url on that page.

Created a twig template within my custom module:

<script src={{ js_file_url }}>test</script>

In my .module file

<?php

/**
 * @file
 * Add the url to the header section.
 */

/**
 * Implments hook_page_attachments_alter().
 */
function custom_module_page_attachments_alter(array &$attachments) {

  $js_file_url = \Drupal::config(' custom_module.settings')->get('url');

  $renderable = [
    '#theme' => 'my_template',
    '#test_var' => 'test variable',
  ];

$attachments['#attached']['html_head'][] = [
    [
      '#theme'  => 'my_template',
      '#js_file_url'   => $js_file_url,
    ],
  ];  

}


function  custom_module_theme($existing, $type, $theme, $path) {
  return [
    'my_template' => [
      'variables' => ['js_file_url' => NULL],
    ],
  ];
}

My output:

<><script src=http://google.com>test</script></>

Expected Output is

<script src=http://google.com>test</script>

How to get rid of empty tags => <>,

By default $attachments['#attached']['html_head'][] is adding the '#tag' even if i dont declare it.

Can someone help me, please.

3
  • 1
    This seems somewhat dangerous (user defined JS url), but drupal.org/project/drupal/issues/2752283
    – Kevin
    Jan 17, 2020 at 19:08
  • can we use #theme inside $attachments['#attached']['html_head'][] ..?
    – Drupal Dev
    Jan 17, 2020 at 19:59
  • I'm wondering if the Twig template makes any sense here at all.
    – leymannx
    Jan 17, 2020 at 21:50

1 Answer 1

1

You can attach custom tags to any render array from where it bubbles up into the HTML head. In the following sample I'm adding structured data when a certain block is viewed. $value then contains what's being printed between the opening and closing script tag.

/**
 * Implements hook_block_view_BASE_BLOCK_ID_alter().
 */
function MYMODULE_block_view_system_breadcrumb_block_alter(array &$build, BlockPluginInterface $block) {

  // Get JSON-LD.
  if ($json_ld = \Drupal::service('easy_breadcrumb.structured_data_json_ld')
    ->value()) {

    // Prepare script tag.
    $structured_data = [
      '#tag' => 'script',
      '#attributes' => ['type' => 'application/ld+json'],
      '#value' => $json_ld,
    ];

    // Add script tag.
    $build['#attached']['html_head'][] = [
      $structured_data,
      'easy_breadcrumb_structured_data_json_ld',
    ];
  }
}

I hope you get the idea how this can easily be adjusted to get your custom snippet into the head. You can keep your logic in hook_page_attachments_alter but then let it look more like in the following snippet. Maybe you don't even need any #value at all.

/**
 * Implements hook_page_attachments_alter().
 */
function MYMODULE_page_attachments_alter(array &$attachments) {

  if ($js_file_url = \Drupal::config('MYMODULE.settings')->get('url')) {

    // Prepare script tag.
    $my_script = [
      '#tag' => 'script',
      '#attributes' => ['src' => $js_file_url],
      '#value' => 'test',
    ];

    // Add script tag.
    $attachments['#attached']['html_head'][] = [
      $my_script,
      'MYMODULE_my_script',
    ];
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.