Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By ahwebd on
I have a custom block with its twig template suggestion.
I want to add an html head link tag when this block is loaded.
ex: <link rel="import" href="test.html">
In the twig template I can use attach_library
to add css/js to , but I don't know how to add html imports in twig.
Is it possible to achieve this using mytheme_preprocess_block
function, or any other hook?
I can add the html head link tag in mytheme_page_attachments_alter
but I don't know how to limit this to only when my custom block is loaded.
Thank you.
Comments
I found a solution
What I was missing is using
\Drupal::service('renderer')->render()
I created an array with #attached key, then rendered it using the above method.
Then I was the calling the above function conditionally in
mytheme_preprocess_block(&variables)
by checking$variables['attributes']['id']