A form was created with codes in a custom module, with three items that must be filled. After submission, the code will do it's magic and return the result on the same page, below the form.

I'll like to have the form alone in a block so that it can be placed anywhere on the websites and when filled from there, it will redirect to the main page and show the results.

I don't know which way to go please.

1 Answer 1


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'));
  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


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.