function fac_generate_json_for_key in Fast Autocomplete 7
Generates the json file for a specific search key.
Parameters
string $key: The key to search for.
Return value
string The json string hat is saved or an empty string on failure.
2 calls to fac_generate_json_for_key()
- fac_bulk_generate_json in ./
fac.module - Bulk generate json files worker callack.
- fac_generate_json in inc/
fac.json.inc - Fast AutoComplete json callback.
File
- ./
fac.module, line 288 - This file contains the main functions of the Fast Autocomplete module.
Code
function fac_generate_json_for_key($language, $key) {
$json_result = '';
$original_user = $GLOBALS['user'];
// For security reasons we can only perform a search as an anonymous user.
// Otherwise the JSON files might expose information that should be private.
// If the risk is deemed acceptable this behavior can be overridden by
// setting the variable "fac_anonymous_search_only to FALSE and the search
// will be performed as the current user.
if (variable_get('fac_anonymous_search', TRUE)) {
// Prevent session information from being rendered.
drupal_save_session(FALSE);
// Force the current user to anonymous to prevent access bypass.
$GLOBALS['user'] = drupal_anonymous_user();
}
// Force the language url to the requested language to make sure the urls that
// are generated possibly contain the correct language path.
$original_language_url = $GLOBALS['language_url'];
$language_list = language_list();
$GLOBALS['language_url'] = $language_list[$language];
$backend_service = variable_get('fac_backend_service', '');
if (!empty($backend_service)) {
$service = new $backend_service();
$result = $service
->search($key, $language);
$items = array();
if (!empty($result['items'])) {
foreach ($result['items'] as $item_info) {
$entities = entity_load($item_info['entity_type'], array(
$item_info['etid'],
));
$entity = reset($entities);
$entity_views = entity_view($item_info['entity_type'], array(
$entity,
), variable_get('fac_view_mode', 'fac'), $language);
$entity_view = reset($entity_views);
$items[] = render($entity_view);
}
}
$object = new stdClass();
$object->items = $items;
$json_result = json_encode($object);
$directory = FAC_JSON_FILES_DIRECTORY . '/' . $language . '/' . _fac_get_role_hmac($GLOBALS['user']);
if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
$destination = $directory . '/' . $key . '.json';
file_unmanaged_save_data($json_result, $destination, FILE_EXISTS_REPLACE);
}
}
// Restore the language_url.
$GLOBALS['language_url'] = $original_language_url;
// Restore the original user.
$GLOBALS['user'] = $original_user;
drupal_save_session(TRUE);
return $json_result;
}