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

mooffie’s picture

$form['announcement_block_max_list_count']

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.

minoura’s picture

Thank you for the useful information.

I changed the function as follows:

function announcement_form_alter(&$form, $form_state, $form_id) {

// echo "announcement_form_alter(&$form, $form_state, $form_id) called <br>\n";

  if ($form_id == 'node_type_form' &&   // for node_type_form
     // only for announcement
     $form['#node_type']->type == 'announcement' &&  
      isset($form['identity']['type'])) { //  may not be needed

// dprint_r($form);

    $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_announcement', 3),
      '#description'   => t('Maximum number of items in block'),
      '#required'      => FALSE, 
      '#weight'        => 0
    );

    $form['announcement_display_classification'] = array(
      '#type' => 'checkbox',
      '#title'         => t('Display announcement classification'),
      '#default_value' => 
 variable_get('announcement_display_classification_announcement', 1),
      '#description'   =>
        t('Insert the classification in the announcement modules'),
      '#required'      => FALSE, 
      '#weight'        => 0
    );
  }
}

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

select * from variable where name like '%announcement%';

| announcement_display_classification_announcement | i:1;
| comment_form_location_announcement               | s:1:"0";
| announcement_block_max_list_count_announcement   | s:2:"30";
| comment_subject_field_announcement               | s:1:"1";
| comment_preview_announcement                     | s:1:"1";
| comment_controls_announcement                    | s:1:"3";
| comment_anonymous_announcement                   | i:0;
| comment_default_per_page_announcement            | s:2:"50";
| comment_default_order_announcement               | s:1:"1";
| comment_default_mode_announcement                | s:1:"4";
| form_build_id_announcement                       | s:37:"form-d5383d0c1653760c25e871b5c4feed6e";
| comment_announcement                             | s:1:"2";
| upload_announcement                              | s:1:"1";
| nodeapi_example_announcement                     | s:1:"0";
| node_options_announcement  

How can I prevent these extra variables from being created.
The variables are needed only for the announcement node type.

Toshi

mooffie’s picture

How can I prevent these extra variables from being created.

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.

form_build_id_announcement

That's a bug in Drupal. You should file an issue.