3

What is the Drupal way of presenting multi-row, multi-column module data to the user in Drupal 6? Nothing jumped out in the API documentation. I was googling for some answers, but they all applied to earlier versions or referenced dead modules.

Am I correct in thinking that the out-of-the-box functionality for the forms API is basically a key::value editing interface? If that's the case, is there a current, well-maintained module to do this, or should I present this data in a different way for the user -- perhaps a view?

Edit I would like this table to have form features for editing the data, such as insert, changing cell data, etc. That's why I thought it would be natural for it to reside in the module's settings, but that table functionality does not appear to be there.

  • There are many administration pages that use a table, and allow to edit the show data. Is there anything particular that you don't understand? – kiamlaluno Jun 17 '11 at 16:54
  • @kiamlaluno None of the documents or tutorials I've read talk about creating such a thing. But since you mention that, I will do some source diving to see how they do it. – user1359 Jun 17 '11 at 17:25
  • What I mean is that the question, as it is, seems too generic. I could report what done from Drupal code to render a form in table layout, but the code would be specific for a specific Drupal form. I am not sure the code I can report would help you, nor which code would help you more. – kiamlaluno Jun 17 '11 at 17:36
  • @kiamlaluna I am looking for generic information. A lot of docs and tutorials have examples like "Here's how to make a select box" or "Here's how to make a fieldset" or things like that, which are generalizable to the select or fieldset that I want to make. I'm looking for any instructions on how to make a table with per-row operations on a Drupal form. I can always look at the source, but there's a reason why docs and tutorials exist :) – user1359 Jun 21 '11 at 16:44
8

A little bit tricky

function your_form(){
  $form = array();
  ...
  $record_ids = array();

  foreach($record_set as $record){
      $record_ids[$record['id']] = ''; // Collect ids of your record as index
      $form['column1'][$record['id']] = array('#value' => $record['field1']);
      $form['column2'][$record['id']] = array('#value' => $record['field2']);
      $form['column3'][$record['id']] = array('#value' => $record['field3']);
  }

  $form['select'] = array(
      '#type' => 'checkboxes',
      '#options' => $item_ids,
  );

  $form['#theme'] = 'render_your_form'; // Theme registry function to render this form
}

Define theme_registry

function your_module_theme(){
   ...
   $theme['render_your_form'] = array(
       'arguments' => array('form' => array()),
   );
   return $theme;
}

Now write your theme handler

function theme_render_your_form($form=array()){
   $head = array('Select', 'Title Col 1', 'Title Col 2','Title Col 3');
   $rows = array();
   foreach(element_children($form['select']) as $id){
       $fields = array();
       $fields[] = drupal_render($form['select'][$id]); //This will render a checkbox, omit if not required
       $fields[] = drupal_render($form['column1'][$id]);
       $fields[] = drupal_render($form['column2'][$id]);
       $fields[] = drupal_render($form['column3'][$id]);
       $rows[] = $fields;
   }
   $output = theme('table', $head, $rows); // Theme as table output
   $output .= drupal_render($form); // Render rest of form components. (For D6)
   $output .= drupal_render_children($form); // Render rest of form components (For D7)
   return $output;
}
  • Is there any nice tutorial explaining this? I'm not able to grasp it. – AgA Dec 17 '11 at 5:50
  • I even did not find that so I investigated Drupal content page (example.com/content/node) from node module. – Shoaib Nawaz Dec 18 '11 at 3:53
  • Does node module contain this table form rendering code? Can you please place a working code here? – AgA Dec 18 '11 at 4:02

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.