diff --git a/core/includes/common.inc b/core/includes/common.inc index 5882f40..44d7e28 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -2722,10 +2722,7 @@ function drupal_process_attached($elements, $dependency_check = FALSE) { */ function drupal_process_states(&$elements) { $elements['#attached']['library'][] = array('system', 'drupal.states'); - $elements['#attached']['js'][] = array( - 'type' => 'setting', - 'data' => array('states' => array('#' . $elements['#id'] => $elements['#states'])), - ); + $elements['#attributes']['data-drupal-states'] = JSON::encode($elements['#states']); } /** @@ -3854,6 +3851,11 @@ function drupal_render(&$elements) { return ''; } + // Add any JavaScript state information associated with the element. + if (!empty($elements['#states'])) { + drupal_process_states($elements); + } + // Get the children of the element, sorted by weight. $children = element_children($elements, TRUE); @@ -3899,11 +3901,6 @@ function drupal_render(&$elements) { $elements['#children'] = $elements['#markup'] . $elements['#children']; } - // Add any JavaScript state information associated with the element. - if (!empty($elements['#states'])) { - drupal_process_states($elements); - } - // Add additional libraries, CSS, JavaScript an other custom // attached data associated with this element. if (!empty($elements['#attached'])) { diff --git a/core/misc/states.js b/core/misc/states.js index de6e6b8..d934ee5 100644 --- a/core/misc/states.js +++ b/core/misc/states.js @@ -18,17 +18,17 @@ var states = Drupal.states = { */ Drupal.behaviors.states = { attach: function (context, settings) { - var $context = $(context); - for (var selector in settings.states) { - if (settings.states.hasOwnProperty(selector)) { - for (var state in settings.states[selector]) { - if (settings.states[selector].hasOwnProperty(state)) { - new states.Dependent({ - element: $context.find(selector), - state: states.State.sanitize(state), - constraints: settings.states[selector][state] - }); - } + var $states = $(context).find('[data-drupal-states]'); + var config, state; + for (var i = 0, il = $states.length; i < il; i += 1) { + config = JSON.parse($states[i].getAttribute('data-drupal-states')); + for (state in config) { + if (config.hasOwnProperty(state)) { + new states.Dependent({ + element: $($states[i]), + state: states.State.sanitize(state), + constraints: config[state] + }); } } }