0

I am trying to create a form with essentially form elements stored inside a table. After digging around, have two alternatives:

1) Install Webform module along with the Webform Table Element module. Now both modules have been enabled and have cleared the caches for the heck of it. I can make the Webform alright, but I don't know how to make use of the Webform table element module and make the webform contents appear within a table. So I am not sure how to go about doing this

2) (SIDE QUESTION): The other alternative is to create a custom module that would let me build my own form with tables and all that jazz. But I am keen on learning the Drupal way although I always prefer hand-coding my challenges.

1

Drupal has theme() which can be used to create a table in a form. Your theme function must be registered with hook_theme().

function MODULE_NAME_theme() {
  return array(
   'MODULE_NAME_header_menu' => array(
     'arguments' => array(
       'form' => NULL,
      ),
   ),
}

function theme_MODULE_NAME_header_menu($form) {
  $rows = array();
  foreach (element_children($form) as $key) {
    if (is_numeric($key)) {
      $wt = $form[$key]['weight' . $key]['#value'];
      $menu_title = drupal_render($form[$key]['title' . $key]);
      $row[] = array('data' => $menu_title);
      $href = drupal_render($form[$key]['href' . $key]);
      $row[] = array('data' => $href);
      $weight = drupal_render($form[$key]['weight' . $key]);
      $row[] = array('data' => $weight);
      $remove = drupal_render($form[$key]['remove' . $key]);
      $row[] = array('data' => $remove);
      $rows[$wt] = array('data' => $row);
      unset($row);
    }
  }
  // Individual table headers.
  $header = array();
  $header[] = array('data' => t('Title'), 'class' => 'menu-title');
  $header[] = array('data' => t('URL'), 'class' => 'menu-href');
  $header[] = array('data' => t('Weight'), 'class' => 'menu-weight');
  $header[] = array('data' => t('Remove'), 'class' => 'menu-remove');

  $output = theme('table', $header, $rows, array('id' => 'header-menu-table'));  
  $output .=  drupal_render($form);

  return $output;
}

This creates a table with three columns (if the form has three numeric array keys: "title", "href", and "weight") plus a fourth column with a "remove" link.

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.