2

For a custom module i would love to add a checkbox to "BANDWIDTH OPTIMIZATION" under "admin/config/development/performance". I know how to add a new admin page but this is just one checkbox and i think it would fit more there.

Can anyone tell me how to do that?

2

2 Answers 2

3

To alter a form implemented by another module, you just implement hook_form_alter(). You probably need to change the #weight property of the form element you are adding, to avoid it is rendered in the wrong place.

function mymodule_form_system_performance_settings_alter(&$form, &$form_state) {
 // Add the checkbox to $form.
}
2

To add another item on the performance settings form you must implement the hook_for_alter or the hook_form_FORM_ID_alter.

To get the form id you just need to inspect the form an get the id attribute of the form tag:

enter image description here

In your custom module you will have the form alter function like this:

function mymodule_form_system_performance_settings_alter(&$form, &$form_state) {
   $form['bandwidth_opt'] = array(
      '#type' => 'checkbox',
      '#title' => t('BANDWIDTH OPTIMIZATION'),
    );

   return $form;
}

Notice that the hyphens of the element id in the HTML are replaced by underscores in the function declaration.

Another good tip is the Form API reference page.

3
  • thanks, just got it with hook_form_alter as suggested in the first commet within my question. I still wonder if hook_form_FORM_ID_alter would be "the better way" to do it since you're the second one suggesting it.
    – rugar
    Commented Jan 23, 2013 at 19:53
  • got it working like you did it in your example but the array should be called $form['bandwidth_optimization']['my_module'] instead. Otherwise you would override the existing one.
    – rugar
    Commented Jan 23, 2013 at 20:07
  • In fact both are right but I use to implement the hook_for_alter() when I have more than one form to alter on my custom module so I use a switch for the $form_id. In the other hand using the hook_form_FORM_ID_alter seems to be more clear and easier to maintain the code. Here goes somethig from the Drupal API page. "Modules can implement hook_form_FORM_ID_alter() to modify a specific form, rather than implementing hook_form_alter() and checking the form ID, or using long switch statements to alter multiple forms."
    – revagomes
    Commented Jan 23, 2013 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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