3

Is it possible without touching Dupal core's code, add some configuration option to content type edit page? preferably to vertical tabs of bottom? such as a checkbox.

I mean this page: /admin/structure/types/manage/article

3

Yes, you can add a fieldset to the content type edit form with hook_form_FORM_ID_alter.

e.g:

function MYMODULE_form_node_type_form_alter(&$form, &$form_state) {
    // Define field set in additional settings group
    $form['advanced'] = array(
      '#type' => 'fieldset',
      '#title' => t('Advanced settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 10,
      '#group' => 'additional_settings',
    );
    // Add checkbox option
    $form['advanced']['special_option'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use special option'),
    );
    // Set default value from automatically saved config variable
    $form['advanced']['special_option']['#default_value'] = variable_get('special_option_' . $form['#node']->type, 1);

}
  • I must be missing something, but my custom advanced option is not saved automatically in the {variable} table... Maybe I must implement some other hooks to save the custom option? – anou Mar 29 '15 at 11:30
  • 1
    I think $form['#node_type']->type is more $form['#node']->typeor $form['type']['#value'] or $form['#bundle']... – anou Mar 29 '15 at 13:32
1

Ok I found the answer: seems to be needed to add a submit function to your $form, to set the variable to store.

In the MYMODULE_form_node_form_alter() function, add:

$form['#submit'][] = 'MYMODULE_node_form_submit';

then the submit function:

function userprog_node_form_submit ($form, &$form_state) {
  $special = $form_state['values']['special_option'];

  variable_set('MYMODULE_' . $form_state['values']['type'] . '_special_option', $special);
}

NB: the variable_get() in the hook_form_alter() function, becomes:

// Set default value from automatically saved config variable
$form['advanced']['special_option']['#default_value'] = variable_get('MYMODULE_' . $form['#node_type']->type . '_special_option', 1);
0

You could also have a look at the nodeapi_example module. (From the Drupal Example modules).

It gives a quite legible example on adding a setting to the content type edit page AND an extra 'setting'/option to every node for which it is enabled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.