0

What is the best approach to change/override the drupalsettings variable which is defined by some other module (basically core) through our custom module ? If I follow the same hooks then how to call it at very last in Drupal 8?

Here: by drupalsettings, I am asking about the Drupal behaviors variable.

The question is updated as suggested by @leymannx.

  • Were does the contrib module attach this variable? Did you try to implement a similar hook and set your own value the same way the contrib module did? What happened? Maybe you then need to ensure to have your implementation run last. Please update your question and provide more details. – leymannx Jun 23 at 20:59
  • Maybe it works similar to this: drupal.org/node/2974681. Attach your own JS from where you need it and have it set Drupal.settings.foobar = 'hello world' to override the foobar settings declared somewhere else before. – leymannx Jun 23 at 21:04
  • Thanks for putting your effort @leymannx, your comment is appreciable. I already did the same thing as I was continued debugging. And yes I am on the same page as you have asked, the issue is in the ordering of my module's hook. What will be the best approach to call it last ? some hook like hook_module_implements_alter or using the module modules_weight ? – Juyal Jee Jun 24 at 2:31
  • Yeah, hook_module_implements_alter. 👍 – leymannx Jun 24 at 7:16
  • 2
    This is usually the last resort and following the same hook is not specific to the topic drupalsettings. These can be defined in different ways. In hook_page_attachments, then there is a hook_page_attachments_alter, in render arrays, which can be changed like any other rendered content in various hooks of the render pipeline and finally for dynamic overrides there is hook_js_settings_alter. – 4k4 Jun 24 at 8:03
1

You can alter the drupalSettings with the hook "hook_js_settings_alter":

function hook_js_settings_alter(array &$settings, \Drupal\Core\Asset\AttachedAssetsInterface $assets) {

  // Add settings.
  $settings['user']['uid'] = \Drupal::currentUser();

  // Manipulate settings.
  if (isset($settings['dialog'])) {
    $settings['dialog']['autoResize'] = FALSE;
  }
}

For more information see: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/function/hook_js_settings_alter/8.9.x

I checked the code and the theme hook is run after the module hook. So if you really want to be last. Add the hook to your theme.

web/core/lib/Drupal/Core/Asset/AssetResolver.php:

$this->moduleHandler->alter('js_settings', $settings, $assets);
$this->themeManager->alter('js_settings', $settings, $assets);
| improve this answer | |
  • You could also use this hook to make sure your module is last: api.drupal.org/api/drupal/… – andeersg Jun 25 at 6:29
  • Useful findings @andeersg, btw I was looking the way to achieve it through module only. Would you also suggest to replace the existing assets(JS/CSS) with my custom one using the same hook ? or I should just append my custom js in the $assets->setLibraries()? – Juyal Jee Jun 30 at 18:12
  • If you want to alter/replace the assets I would recommend this hook: api.drupal.org/api/drupal/…, js_settings_alter is just for altering the settings. – andeersg Jul 6 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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