diff options
author | Steven Wittens <steven@10.no-reply.drupal.org> | 2007-06-22 08:46:16 +0000 |
---|---|---|
committer | Steven Wittens <steven@10.no-reply.drupal.org> | 2007-06-22 08:46:16 +0000 |
commit | 569ede574f2db14d7359b42e1ccbf5772a4554fb (patch) | |
tree | fadd967fb5610aacf078b562a29f5728f23a710d | |
parent | fe8b35f52778b63b96c969da27d2929beee8c1b9 (diff) | |
download | brdo-569ede574f2db14d7359b42e1ccbf5772a4554fb.tar.gz brdo-569ede574f2db14d7359b42e1ccbf5772a4554fb.tar.bz2 |
#115139: Add centralized json handler (and send text/javascript content-type header).
-rw-r--r-- | includes/common.inc | 18 | ||||
-rw-r--r-- | modules/profile/profile.module | 7 | ||||
-rw-r--r-- | modules/taxonomy/taxonomy.module | 6 | ||||
-rw-r--r-- | modules/upload/upload.module | 3 | ||||
-rw-r--r-- | modules/user/user.module | 4 |
5 files changed, 29 insertions, 9 deletions
diff --git a/includes/common.inc b/includes/common.inc index 9b4264f49..c371d40d8 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -2123,6 +2123,24 @@ function drupal_to_js($var) { } /** + * Return data in JSON format. + * + * This function should be used for JavaScript callback functions returning + * data in JSON format. It sets the header for JavaScript output. + * + * @param $var + * (optional) If set, the variable will be converted to JSON and output. + */ +function drupal_json($var = NULL) { + // We are returning JavaScript, so tell the browser. + drupal_set_header('Content-Type: text/javascript; charset=utf-8'); + + if (isset($var)) { + echo drupal_to_js($var); + } +} + +/** * Wrapper around urlencode() which avoids Apache quirks. * * Should be used when placing arbitrary data in an URL. Note that Drupal paths diff --git a/modules/profile/profile.module b/modules/profile/profile.module index c973986af..87238db73 100644 --- a/modules/profile/profile.module +++ b/modules/profile/profile.module @@ -742,16 +742,15 @@ function profile_form_profile($edit, $user, $category, $register = FALSE) { * Callback to allow autocomplete of profile text fields. */ function profile_autocomplete($field, $string) { + $matches = array(); if (db_result(db_query("SELECT COUNT(*) FROM {profile_fields} WHERE fid = %d AND autocomplete = 1", $field))) { - $matches = array(); $result = db_query_range("SELECT value FROM {profile_values} WHERE fid = %d AND LOWER(value) LIKE LOWER('%s%%') GROUP BY value ORDER BY value ASC", $field, $string, 0, 10); while ($data = db_fetch_object($result)) { $matches[$data->value] = check_plain($data->value); } - - print drupal_to_js($matches); } - exit(); + + drupal_json($matches); } /** diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index 17e169f8c..adc722559 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -1506,12 +1506,12 @@ function taxonomy_autocomplete($vid, $string = '') { // Fetch last tag $last_string = trim(array_pop($array)); + $matches = array(); if ($last_string != '') { $result = db_query_range(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t WHERE t.vid = %d AND LOWER(t.name) LIKE LOWER('%%%s%%')", 't', 'tid'), $vid, $last_string, 0, 10); $prefix = count($array) ? implode(', ', $array) .', ' : ''; - $matches = array(); while ($tag = db_fetch_object($result)) { $n = $tag->name; // Commas and quotes in terms are special cases, so encode 'em. @@ -1520,9 +1520,9 @@ function taxonomy_autocomplete($vid, $string = '') { } $matches[$prefix . $n] = check_plain($tag->name); } - print drupal_to_js($matches); - exit(); } + + drupal_json($matches); } /** diff --git a/modules/upload/upload.module b/modules/upload/upload.module index 460d4726a..32e337752 100644 --- a/modules/upload/upload.module +++ b/modules/upload/upload.module @@ -741,7 +741,10 @@ function upload_js() { // @todo: Put status messages inside wrapper, instead of above so they do not // persist across ajax reloads. $output = theme('status_messages') . drupal_render($form); + // We send the updated file attachments form. + // Don't call drupal_json(). upload.js uses an iframe and + // the header output by drupal_json() causes problems in some browsers. print drupal_to_js(array('status' => TRUE, 'data' => $output)); exit; } diff --git a/modules/user/user.module b/modules/user/user.module index 9d90d30ad..43a1fa07c 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -2827,8 +2827,8 @@ function user_autocomplete($string = '') { $matches[$user->name] = check_plain($user->name); } } - print drupal_to_js($matches); - exit(); + + drupal_json($matches); } /** |