1

I'm trying to create a simple page with a form. With the different filled fields I want to execute different EntityFieldQuery in the HOOK_form_submit. To finish I want to display the result of myEntityFieldQueryunder the form like aView`.

For the moment I created a module with HOOK_menu :

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

    $items['statistiques'] = array(
        'title' => t('Statistics Page'),
        'description' => t('Homepage for statistics'),
        'page callback' => '_homepage_for_statistics',
        //'page arguments' => array('form_example_form'),
        'access arguments' => array('access content'),
    );

    return $items;
}

A simple form here :

function statistiques_form($form, &$form_state) {
    $form['age'] = array(
        '#title' => t('Age'),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#maxlength' => 3,
        '#size' => 3,
        '#weight' => 0,
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#validate' => array('statistiques_form_validate'),
        '#submit' => array('statistiques_form_submit'),
        '#weight' => 10,
        '#value' => t('Click Here!'),
    );

    return $form;
}

I have two functions statistiques_form_validate and statistiques_form_submit under hook_form

Then i send my form and others vars to my template like this :

function statistiques_theme() {
    return array(
        'statistiques_template' => array(
            'template' => 'statistiques',
            'variables' => array(),
        ),
    );
}

function _homepage_for_statistics(){
    global $user;

    $output = '';
    $vartest = 'my var test to template';

    $output .= theme('statistiques_template', array(
        'vartest' => $vartest,
        'form' => drupal_get_form('statistiques_form'),
    ));

    return $output;
}

My template look like this :

<?php print render($form['age']); ?>
<?php print render($form['submit']); ?>

I tried Example module, but this case isn't on this module. There is some cases which are close to my problem, after submit the form data are sent to flash message.

With my code the form doesn't work, I can't submit the form but I think it's a small problem... my real problem will come when I will need to display the result of my form validation to my template under the form or on another template. How can do that ?

Thanks

0

I recommend adding the results of the query to the form markup-like:

$form['something'] = array(
  '#theme' => 'statistiques_template',
  '#vartest' => $vartest,
);

is equivalent to theme('statistiques_template', array('vartest' => $vartest)) -- the theme system will remove the # from the variable names if #theme is set.

But, of course, you can add the form to the page build as well, see how comment_reply does it:

  $build['comment_node'] = node_view($node);
  $build['comment_form'] = drupal_get_form("comment_node_{$node->type}_form", (object) $edit);

If coding in Drupal 7, forget direct theme calls, strings and the . operator. Work with render arrays instead. It's also a good preparation for Drupal 8 where concatenating strings leads to double escape bugs.

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.