phpdrupaldrupal-fapidrupal-preprocess

How to get form submitted values in page_preprocess function in drupal 7


I have some output data from databse on my page (few queries to db in mytheme_page_preprocess function) and i want to do search form (text field and submit button). So, how can i get form submitted values in preprocess function ?

Something like $form_state['values'] in myform_form_submit($form, $form_state), but in preprocess function.

My simple search form

function reestr_form($form, &$form_state) 
{
    $form = array();
    $form['q'] = array(
                       '#type' => 'textfield',
                       '#size' => 30,
                       '#default_value' => t(''),
                      );
    $form['submit'] = array(
                            '#type' => 'submit',
                            //'#value' => 'send',
                            '#name' => 'op',
                            '#src' => base_path() . path_to_theme() . '/images/search-button.png',
                            '#submit' => array('reestr_form_submit')
                            );
    //$form['#submit'][] = 'reestr_search_submit';
    return $form;
}

function reestr_form_submit($form, &$form_values) 
{
    $message = 'You have submitted the ' . $form_id . ' form which contains the following data:<pre>' . print_r($form_values, true) . '</pre>';
    var_dump($message);
}

Solution

  • If template_process_HOOK is part of your form, you should have access to your entity in $variables['form']['#entity'].

    /**
     * Implements template_process_HOOK().
     */ 
    function mymodule_preprocess_myhook(&$variables) {
      $entity = $variables['form']['#entity'];
      $field_name = $variables['form']['#field_name'];
      $info = field_info_field($field_name);
      $instance = field_info_instance($entity->entityType(), $field_name, $entity->type);
    }
    

    Alternatively use some ctools cache (ctools_object_cache_set()/ctools_object_cache_get()) or load the form from the cache via form_get_cache().