1

I am trying to adapt a Drupal 5 form to a Drupal 7 form, but something is not working. The form is correctly displayed, but when "Submit" button is clicked the information in the form is not correctly send to $_GET. What am I missing??

<?php 
 function my_menu($may_cache) {
  $items = array();
    $items['admin/reporting/report_details'] = array(
    'title' => 'Report: User details by stores',
    'access arguments' => array('access content'),
    'page callback' => 'say_report_details',
    'type' => MENU_CALLBACK,);  

 return $items;
}

function say_report_details($values = array()) {
  $form = drupal_get_form("report_details_form");
  //$output = theme("report_page", $form, $output);
  return $form;
}


function report_details_form() {
 $form["search"] = array(
 '#type' => 'fieldset',
 '#title' => t('Search params'),
 '#collapsible' => FALSE,
 '#tree' => TRUE,
 );

 for ($i = 1; $i < 32; $i++) {
  $days_opt[$i] = $i;
 }
 for ($i = 1; $i < 13; $i++) {
  $month_opt[$i] = $i;
 }
 for ($i = 2008; $i < date("Y") + 3; $i++) {
  $year_opt[$i] = $i;
 }

 $form["search"]["from_day"] = array(
  '#type' => 'select',
  '#title' => 'Date from',
  '#options' => $days_opt,
  '#default_value' => (($_GET['from_day'] == "") ? date("d") : $_GET['from_day'])
 );
 $form["search"]["from_month"] = array(
  '#type' => 'select',
  '#title' => '&nbsp;',
  '#options' => $month_opt,
  '#default_value' => (($_GET['from_month'] == "") ? date("m") : $_GET['from_month'])
 );
 $form["search"]["from_year"] = array(
  '#type' => 'select',
  '#title' => '&nbsp;',
  '#options' => $year_opt,
  '#default_value' => (($_GET['from_year'] == "") ? date("Y") : $_GET['from_year'])
 );

 $form["search"]["to_day"] = array(
  '#type' => 'select',
  '#title' => 'Date to',
  '#options' => $days_opt,
  '#default_value' => (($_GET['to_day'] == "") ? date("d") : $_GET['to_day'])
 );
 $form["search"]["to_month"] = array(
  '#type' => 'select',
  '#title' => '&nbsp;',
  '#options' => $month_opt,
  '#default_value' => (($_GET['to_month'] == "") ? date("m") : $_GET['to_month'])
 );
 $form["search"]["to_year"] = array(
  '#type' => 'select',
  '#title' => '&nbsp;',
  '#options' => $year_opt,
  '#default_value' => (($_GET['to_year'] == "") ? date("Y") : $_GET['to_year'])
 );

 $result = db_query('SELECT something in the database');

 $strs = array("all" => "All");

 foreach ($result as $store) {
   $strs[$store->tid] = $store->name;
 }

 $form["search"]["store"] = array(
  '#type' => 'select',
  '#title' => 'Stores',
  '#options' => $strs,
  '#default_value' => $_GET['store']
 );

 $form["submit"] = array("#type" => "submit", "#value" => "Show report");

 return $form;
}

function report_details_form_submit($form_id, $form_values) {
  $query = "";
// This next line is the one I am having an issue with right now.$form_values is empty
  foreach ($form_values['search'] as $key => $value) {
     $query .= "&". $key ."=". $value;
  }

 return array('admin/reporting/report_details', $query);
}

The code throws an error at last "foreach" as $form_values is empty for some reason.Thank you!!

  • 3
    I recommend you to start with Examples for Developers. $_GET is processed automatically now. – kalabro Jun 8 '12 at 7:27
  • 1
    You can use the following code in submit button function report_details_form_submit($form_id, &$form_state) { $query = ""; $from_month=$form_state['values']['search']['from_month']; //In this way you can get values of all fields and use them as per your requirement – Dharmender Jun 8 '12 at 7:35
1

By default Drupal form sends values using post method. If you are looking to recieve values in $_GET you will have to define #method attribute of your form to get.

$form['#method'] = 'get';
0

Your code is not correct in many points, but the main reason you are not getting the values you are expecting is that the code to redirect the form is wrong, for Drupal 6/7.

The following code works.

function report_details_form_submit($form, &$form_state) {
  $form_state['redirect'] = array($_GET['q'], 'query' => $form_state['values']);
}

This code is similar to the one executed by search_form_submit(); the difference is that search_form_submit() add the search values to the path, separating them with a slash.

 $keys = $form_state['values']['processed_keys'];
  if ($keys == '') {
    form_set_error('keys', t('Please enter some keywords.'));
    // Fall through to the form redirect.
  }

  $form_state['redirect'] = $form_state['action'] . '/' . $keys;

There are other things that needs to be changed, such as:

  • hook_menu() doesn't get any parameter, in Drupal 7.
  • There is no need to use a function that calls drupal_get_form() as page callback. Just use code similar to the following one.

    function my_menu($may_cache) {
      $items = array();
    
      $items['admin/reporting/report_details'] = array(
        'title' => 'Report: User details by stores',
        'access arguments' => array('access content'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('report_details_form'),
        'type' => MENU_CALLBACK,
      );  
    
      return $items;
    }
    
  • Functions defined from a module should be prefixed with its short name. If "my" is the short name of your module, then every function it defines it should have its name starting with my_, or _my_ (if the function is private).

  • You should verify the values are set, before to using them. Module code is supposed not to throw any warning. I would rather use the following code.

    $form["search"]["from_month"] = array(
      '#type' => 'select',
      '#title' => '&nbsp;',
      '#options' => $month_opt,
      '#default_value' => empty($_GET['from_month']) ? date("m") : $_GET['from_month'])
    

    );

Considering that (since Drupal 6) a form builder receives two parameters ($form, &$form_state), I would rather write the code as follows.

function my_menu() {
  $items = array();

  $items['admin/reporting/report_details'] = array(
    'title' => 'Report: User details by stores',
    'access arguments' => array('access content'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('my_report_details_form'),
    'type' => MENU_CALLBACK,
  );  

 return $items;
}


function my_report_details_form($form, $form_state) {
  $form["search"] = array(
    '#type' => 'fieldset',
    '#title' => t('Search parameters'),
    '#collapsible' => FALSE,
  );

  $from_date = array(
    'day' => isset($form_state['my_report']['from']) ? $form_state['my_report']['from']['day'] : date('d'),
    'month' => isset($form_state['my_report']['from']) ? $form_state['my_report']['from']['month'] : date('m'),
    'year' => isset($form_state['my_report']['from']) ? $form_state['my_report']['from']['year'] : date('Y'),
  );

  $form["search"]["from"] = array(
   '#type' => 'date',
   '#title' => t('From'),
   '#default_value' => $from_date,
  );

  $to_date = array(
    'day' => isset($form_state['my_report']['from']) ? $form_state['my_report']['to']['day'] : date('d'),
    'month' => isset($form_state['my_report']['from']) ? $form_state['my_report']['to']['month'] : date('m'),
    'year' => isset($form_state['my_report']['from']) ? $form_state['my_report']['to']['year'] : date('Y'),
  );

  $form["search"]["to_day"] = array(
    '#type' => 'date',
    '#title' => t('To'),
    '#default_value' => $to_date,
  );

  $options = array("all" => "All");
  $result = db_query(/* */);

  foreach ($result as $store) {
    $options[$store->tid] = $store->name;
  }

  $form["search"]["store"] = array(
    '#type' => 'select',
    '#title' => t('Stores'),
    '#options' => $options,
    '#default_value' => isset($form_state['my_report']['store']) ? $form_state['my_report']['store'] : 'all',
  );

  $form["submit"] = array("#type" => "submit", "#value" => "Show report");

  if (isset($form_state['my_report']['result'])) {
    $headers = array(/* The headers to use for the table */);

    $form['result'] = array(
      '#theme' => 'table',
      '#header' => $headers,
      '#rows' => $form_state['my_report']['result'],
      '#empty' => t('There are no reports found in the database'),
    );
  }

  return $form;
}

function my_report_details_form_submit($form, &$form_state) {
  $form_state['my_report']['from'] = $form_state['values']['from'];
  $form_state['my_report']['to'] = $form_state['values']['to'];
  $form_state['my_report']['store'] = $form_state['values']['store'];

  // Search the reports, and populate $form_state['my_report']['result'].

  $form_state['rebuild'] = TRUE;
}

In this way, there would not be any need to populate $_GET with the data submitted in the form.

See theme_table() for the value to use as #rows.

Reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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