I'm not sure how many modules in contrib have done this, but I'm finding this a bit confusing. I'd like to have some active examples to easily demonstrate how to do this.
At the moment I'm looking at Search404. I've basically copied what you did for forums and set up a search404.variable.inc file which I then call from the search404.info with files[] = search404.variable.inc.
This loads, but I don't see any changes admin/config/search/search404
nor do I see them admin/config/regional/i18n/variable
From your docs, I'd assume it would be:
function search404_variable_info($options) {
$variables['search404_title'] = array(
'title' => t('Custom page title', array(), $options),
'description' => t('You can enter a value that will displayed at the title of the webpage e.g. "Page not found".'),
'type' => 'textfield',
'default' => t('Custom page title', array(), $options),
'group' => 'search404_settings'
);
$variables['search404_page_text'] = array(
'title' => t('Custom page text', array(), $options),
'description' => t('You can enter a custom text message that can be displayed at the top of the search results, HTML formatting can be used.'),
'type' => 'textarea',
'default' => t('Custom page text', array(), $options),
'group' => 'search404_settings'
);
$variables['search404_ignore_extensions'] = array(
'#type' => 'textfield',
'#title' => t('Extensions to ignore', array(), $options),
'#description' => t('These extensions will be ignored from the search query, e.g.: http://www.example.com/invalid/page.php will only search for "invalid page". Separate extensions with a space, e.g.: "htm html php". Do not include leading dot.'),
'#default_value' => t('Extensions to ignore', array(), $options),
'group' => 'search404_settings'
);
return $variables;
}
But logically it has to be tied more directly to the contrib module:
function search404_variable_info($options) {
$variables['advanced']['search404_page_title'] = array(
'title' => t('Custom page title', array(), $options),
'description' => t('You can enter a value that will displayed at the title of the webpage e.g. "Page not found".'),
'type' => 'textfield',
'default' => t('Custom page title', array(), $options),
'group' => 'search404'
);
$variables['advanced']['search404_page_text'] = array(
'title' => t('Custom page text', array(), $options),
'description' => t('You can enter a custom text message that can be displayed at the top of the search results, HTML formatting can be used.'),
'type' => 'textarea',
'default' => t('Custom page text', array(), $options),
'group' => 'search404'
);
$variables['advanced']['search404_ignore_extensions'] = array(
'#type' => 'textfield',
'#title' => t('Extensions to ignore', array(), $options),
'#description' => t('These extensions will be ignored from the search query, e.g.: http://www.example.com/invalid/page.php will only search for "invalid page". Separate extensions with a space, e.g.: "htm html php". Do not include leading dot.'),
'#default_value' => t('Extensions to ignore', array(), $options),
);
return $variables;
}Sadly neither of these approaches is working. Are there some good examples of modules (outside of Core & i18n) that are using the Variable module? I'm not sure what I'm missing.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | search404.variable.inc_2_.txt | 1.74 KB | mgifford |
| #3 | search404.variable.inc_.txt | 1.74 KB | mgifford |
Comments
Comment #1
jose reyero commentedVariable types are not the same as field types.
See system_variable_info() in includes/system.variable.inc for examples of this kind of variables.
Comment #2
mgiffordSo the function system_site_maintenance_mode() in modules/system/system.admin.inc defines the maintenance fields as:
In the Variable file includes/system.variable.inc the function system_variable_info() declares these two variables with:
Seems that so much of this is basically redundant information. It's close enough that it it really should be automated in some way.
I can see here where the type is defined but it isn't a complete list.
http://drupalcode.org/project/variable.git/blob/refs/heads/7.x-1.x:/vari...
Would be important to have a Drupal type to Variable type table as they aren't the same.
Also, the Variable file (in my case search404.variable.inc) should be put where?
Comment #3
mgiffordI find this very frustrating.. The only place this seems to be used in D7 is with Core. I want a simple set of examples so that I can have multi-lingual user variables expressed like I would have in D6 in the settings.php file http://evolvingweb.ca/story/drupal-7-multilingual-whats-new-i18n
I've got an example here that looks (as best as i can tell) like what I'm supposed to provide. I've enabled it in the search404.info file, went to produce a 404 page and still can't see the user defined variable here admin/config/regional/translate/translate
Nor does changing it in en/admin/config/search/search404 or fr/admin/config/search/search404 do anything but change it everywhere.
We need some simple examples as I don't think there is a good understanding of how to do this in D7 yet.
On a related issue #582044: Checkout messages not available as multi-lingual values
Comment #4
jose reyero commentedWell, for most variable definitions you just need to find a similar one in the include files for core, copy, paste and rename. Though I agree variable types could be documented better, so I'm creating a task for that.
Then for making them multilingual you need something else.
- First, variable types must be localizable (by default 'text' variables are localizable)
- Then you need to mark them as multilingual in Configuration/Regional and Language/Multilingual settings/Variables
About redundant information, that is true. However, the 'variable' module can produce your variable forms automatically (that is why we need variable title and description there). Since you have defined a 'group' you can produce the full settings form with
So at the expense of adding a hard dependency on variable module you can save your settings forms. We cannot do that for core though, so these need to be duplicated in their settings form and in variable definitions.
Comment #5
mgiffordThey are presently all listed as type text in the example I provided above (so by default it should be localizable):
But it doesn't appear in the list of variables - admin/config/regional/i18n/variable
How does the 'variable' module can produce your variable forms automatically?
I'll look more at the results from the form element:
http://api.drupal.org/api/drupal/includes!form.inc/function/drupal_get_f...
Comment #6
mgiffordSmall change in the attachment above.
Comment #7
mgiffordHow are the
"_[]"style variables being built here. This could use additional documentation.Looking at the last two, it looks like I should be able to leverage the advanced search using something like
$variables['advanced']['search404_page_text']Sadly I'm still not able to get it working. I can get the group appearing admin/config/system/variable/module with:
print_rdrupal_get_form('variable_group_form', 'search404'));But the group is empty other than a Default empty link.
Comment #8
jose reyero commentedThere's a 'Variable Example' module.
Comment #9
mgiffordOk, perhaps we should start with. Should I be using the stable release from August (7.x-1.1) or the dev release from earlier this month (7.x-1.x-dev)? I just checked out the git & dev version & the example module is there. That's useful.
I can also now see & enable the variables en/admin/config/regional/i18n/variable
Custom page title
Custom page text
Words to ignore
I can also edit them here:
fr/admin/config/system/variable/group/search404_i18n
But it just doesn't save.
The variables I'm trying to edit are in an 'Advanced' fieldset and as I've been trying to illustrate, they are in the form in the search404 module:
$variables['advanced']['search404_page_title']
When I use this, I get nothing. However, the partial results I'm getting are coming by dropping the fieldset to:
$variables['search404_page_title']
If I use the shorter one then I see the results I'd expect. Your example doesn't address this concern with the fieldset.
I can try to play a bit more with the example to produce a patch that at least demonstrates the bug in a clearer way.
Comment #10
mgiffordJust looking at the example... Generally, I think it would be more useful if it replicated the type of environment folks are looking to emulate. For instance in my case it's:
function search404_settings() {
But really this could be a good example for anyone. You need to be able to see the form you want to edit. See options for fields that you can edit & can't. For text, checkboxes/radios & fieldsets. Maybe other options too. Maybe even file uploads.
I can see the variables here are assigned to be multi-lingual:
en/admin/config/system/variable_example
Other than the title, both of these look the same:
en/variable/realm/site_name/site_name
en/variable/example
Even with the git example though I'm not able to save English & French variables.
Comment #11
jose reyero commentedThe dev release has just become stable, I didn't realize the example module was only there...
For i18n to automatically find and handle variables in forms you cannot use complex fieldsets, they must be plain settings forms. Also you may need latest version of i18n (just released too) to work with latest variable module.
Variables for file uploads are coming, possibly in next version.
Comment #12
mgiffordWhat about variables for variables within fieldsets? Is it just something that can't won't be translated with Variables, or just something that can't be for the moment?
I was looking for ways to explicitly tell i18n that a complex fieldset value needed translation. With all of the information stored in vertical tabs these days it seems like that's pretty critical.
I'm also not sure now if it's a matter of getting module developers to declare their variables or if it's more an issue of getting the Variables module to work with fieldsets? Anyways, thanks for helping to push this along. It's important for a lot of sites which need to be fully bilingual.
Comment #13
jose reyero commentedThe module is recursively traversing all fieldsets in the form. So If you handle your module's variables with system_settings_form() they will be handled even if you are using vertical tabs...
So maybe the form is not handled through system_settings? Or maybe has some other condition for this not to work?
Could you please print a dump of the form so we can see what happens?
Comment #14
mgiffordDid you want a dump of the form or to replace variable_form_alter() from git with the code in #13?
I just put in dsm()'s at the beginning of the form for $form, $form_state & $form_id, but not sure this is what you are looking for.
The "Page not found" text I was looking for wasn't found. The output of the $form variable didn't have the title. It's been too long since I looked at this code.
Key challenge though remains. For something like Search404, how do you get the Page Not Found title (which is customizable by the user) to show up in the right language.
Comment #15
jose reyero commentedWe need a dump of the form to see why variable translation does not kick in. But forget about the code I've posted in #13 because that has been reworked in variable 2.x
Anyway, seeing your last questions, it looks like you haven't properly set up your variables for translation.
> Key challenge though remains. For something like Search404, how do you get the Page Not Found title (which is customizable by the user) to show up in the right language.
This is not Variable module, see about Variable translation in i18n handbook.
Comment #16
jose reyero commentedI think docs and examples are enough at this point. Let me know if you are missing anything else.
Comment #17
ofry commentedHow I can do custom validation of my variables in hook_variable_info()
or custom types described in hook_variable_type_info() ?
Comment #18
nevets commentedThis thread is 2 years old, please mark this one as "Closed(fixed)" again and start a new tropic.
Comment #19
ofry commentedWhy I should mark support request as closed(fixed)? This topic was useful for me, but still lack of documentation with validation process...
I think all support for declare variables should be in one place.
Comment #20
ofry commentedComment #21
ofry commentedHuh, I understand.
Validation provided by
'validate callback' => 'your_validation_function',
Validation function should be like
Comment #22
ofry commentedBut next question.
I want to programmatically modify value on save.
For sample, I have list of emails:
"lala@la.la, rara@ra.ra, didi@di.di"
I want do something like:
but if I use format function, value still unchanged in edit window, in result of variable_get_value function.
How I can do this modification of my variables "on the fly" in save stage?
Comment #23
ofry commentedPlease provide more documentation about variable_format_value() and variable_get_value() difference, usage samples. And create more docs pages with link from project page.
Comment #24
jose reyero commented@ofry,
Please do not reopen old issues, open a new one.