سؤال

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);
}
هل كانت مفيدة؟

المحلول

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().

نصائح أخرى

Since it's a search form, how about making it use GET and grabbing the value out of the $_GET array in your preprocess function? GET is usually recommended for search forms anyway so the results are bookmarkable and shareable.

Like this: https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#method

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top