Drupal 7 How to add a button act like "Clear all caches" in the custom form in custom module? OR Example for button act like "Clear all caches" in the custom form in custom module.
This Docs help you to understand How to add more than one submit button in a single Drupal 7 Custom form in Custom module?
More than that How to add a button act like "Clear all caches" in the custom form in custom module?
Please find the following code, will help you to achieve this.
In the code we do have implemented hook_menu(). After enabling this code in your custom module you can go to the page where it appearing the form, here the path is "example/extrabutton_form",
You can see two buttons like 'View my username' and 'View my user ID', By clicking this
button it will perform certain action and show the respective result. This how the "Clear all caches" button works.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @File | |
* Example for button act like "Clear all caches" in the custom form in custom module | |
* @author Rakesh James | |
* | |
*/ | |
/** | |
* Implementing hook_menu(). | |
*/ | |
function example_menu() { | |
$items['example/extrabutton_form'] = array( | |
'title' => 'My form', | |
'page callback' => 'drupal_get_form', | |
'page arguments' => array('my_form'), | |
'access callback' => TRUE, | |
'type' => MENU_CALLBACK, | |
); | |
return $items; | |
} | |
/** | |
* call back function. | |
*/ | |
function my_form() { | |
$form['button_view_uname'] = array( // First button for viewing your user name, It will act as "Clear all caches" button. | |
'#type' => 'submit', | |
'#value' => 'View my username', | |
'#submit' => array('view_my_user_name'), | |
); | |
$form['button_view_uid'] = array( // First button for viewing your user ID, It will act as "Clear all caches" button. | |
'#type' => 'submit', | |
'#value' => 'View my user ID', | |
'#submit' => array('view_my_user_id'), | |
); | |
return $form; | |
} | |
/** | |
* Callback function for the First button "View my username". | |
* @function it will print your user name, when you press the button. | |
*/ | |
function view_my_user_name($form, $form_state) { | |
global $user; | |
drupal_set_message(t("Your user name is .$user->name")); | |
} | |
/** | |
* Callback function for the Second button "View my userid". | |
* @function it will print your user id, when you press the button. | |
*/ | |
function view_my_user_name($form, $form_state) { | |
global $user; | |
drupal_set_message(t("Your user ID is .$user->uid")); | |
} |
Comments
Post a Comment