drupal 7 system_settings_form add button

In most cases it is enough of functional of system_settings_form for saving settings and dont care 'bout submit ops. But what to do if it you need to add additional button to form?

To add a button and make it functional we need to add functoin to $form['#submit'] array.

 

Let's begin. Write standart form function.

Declare a variable $form

$form = system_settings_form($form);

After this, write down all your fields. At the end we added new button (Of course, new button can be added elsewhere after declare variable $form).

$form['actions']['custom_submit'] = array(

  '#type' => 'submit',

  '#value' => t('Custom submit'),

);

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

We added in array of submits our function. It will  fire after the system submit.

function _mymodule_custom_submit($form, $form_state) {
  switch ($form_state['clicked_button']['#value']) {
    case t('Custom submit'):
    // тут код
    break;
  }
}

That's all. Now we have system form settings with built-in submit and our button.

Tags: 

Comments

Add new comment