Language object * - default => Default value if not set */ function variable_get_value($variable, $options = array()) { $variable = _variable_variable($variable, $options); if (isset($variable['value'])) { return $variable['value']; } elseif (!empty($variable['value callback'])) { return variable_callback($variable['value callback'], $variable, _variable_options($options)); } else { $value = variable_get($variable['name'], NULL); if (isset($value)) { return $value; } else { return isset($options['default']) ? $options['default'] : variable_get_default($variable, $options); } } } /** * Set variable value * * This basically sets the variable but also invokes hook_variable_update */ function variable_set_value($name, $value, $options = array()) { $old_value = variable_get($name, NULL); variable_set($name, $value); module_invoke_all('variable_update', $name, $value, $old_value); } /** * Get variable default * * @param $variable * Variable name or variable information */ function variable_get_default($variable, $options = array()) { $variable = _variable_variable($variable, $options); if (isset($variable['default callback'])) { variable_include($variable); return call_user_func($variable['default callback'], $variable, _variable_options($options)); } elseif (isset($variable['default'])) { return $variable['default']; } return NULL; } /** * Delete variable (included children variables) */ function variable_delete($variable) { $variable = _variable_variable($variable); variable_include(); // We need to build the variable, it may be multiple $variable = variable_build($variable); if (!empty($variable['children'])) { array_map('variable_del', array_keys($variable['children'])); } else { variable_del($variable['name']); } // Invoke the hook only once even if its multiple module_invoke_all('variable_delete', $variable); } /** * Provide list of variable titles * * @param $names * List of variable names or full variable arrays * @return array() * List of name => title */ function variable_list($names) { $list = array(); foreach ($names as $name) { if ($variable = _variable_variable($name)) { $list[$variable['name']] = $variable['title']; } } return $list; } /** * @} End of "defgroup variable_api". */ /** * Build full variable data */ function variable_build($variable, $options = array()) { variable_include(); $variable = _variable_variable($variable, $options); return variable_build_variable($variable, $options); } /** * Clear cache */ function variable_cache_clear() { cache_clear_all('*', 'cache_variable', TRUE); } /** * Implementation of hook_flush_caches() */ function variable_flush_caches() { return array('cache_variable'); } /** * Implements hook_element_info() */ function variable_element_info() { // A fieldset with variable list $types['variable_fieldset'] = array( '#collapsible' => FALSE, '#collapsed' => FALSE, '#value' => NULL, '#process' => array('variable_element_process_fieldset', 'form_process_fieldset', 'ajax_process_form'), '#pre_render' => array('form_pre_render_fieldset'), '#theme_wrappers' => array('fieldset'), '#variable_list' => array(), ); return $types; } /** * Process variable fieldset */ function variable_element_process_fieldset($element) { $element += variable_edit_subform($element['#variable_list']); return $element; } /** * Implements hook_hook_info(). */ function variable_hook_info() { $hooks['variable_info'] = array( 'group' => 'variable', ); $hooks['variable_group_info'] = array( 'group' => 'variable', ); $hooks['variable_type_info'] = array( 'group' => 'variable', ); return $hooks; } /** * Form for variable list * * @param $list * Variable name or list of variable names */ function variable_edit_form($form, $form_state, $list) { $form += variable_edit_subform($list); return system_settings_form($form); } /** * Form elements for variable list */ function variable_edit_subform($list) { module_load_include('form.inc', 'variable'); $list = is_array($list) ? $list : array($list); $form = array(); foreach ($list as $name) { if ($variable = variable_get_info($name)) { $form[$name] = variable_form_element($variable); } } return $form; } /** * Include extended API and files related to specific variable * * @param $variable * Variable array */ function variable_include($variable = NULL) { static $included; if (!isset($included)) { // As a first step, include main variable API module_load_include('inc', 'variable'); $included = array(); } if ($variable && !isset($included[$variable['name']])) { // Include module.variable.inc files related to the variable and the variable type. variable_module_include($variable['module']); variable_type_include($variable['type']); $included[$variable['name']] = TRUE; } } /** * Include variable type files */ function variable_type_include($type) { variable_include(); $info = variable_get_type($type); variable_module_include($info['module']); // Include subtype if any if (!empty($info['type'])) { variable_type_include($info['type']); } } /** * Form for module variables */ function variable_module_form($form, $form_state, $module) { variable_include(); return variable_edit_form($form, $form_state, array_keys(variable_list_module($module))); } /** * Form for group variables */ function variable_group_form($form, $form_state, $group) { variable_include(); return variable_edit_form($form, $form_state, array_keys(variable_list_group($group))); } /** * Implements hook_modules_uninstalled(). */ function variable_modules_uninstalled($modules) { variable_include(); array_map('variable_module_uninstall', $modules); variable_cache_clear(); } /** * Implements hook_theme() */ function variable_theme($existing, $type, $theme, $path) { return array( 'variable_table_select' => array( 'render element' => 'element', 'file' => 'variable.form.inc', ), ); } /** * Get variable info static data, try the cache, or invoke the hook to collect it. * * @param $type * Name of the info to collect * - 'variable', Variable information, hook_variable_info() * - 'group', Group information, hook_variable_group_info() * - 'type', Type information, hook_variable_type_info() * @param $options * Options to retrieve or build the data. * The only used option to collect the data is 'langcode', though a full array of options may be used for the hooks */ function &variable_static($type, $options = array()) { static $data; $name = 'variable_' . $type; $langcode = isset($options['langcode']) ? $options['langcode'] : 'default'; if (!isset($data[$type])) { $data[$type] = &drupal_static($name); } if (!isset($data[$type][$langcode])) { $cache_id = $type . ':' . $langcode; if ($cache = cache_get($cache_id, 'cache_variable')) { $data[$type][$langcode] = $cache->data; } else { variable_include(); $data[$type][$langcode] = variable_build_info($type, $options); cache_set($cache_id, $data[$type][$langcode], 'cache_variable'); } } // Return a reference inside the big array return $data[$type][$langcode]; } /** * Get data from a variable module info array. */ function _variable_info($type, $name = NULL, $options = array()) { $info = &variable_static($type, $options); if ($name) { return isset($info[$name]) ? $info[$name] : array(); } else { return $info; } } /** * Get global language object. * * Initialize the language system if needed as we absolutely need a language here */ function _variable_language() { global $language; if (!isset($language)) { drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE); } return $language; } /** * Normalize variable options * * Will fill the following values if not present in the parameters * - langcode, Language code * - language, Language object */ function _variable_options($options = array()) { if (!empty($options['language'])) { $options['langcode'] = $options['language']->language; } elseif (!empty($options['langcode']) && ($list = language_list()) && isset($list[$options['langcode']])) { $options['language'] = $list[$options['langcode']]; } else { $language = _variable_language(); $options['language'] = $language; $options['langcode'] = $language->language; } return $options; } /** * Normalize variable data * * @param $variable * Variable name or variable info array * @return array * Variable information */ function _variable_variable($variable, $options = array()) { if (is_array($variable)) { return $variable; } elseif ($info = variable_get_info($variable, $options)) { return $info; } else { // We don't have meta-information about this variable. return _variable_unknown($variable); } } /** * Unknown variable */ function _variable_unknown($name) { return array( 'name' => $name, 'title' => t('Unknown variable') . ' ' . check_plain($name), 'type' => 'unknown', 'module' => 'variable', // Space for public service advertisement :-) 'description' => t('We have no meta information for this variable. Please, ask module developers to declare their variables. See Variable module.'), ); }