0

I have a custom module which had a form with some inputs and after submitting it, it creates two tables with some info.

I'm now changing the way I show the information. For that, I'm using Panels, creating the form inputs and the content with blocks.

My question is: how can I have the form inputs in block1, and two different content result for block2 and block3, both dependent of the inputs on block1?

As an example, I managed to create one single block with both the form inputs and the result of the submit. But I'm unable to create the 3 blocks separately.

EDIT: Tried to use $_SESSION or variable_set and variable_get. It works partially. Only after the second form submit, the values of my inputs are setted correctly in the variables. Is this even the best solution?

function MYMODULE_form_submit($form, &$form_state) {
    $form_state['storage']['dateMonth'] = $form_state['values']['dateMonth'];
    $form_state['storage']['user'] = $form_state['values']['user'];

    $form_state['rebuild'] = TRUE;

    variable_set('dateMonth', $form_state['values']['dateMonth']);
    variable_set('user', $form_state['values']['user']);
}

function MYMODULE_getValues() {
    $dateMonth = variable_get('dateMonth');
    $user = variable_get('user');
}

Complete Code:

function MYMODULE_block_info() {
    $blocks = array();
    $blocks['Block_Time_Table'] = array(
        'info' => t('Block_Time_Table'),
    );
    $blocks['Block_Time_Total'] = array(
        'info' => t('Block_Time_Total'),
    );
    return $blocks;
}

function MYMODULE_block_view($delta = '') { 
    $block = array();
    switch ($delta) {
        case 'Block_Time_Table':
            $block['subject'] = '';
            $block['content'] = drupal_get_form('MYMODULE_timetable_form');
            break;
        case 'Block_Time_Total':
            $block['subject'] = '';
            $block['content'] = drupal_get_form('MYMODULE_timetotal');
            break;
    }
    return $block;
}

function MYMODULE_timetable_form($form, &$form_state) {
    $format = 'F-y';
    $today = date('Y-m-d');

    $form['container'] = array(
        '#prefix' => '<div class="container-inline">',
        '#suffix' => '</div>',
    );

    $form['table_container'] = array(
        '#prefix' => '<div class="container-inline">',
        '#suffix' => '</div>',
    );

    $form['container']['dateMonth'] = array(
        '#type' => 'date_select',
        '#default_value' => isset($form_state['storage']['dateMonth']) ? $form_state['storage']['dateMonth'] : $today,
        '#date_format' => $format,
        '#date_label_position' => 'within',
        '#date_year_range' => '-3:+1',
    );

    $form['container']['user'] = array(
        '#title' => t('User'),
        '#type' => 'select',
        '#options' => array(
            0 => 'user1',
            1 => 'user2',
        ),
        '#default_value' => isset($form_state['storage']['user']) ? $form_state['storage']['user'] : 0,
    );

    $form['container']['submit'] = array(
        '#type' => 'submit',
        '#value' => t("Submit"),  
    );

    $form['markup'] = array(
        '#markup' => '</br>',
        '#weight' => 0,
    );

    if (isset($form_state['storage']['dateMonth'])) {   
        $dateMonth = $form_state['storage']['dateMonth'];

        $table = MYMODULE_getTimeTable($dateMonth, $form_state['storage']['user'], 'timeTable');

        $form['table_container']['table'] = array(
            '#theme' => 'table',
            '#header' => $table['header'],
            '#rows' => $table['rows'],
            '#empty' => t('No data to show.')
        );
    }
    return $form;
}

function MYMODULE_timetable_form_submit($form, &$form_state) {
    $form_state['storage']['dateMonth'] = $form_state['values']['dateMonth'];
    $form_state['storage']['user'] = $form_state['values']['user'];
    $form_state['rebuild'] = TRUE;
}

function MYMODULE_timetotal() {
    ### I NEED TO GET THE 2 VARIABLES BELOW FROM THE FORM:
    $dateMonth = '2018-12';
    $user = 1;
    ###

    $table = MYMODULE_getTimeTable($dateMonth, $user, 'timeTotal');

    $header = array(
        0 => 'id',
        1 => 'Total Time',
    );

    $form['table'] = array(
        '#theme' => 'table',
        '#header' => $header,
        '#rows' => $table['totalTime'],
    );
    return $form;
}
  • I'm not sure I get what you are doing, but I can tell you that you are globally setting a variable here that will affect every user, not just the current user. Not sure if that is your intent or not. – Kevin Jan 3 at 16:48
  • Thanks for your comment. I just need to have a form with date selectbox, user select box and a submit button. I want to render that form in block1 When I submit it, I'm querying the database and creating a table. I want to render it in block2. I also need to render other table in block3. Why blocks? Because this way I can add them to a panel layout. If the variable_set is global, I can't use that method. – zephirus Jan 3 at 17:08
0

Here is the solution for what I wanted. The only "problem" with this solution is that the arguments are passed by url.

function MYMODULE_block_info() {
    $blocks = array();
    $blocks['Block_Time_Table'] = array(
        'info' => t('Block_Time_Table'),
    );
    $blocks['Block_Time_Total'] = array(
        'info' => t('Block_Time_Total'),
    );
    return $blocks;
}

function MYMODULE_block_view($delta = '') { 
    $block = array();
    switch ($delta) {
        case 'Block_Time_Table':
            $block['subject'] = '';
            $block['content'] = drupal_get_form('MYMODULE_timetable_form');
            break;
        case 'Block_Time_Total':
            $block['subject'] = '';
            $block['content'] = drupal_get_form('MYMODULE_timetotal');
            break;
    }
    return $block;
}

function MYMODULE_timetable_form($form, &$form_state) {
    $querystring = drupal_get_query_parameters();

    $format = 'F-y';
    $today = date('Y-m-d');

    $form['container'] = array(
        '#prefix' => '<div class="container-inline">',
        '#suffix' => '</div>',
    );

    $form['table_container'] = array(
        '#prefix' => '<div class="container-inline">',
        '#suffix' => '</div>',
    );

    $form['container']['dateMonth'] = array(
        '#type' => 'date_select',
        '#default_value' => isset($querystring['dateMonth']) ? $querystring['dateMonth'] : $today,
        '#date_format' => $format,
        '#date_label_position' => 'within',
        '#date_year_range' => '-3:+1',
    );

    $form['container']['user'] = array(
        '#title' => t('User'),
        '#type' => 'select',
        '#options' => array(
            0 => 'user1',
            1 => 'user2',
        ),
        '#default_value' => isset($querystring['user']) ? $querystring['user'] : 0,
    );

    $form['container']['submit'] = array(
        '#type' => 'submit',
        '#value' => t("Submit"),  
    );

    $form['markup'] = array(
        '#markup' => '</br>',
        '#weight' => 0,
    );

    if (isset($form_state['storage']['dateMonth'])) {   
        $dateMonth = $form_state['storage']['dateMonth'];

        $table = MYMODULE_getTimeTable($dateMonth, $form_state['storage']['user'], 'timeTable');

        $form['table_container']['table'] = array(
            '#theme' => 'table',
            '#header' => $table['header'],
            '#rows' => $table['rows'],
            '#empty' => t('No data to show.')
        );
    }
    return $form;
}

function MYMODULE_timetable_form_submit($form, &$form_state) {
    $form_state['redirect'] = array(
        current_path(),
        array(
            'query' => array(
                'dateMonth' => $form_state['values']['dateMonth'],
                'user' => $form_state['values']['user'],
            ),
        ),
    );  
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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