Currently, I see the module group has some widget

- Div
- Fieldset
- Vertical tabs group
- Vertical tab
- Horizontal tabs group
- Horizontal tab item
- Multipage group
- Multipage
- Accordion group
- Accordion item

Can add the "Table" format to this field type widget?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

webankit’s picture

Will be really good feature. May be something like http://drupal.org/project/tableform.

ErnestoJaboneta’s picture

I'd also like to use this feature! It would make my many fields so much nicer. (subscribe)

Stalski’s picture

Hello,

It seems like a nice feature indeed. It seems like a bit more difficult then the other since there are rows and columns to think about.
Is someone up for a patch?

Stalski’s picture

Status: Active » Needs work
charlie-s’s picture

Needs work indeed. I was thinking it would, for starters at least, be a 2 column table where td1 = label and td2 = field, is that what others were thinking?

Edit: I guess that doesn't take into account multiple value field, which would either need the label as the th and the fields as individual rows OR would put them inside of multiple rows like:

Field 1 Label | Field 1 Value
Field 2 Label | Field 2 Value 1
              | Field 2 Value 2
              | Field 2 Value 3
Field 3 Label | Field 3 Value
bforchhammer’s picture

Maybe this sandbox can serve as a starting point for a patch... The code isn't too complicated.

charlie-s’s picture

bforchhammer - this is awesome. The code is very minimal and readable. Do you see any issues or shortcomings here that need work?

bforchhammer’s picture

I am not sure about the table cell rendering code; basically I just copied and modified theme_field() and manually call the field preprocessing/processing functions (line 49/50). Two potential issues:

  • not sure whether the css classes for normal fields are appropriate inside a table... (might mess things up in some themes)
  • This ignores any overrides by other modules or themes (e.g. display suite "field templates"); not sure how important this is, but maybe there's a better or cleaner way...

Besides that, there should probably be an option for displaying a table <caption>; support for the caption is already in the sandbox, but commented out (see line 39).

If anyone wants to work on this inside the sandbox, send me a message and I'll grant you access.

charlie-s’s picture

Message sent.

I will take a look and see what's recommended for the classes. Caption option would be nice, but wouldn't that just be the field_group's label?

bforchhammer’s picture

Caption option would be nice, but wouldn't that just be the field_group's label?

Yes, I think so. However, people may not always want to display a table caption, in contrast to the fieldgroup legend which is needed for collapsing. So I think the value can just be the field group label, but I would probably also add a checkbox to the settings form for showing/hiding it. (see field_group.api.php for info on how to do this).

charlie-s’s picture

mrsinguyen’s picture

I see this module can resolve this problem http://drupal.org/project/field_group_multiple

nils.destoop’s picture

Status: Needs work » Fixed

Idd. That looks good. I'll add a link on our project page.

nils.destoop’s picture

Status: Fixed » Closed (fixed)
webadpro’s picture

Unless I'm not getting something how can field_group_multiple do:

Field Label | Field Value
Field Label | Field Value
Field Label | Field Value
...

I actually feel like bforchhammer's module is the right module.

davidwhthomas’s picture

Status: Closed (fixed) » Needs review
FileSize
2.87 KB

Here's how it's done with field group :-)

/**
 * hook_field_group_formatter_info().
 */
function EXAMPLE_field_group_formatter_info(){
  return array(
    'display' => array(
      'table' => array(
        'label' => t('Table'),
        'description' => t('This fieldgroup renders the field labels and values as cells in a table, with the group title as table heading.'),
        'format_types' => array('table'),
        'instance_settings' => array('classes' => ''),
        'default_formatter' => 'table',
      ),
  ));
}

/**
 * Implements hook_field_group_pre_render().
 * 
 * @param Array $elements by address.
 * @param Object $group The Field group info.
 */
function EXAMPLE_field_group_pre_render(&$element, $group, &$form) {

  switch ($group->format_type) {
    // Table formatter
    case 'table':
      
      // Sanity check, check group has field elements
      if ( ! is_array($element) || ! count($element) ) return FALSE;
      // Sort the grouped fields by #weight
      uasort($element, 'element_sort');
      // Build table options
      $options = array();
      $options['attributes'] = array(
        'class' => array('field-group-table',
                         'field-group-table-' . drupal_html_class($group->group_name))
      );
      // Two column header, value is group label
      $options['header'] = array();
      $options['header'][] = array(
        'data' => $group->label ? check_plain(t($group->label)) : '',
        'colspan' => 2
      );
      // Col 1 is field label
      // Col 2 is rendered field value
      $rows = array();
      // Loop over group elements and add to table rows
      foreach ($element as $field_name => $field) {
        $row = array();
        // Add field title
        $row[] = array(
          'data' => check_plain($field['#title']),
          'class' => array('field-label', 'field-label-' . drupal_html_class($field_name)),
        );
        // Hide title from Col 2
        $field['#label_display'] = 'hidden';
        $field['#title'] = '';
        // Render the field value if Col 2
        $row[] = array(
          'data' => render($field),
          'class' => array('field-value', 'field-value-' . drupal_html_class($field_name)),
        );
        // Add row to table rows
        $rows[] = $row;
      }
      // Finish off table options
      $options['rows'] = $rows;
      // Build element merge array
      $table = array(
        '#type' => 'markup',
        '#prefix' => '<div class="field-group-table-wrapper">',
        '#markup' => theme('table', $options),
        '#suffix' => '</div>',
        '#weight' => $group->weight,
      );
      // Merge our table into the element
      $element += $table;
      break;
  }
}

A patch integrating this with the core field group module is attached.

Cheers,

DT

m.stenta’s picture

I need something similar, but in my case I want to group a dozen or so fields in a table that has only two rows, and one column for each field.

So instead of representing it as a 2 column, X rows... I need X columns, 2 rows. Make sense?

Perhaps it makes sense to add a toggle setting to the table formatter that allows switching back and forth between these two scenarios.

davidwhthomas’s picture

The code for the above table formatter only does

<groupname>
<label> | <value>
<label> | <value>
<label> | <value>

style tables.

As that was all I needed.
I've no plans to change myself that at this stage.

DT

webadpro’s picture

Actually it shouldn't be that hard.

Loop Through all the fields twice.

1st loop: Add the label column for each to row1
2nd loop: add the value column for each to row2

m.stenta’s picture

Thanks, I just tried the field_group_multiple module, and it does exactly what I need.

Stalski’s picture

Status: Needs review » Closed (fixed)

This was closed. If people want to use this code, they can, but the field_group_multiple module already exists. We are not going to maintain the same thing.

webadpro’s picture

I don't think this field_group_multiple can actually do FIELD|Fieldvalue. Unless you manually want to fill out a table, yes it would work, but its wasn't working last time I Tried.

webadpro’s picture

m.stenta how did you managed to have
Label|Label|Label|...
Value|Value|Value|...

using field_group_multiple?

m.stenta’s picture

@webadpro: yes. field_group_multiple worked for me.

webadpro’s picture

Which option have you selected?

m.stenta’s picture

@webadpro: I am using the "Multiple fields table" formatter, provided by http://drupal.org/project/field_group_multiple

maulwuff’s picture

#16 is great. I put the code right away into its own module called field_group_tableformatter. I added a info and a install file and was finished. So for those not willing or able to patch the field_group module, this is an easy alternative. I've attached the module with the code from #16
Thanks a lot davidwhthomas

a.ross’s picture

I suggest to continue this here: #1477532: Add flipped table formatter

a.ross’s picture

I've just promoted this module to a full project:

http://drupal.org/project/field_group_table

It's much more simplistic than field_group_multiple, which solves a different problem than what most people in this issue seem to want, including myself.