2

I'm at an absolute loss as to how display a table and a form on the same page.

My objective is to publish information comparing our user data with our billing gateways data. My first thought was to create a dummy form item that can have it's values replaced by the submit handler akin to this Table and Form in the same page but that didn't appear to work with my callback.

My next attempt was to create a embedded form that upon a successful submission callback replaced information in the original render array. The idea of the code is to publish the 'one' in all the values before the submit button is called and 'two' after it is called. Here is that code:

function test_lookup_page($email = NULL){
    $hinput = array('One Customer Information' => 'aci', 'Two Customer Information' => 'uci');

    $header = array(
        'Legend' => array(
          'data'  => t('Legend'),
          'id'    => 'head-legend',
          'class' => 'col-legend',
        ),
      );

    foreach ($hinput as $hkey => $hvalue){

         $ckey[$hkey] = array(
          'data' => $hkey,
          'id'    => 'head-' . $hvalue,
          'class' => 'col-' . $hvalue,
        );
    $header = array_merge($header, $ckey);
    }

      // Table rows
      $rows = array(
        array(
          // Column 1
            'customer' => array(
            'data'  => t('Customer Account'),
            'id'    => 'row1-legend',
            'class' => 'col-legend',
          ),
            'One Customer Information' => array(
            'data' => 'first',
            'id' => 'row1-aci',
            'class' => 'col-aci',
          ),
            'Two Customer Information' => array(
            'data' => 'first',
            'id' => 'row1-uci',
            'class' => 'col-uci',
          ),
        ),
          array(
          // Column 2
          'billing' => array(
            'data'  => t('Billing'),
            'id'    => 'row2-legend',
            'class' => 'col-legend',
          ),
            'One Customer Information' => array(
            'data' => 'first',
            'id' => 'row2-aci',
            'class' => 'col-aci',
          ),
            'Two Customer Information' => array(
            'data' => 'first',
            'id' => 'row2-uci',
            'class' => 'col-uci',
          ),
        ),
          array(
          // Column 3
          'shipping' => array(
            'data'  => t('Shipping'),
            'id'    => 'row3-legend',
            'class' => 'col-legend',
          ),
            'One Customer Information' => array(
            'data' => 'first',
            'id' => 'row3-aci',
            'class' => 'col-aci',
          ),
            'Two Customer Information' => array(
            'data' => 'first',
            'id' => 'row3-uci',
            'class' => 'col-uci',
          ),
        ),
        array(
          // Column 1
          'subscription' => array(
            'data'  => t('Subscription(s)'),
            'id'    => 'row4-legend',
            'class' => 'col-legend',
          ),
          'One Customer Information' => array(
            'data' => 'first',
            'id' => 'row4-aci',
            'class' => 'col-aci',
          ),
            'Two Customer Information' => array(
            'data' => 'first',
            'id' => 'row4-uci',
            'class' => 'col-uci',
          ),
        ),

      );

        $build = array(
        'header_text' => array(
          '#type' => 'markup',
          '#markup' => '<p>' . t('To add additional content to a form page, you can use a page callback and return a render array like this.') . '</p>',
            '#theme' => 'table',
            '#header' => $header,
            '#rows' => $rows,
        ),

        'test_order_admin_form' => drupal_get_form('test_order_admin_settings', $email),

      );
      return $build;
    }



  function test_order_admin_settings($form, &$form_state, $email = NULL) {


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

return ($form);

}

  function test_order_lookup($form, &$form_state) {

     $hinput = array('One Customer Information' => 'aci', 'Two Customer Information' => 'uci');

     $header = array(
      'Legend' => array(
        'data'  => t('Legend'),
        'id'    => 'head-legend',
        'class' => 'col-legend',
      ),
    );

     foreach ($hinput as $hkey => $hvalue){

       $ckey[$hkey] = array(
        'data' => $hkey,
        'id'    => 'head-' . $hvalue,
        'class' => 'col-' . $hvalue,
      );
      $header = array_merge($header, $ckey);
   }

// Table rows
        $rows = array(
        array(
          // Column 1
            'customer' => array(
            'data'  => t('Customer Account'),
            'id'    => 'row1-legend',
            'class' => 'col-legend',
          ),
            'One Customer Information' => array(
            'data' => 'second',
            'id' => 'row1-aci',
            'class' => 'col-aci',
          ),
            'Two Customer Information' => array(
            'data' => 'second',
            'id' => 'row1-uci',
            'class' => 'col-uci',
          ),
        ),
          array(
          // Column 2
          'billing' => array(
            'data'  => t('Billing'),
            'id'    => 'row2-legend',
            'class' => 'col-legend',
          ),
            'One Customer Information' => array(
            'data' => 'second',
            'id' => 'row2-aci',
            'class' => 'col-aci',
          ),
            'Two Customer Information' => array(
            'data' => 'second',
            'id' => 'row2-uci',
            'class' => 'col-uci',
          ),
        ),
          array(
          // Column 3
          'shipping' => array(
            'data'  => t('Shipping'),
            'id'    => 'row3-legend',
            'class' => 'col-legend',
          ),
            'One Customer Information' => array(
            'data' => 'second',
            'id' => 'row3-aci',
            'class' => 'col-aci',
          ),
            'Two Customer Information' => array(
            'data' => 'second',
            'id' => 'row3-uci',
            'class' => 'col-uci',
          ),
        ),
         array(
          // Column 1
          'subscription' => array(
            'data'  => t('Subscription(s)'),
            'id'    => 'row4-legend',
            'class' => 'col-legend',
          ),
          'One Customer Information' => array(
            'data' => 'second',
            'id' => 'row4-aci',
            'class' => 'col-aci',
          ),
            'Two Customer Information' => array(
            'data' => 'second',
            'id' => 'row4-uci',
            'class' => 'col-uci',
          ),
        ),

      );


        $build = array(
        'header_text' => array(
            '#theme' => 'table',
            '#header' => $header,
            '#rows' => $rows,
        ),

      );


       return ($build);

       }

1 Answer 1

6

The submit handler isn't the right place to build a display, the core form builder functions don't actually assign the return value to anything so it's essentially ignored.

The normal process for this sort of thing is to set $form_state['rebuild'] = TRUE in your submit handler, generate the table and add it back on to the $form_state, and then let the form builder function decide what to display depending on the existence of that value.

This is a skeleton example which should help:

function MYMODULE_some_form($form, &$form_state) {
  if (isset($form_state['table'])) {
    $form['table'] = $form_state['table'];
  }
  else {
    // Otherwise build the form as normal
    $form['some_field'] = array(
      '#type' => 'textfield',
      // etc...
    );
  }

  return $form;
}

function MYMODULE_some_form_submit($form, &$form_state) {
  $form_state['table'] = function_to_generate_table($form_state);
  $form_state['rebuild'] = TRUE;   
}

There are various different permutations of that (depending on where you want to build the table based on the form state values; it could be the submit handler, form function or a helper function), just put your code wherever you feel most comfortable with it.

2
  • Thank you so much for your answer @Clive, I don't know what drupal.stackexchange.com would be without you. I really really appreciate how many things I've learned from you. Commented Aug 23, 2012 at 20:21
  • 1
    You're welcome, glad I can help so much :)
    – Clive
    Commented Aug 24, 2012 at 1:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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