| 5 common.inc | drupal_to_js($var) |
| 6 common.inc | drupal_to_js($var) |
Converts a PHP variable into its Javascript equivalent.
We use HTML-safe strings, i.e. with <, > and & escaped.
File
- includes/
common.inc, line 2529 - Common functions that many Drupal modules will need to reference.
Code
<?php
function drupal_to_js($var) {
switch (gettype($var)) {
case 'boolean':
return $var ? 'true' : 'false'; // Lowercase necessary!
case 'integer':
case 'double':
return $var;
case 'resource':
case 'string':
return '"' . str_replace(array("\r", "\n", "<", ">", "&"),
array('\r', '\n', '\x3c', '\x3e', '\x26'),
addslashes($var)) . '"';
case 'array':
// Arrays in JSON can't be associative. If the array is empty or if it
// has sequential whole number keys starting with 0, it's not associative
// so we can go ahead and convert it as an array.
if (empty($var) || array_keys($var) === range(0, sizeof($var) - 1)) {
$output = array();
foreach ($var as $v) {
$output[] = drupal_to_js($v);
}
return '[ ' . implode(', ', $output) . ' ]';
}
// Otherwise, fall through to convert the array as an object.
case 'object':
$output = array();
foreach ($var as $k => $v) {
$output[] = drupal_to_js(strval($k)) . ': ' . drupal_to_js($v);
}
return '{ ' . implode(', ', $output) . ' }';
default:
return 'null';
}
}
?> Login or register to post comments
Comments
Drupal 7
The equivalent function in Drupal 7 is http://api.drupal.org/api/function/drupal_json_encode/7
See http://drupal.org/update/modules/6/7#rename-drupal-to-js for more information.
Druplication
Yay Druplication of PHP functions! json_encode() pretty much accomplishes the same thing, but apparently is missing a couple things that we could most definitely easily add to PHP itself.
json_encode() was introduced
json_encode() was introduced only in PHP 5.2, Drupal 7 makes use of it
Patch
I have experienced problems with the D6 implementation of json_encode.
If you have json_encode at your disposal (really, who doesn't?) then this patch might be of interest. Code pulled from D7 drupal_json_encode:
http://pastie.org/2350717
Breaks in jQuery 1.4 AJAX
Note that this function breaks valid json, as it does not properly double escape HTML entities. That will generally not matter until you try to use it with jQuery 1.4. This is fixed in the d7 version.
Ouch
Yes yes, this kept me running in circles for too many hours today. This is really painful.
Found fix while repairing broken block views
I've had to implement the fix below in several sites where views as blocks use ajax paging (with and without panels). It seems to be a stable fix. Should or has an issue been reported? I'm aware that it's dependant on the version of jquery being used (drupal ist still at 1.3.x?) I almost always seem to need to use 1.6 or newer!
http://drupal.org/node/914360#comment-3468550