Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 293434

Settings returned via ajax are not run through hook_js_alter()

$
0
0

Updated: Comment #0

Problem/Motivation

It is possible to change javascript settings via hook_js_alter(), but the settings are returned unaltered from ajax request.

For example:

function mymodule_js_alter(&$javascript) {
    $javascript['settings']['data'][1]['pathPrefix'] = 'whatever/';
}

Proposed resolution

Use the altered JS settings - if they are available.

Remaining tasks

* Create proper patch.

Patch:

diff --git a/docroot/includes/ajax.inc b/docroot/includes/ajax.inc
index ab0111c..dc0491e 100644
--- a/docroot/includes/ajax.inc
+++ b/docroot/includes/ajax.inc
@@ -267,7 +267,10 @@ function ajax_render($commands = array()) {
   // HTML in the page. We pass TRUE as the $skip_alter argument to prevent the
   // data from being altered again, as we already altered it above. Settings are
   // handled separately, afterwards.
+
+  $scripts = array();
   if (isset($items['js']['settings'])) {
+    $scripts['settings'] = $items['js']['settings'];
     unset($items['js']['settings']);
   }
   $styles = drupal_get_css($items['css'], TRUE);
@@ -289,7 +292,10 @@ function ajax_render($commands = array()) {
   }
 
   // Now add a command to merge changes and additions to Drupal.settings.
-  $scripts = drupal_add_js();
+  // Only reload the settings when its not yet set.
+  if (!isset($scripts['settings'])) {
+    $scripts = drupal_add_js();
+  }
   if (!empty($scripts['settings'])) {
     $settings = $scripts['settings'];
     array_unshift($commands, ajax_command_settings(call_user_func_array('array_merge_recursive', $settings['data']), TRUE));

Viewing all articles
Browse latest Browse all 293434

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>