I created the following hook_alter_form() in announcement.module:
function announcement_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
$form['announcement_block_max_list_count'] = array(
'#type' => 'textfield',
'#title' => t('Maximum number of block announcements'),
'#default_value' => variable_get('announcement_block_max_list_count', 3),
'#description' => t('Maximum number of items in announcement block'),
'#required' => FALSE,
'#weight' => 0
);
$form['announcement_display_classification'] = array(
'#type' => 'checkbox',
'#title' => t('Display additional announcement classification'),
'#default_value' => variable_get('announcement_display_classification', 1),
'#description' =>
t('Insert the additional classification in the announcement modules'),
'#required' => FALSE,
'#weight' => 0
);
}
}
I can see that the two new form elements on the page activated
with admin/content/node_type/announcement are correctly displaying
the initial values.
However, the modified values in those elements are not saved,
although the node_example.module contains the comment
// Alter the node type's configuration form to add our setting. We don't
// need to worry about saving this value back to the variable, the form
// we're altering will do it for us.
Normally, the values provided in the form are saved by hook_submit().
I may have to add code to save the values changed in the new form elements.
What should I do in this case?
I will appreciate any help.
Toshi
Comments
...
Drupal does some magic: it saves this variable under the name 'announcement_block_max_list_count_nodetype' (so that every node-type has separate settings). Your variable_get() must include this suffix. See comment.module's hook_form_alter.
hook_form_alter() Creates Extra Variables for Other Modules
Thank you for the useful information.
I changed the function as follows:
Variables announcement_block_max_list_count_announcement and
announcement_display_classification_announcement are now correctly stored and retrieved.
However, in addition to the two variables, extra variables are created for other modules as
How can I prevent these extra variables from being created.
The variables are needed only for the announcement node type.
Toshi
...
You don't. That's how Drupal operates. And these variables aren't yours; they are of other modules.
There's some work going on to make drupal save variables in the {variable} table only if they deviate from the default. That'd solve much of the problem.
That's a bug in Drupal. You should file an issue.