2

I am creating a custom module that has a button on the module's configuration page. When the button is clicked, a custom php function (located in the custom_module.module file) needs to run to perform some task.

How can I create a button on the custom module's configuration page that allows me to click the button to manually run the custom php function?

2 Answers 2

1

Each different button on your form can perform a different function by assigning it the #submit property:

function mymodule_my_form($form, &$form_state) {
  $form['button1'] = array(
    '#type' => 'submit',
    '#value' => 'Perform 1st Action',
    '#submit' => array('mymodule_my_form_action_one')
  );

  $form['button2'] = array(
    '#type' => 'submit',
    '#value' => 'Perform 2nd Action',
    '#submit' => array('mymodule_my_form_action_two')
  );
}

function mymodule_my_form_action_one($form, &$form_state) {
  // Perform the 1st action
}


function mymodule_my_form_action_two($form, &$form_state) {
  // Perform the 2nd action
}

Only the specified submit function will run for each button click.

1

You should use the Drupal Form API for it.

Please, check it: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#submit (it is about adding button).

And it: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#submit_property (it is about run custom function).

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