I have called the JavaScript source code, <script scr=""></script>, inside a block. And this code is working fine.

But I am not sure whether it's good practice or not.

If it's not a good practice, give me some suggestions for adding above code inside the block.

1 Answer 1


You should add javascript as a library, then attach it to your block in the build array or a preprocess function like this:

$block['#attached']['library'][] = 'mymodule/mymodule.search_filters';
$block['#attached']['drupalSettings']['mymodule'] = [
  'cats' => 'tabby',
  'dogs' => 'dalmation',

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.