Site variables
Component ID
Component name
Component type
Maintenance status
Development status
Component security advisory coverage
Component created
Component changed
Component body
The Site variables module allows developers to create a settings menu and page from which users with proper permissions can edit variables used on the site, in a very simple way.
Site variables provides a hook to define which variables should be editable.
Installation
- Install the module as usual
- Change the basic settings on '/admin/settings/site-variables'
- Implement hook_site_variables
Simplest usage
This code, placed in your custom module "YOURMODULE" would create a menu and page with a form to edit the variable "YOURMODULE_custom_var" that you can then use with variable_get() in other parts of your module.
/**
* Implementation of hook_site_variables
*/
function YOURMODULE_site_variables() {
$variables = array();
$variables['YOURMODULE_custom_var'] = array();
return $variables;
}
Basic usage
This example makes use of the Form API to add more details to the edit variables form.
It also uses the attribute group, which groups the variables in different pages. This attribute would also provide a new permission, 'edit homepage site variables', that you can configure for each role in your site.
/**
* Implementation of hook_site_variables
*/
function YOURMODULE_site_variables() {
$variables = array();
$variables['YOURMODULE_custom_text'] = array(
//You can use Form API attributes like #title, #type, #description, etc
'#title' => t('Custom text'),
'#type' => 'textarea',
'#description' => t('This is a custom text'),
);
$variables['YOURMODULE_homepage_intro_text'] = array(
'#title' => t('Intro'),
'#type' => 'textarea',
//This will show all the variables related to 'homepage' together.
'group' => t('Homepage'),
);
$variables['YOURMODULE_homepage_number_of_news'] = array(
'#title' => t('Number of news'),
'#description' => t('Number of news to show on the homepage'),
'#default_value' => 5,
'group' => t('Homepage'),
);
return $variables;
}
