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.