You are here

function advagg_replace_drupal_settings_string in Advanced CSS/JS Aggregation 7.2

Replace inline drupal settings script.

Parameters

string $subject: Inline js.

string $replace: JS settings replacement.

Return value

string Returns the subject with the replacement in place if this is a drupal settings json blob.

1 call to advagg_replace_drupal_settings_string()
_advagg_process_html in ./advagg.module
Replacement for template_process_html().

File

./advagg.module, line 2480
Advanced CSS/JS aggregation module.

Code

function advagg_replace_drupal_settings_string($subject, $replace) {
  $start = strpos($subject, 'jQuery.extend(Drupal.settings,');
  if ($start === FALSE) {
    return $subject;
  }

  // Find the end of the Drupal.settings.
  $script_end = stripos($subject, '</script>', $start);
  $settings_substring = substr($subject, $start, $script_end - $start);
  $json_end = strripos($settings_substring, '});');

  // Check if LABjs has added an additional wrapper around Drupal settings.
  $script_tag_start = strripos(substr($subject, 0, $start), '<script');
  if (strpos(substr($subject, $script_tag_start, $start), '$L.wait(') !== FALSE) {

    // Refine JSON end position.
    $_json_end = strripos(substr($settings_substring, 0, $json_end), '});');
    if ($_json_end !== FALSE) {
      $json_end = $_json_end;
    }
  }

  // Replace Drupal.settings json.
  $subject = substr($subject, 0, $start + 30) . $replace . substr($subject, $json_end + $start + 1);
  return $subject;
}