First thing you have to do is to move the form into it's own block. That will require a hook_block_info()
, to define the block, and hook_block_view()
, to create the content of the block.
https://api.drupal.org/api/drupal/modules%21block%21block.api.php/function/hook_block_info/7.x
https://api.drupal.org/api/drupal/modules%21block%21block.api.php/function/hook_block_view/7.x
Define the block:
function my_module_block_info() {
$blocks['your_block'] = array(
// info: The name of the block.
'info' => t('Your Block Name'),
);
return $blocks;
}
Then create the content. You can use another function that contains the form.
function my_module_block_view($delta = '') {
// This example is adapted from node.module.
$block = array();
switch ($delta) {
case 'your_block':
$block['subject'] = t('My Form');
$block['content'] = drupal_render(drupal_get_form('my_module_myform_form'));
break;
}
return $block;
}
function my_module_myform_form() {
// Your form will go here.
$form['my_value'] = array
(
'#type' => 'select',
'#options' => array('' => '---', 1 => t('One'), 2 => t('Two'), 3 => t('Three')),
'#required' => TRUE,
);
$form['submit'] = array
(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
Next is to create a submit callback which redirects to your page. You don't need to register the callback. Drupal looks for the form function name with '_submit' appended.
function my_module_myform_form_submit($form, &$form_state) {
$form_state['redirect'] = 'some/path/' . $form_state['values']['my_value'];
}
And then we need to create the page:
function my_module_menu()
{
$menu['some/path/%'] = array
(
'title' => 'My form submitted values',
'page callback' => 'my_form_submitted_value_callback',
// array(2) will pass the value of the wildcard to the callback function
'page arguments' => array(2),
'access callback' => TRUE,
);
return $menu;
}
function my_form_submitted_value_callback($submitted_value)
{
return t('The value you submitted was: @value', array('@value' => $submitted_value));
}
If you need more than one value, there are more methods on this page like using $_GET and session variables:
https://www.drupal.org/node/1990276
Sources: