- Issue created by @debra-v
The JavaScript API documentation → says to use the hook—so that is more the official way than a workaround.
You said this no longer works. Which Drupal release changed the behavior?
- 🇮🇳India kulpratap2002
Right now, when you add #attached['drupalSettings'] in a block’s build() method, those settings don’t always make it into the global drupalSettings unless the block also attaches a library. This feels like a change in behavior compared to earlier versions.
It would be more intuitive if settings from a block were always passed through, even without a library. That way we can attach simple data for frontend scripts without having to create a library just for it.
As a temporary workaround, you can either attach a library in the block or move the settings into hook_page_attachments().
In which earlier Drupal versions did
#attached
work? It is important to have this information to understand why the behavior changed.- 🇺🇸United States debra-v
I just had an AHA moment. The earlier version where it worked was Drupal 10.2+. However, we made a significant change after that version to move to a new theme using SDC components. There is a library that is loaded with that block, but that library is loaded via SDC. **insert suspenseful sound effect**
- 🇺🇸United States debra-v
But wait... it appears libraries aren't being added to blocks either.
# My favorite library: my_custom_module.libraries.yml styles: css: component: css/styles.css: {}
... $build = [ '#theme' => 'my_custom_block', '#favorite_settings' => $favorite_settings, '#cache' => ['contexts' => ['url.path']], '#attached' => [ 'library' => [ 'my_custom_module/styles', ], ], ]; return $build; ...
The styles.css file doesn't load.