You are here

function multifield_table_field_formatter_view in Multifield table 7

Implements hook_field_formatter_view().

File

./multifield_table.module, line 81
Provides a table formatter and widget for multifield field types.

Code

function multifield_table_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  if ($items) {
    $header = $rows = array();
    foreach ($items as $item) {

      // Generate the table header.
      if (!$header) {
        $instances = field_info_instances('multifield', $item['#pseudo_entity']->type);
        foreach ($instances as $name => $instance) {
          if ($instance['display'][$settings['view_mode']]['type'] != 'hidden') {
            $fields[$instance['display'][$settings['view_mode']]['weight']] = $name;
          }
        }
        ksort($fields);
        foreach ($fields as $subtype) {
          $header[$subtype] = array(
            'data' => $instances[$subtype]['label'],
            'class' => drupal_clean_css_identifier($subtype),
          );
        }
      }

      // Display field columns.
      $row = array();
      foreach ($fields as $subtype) {
        if ($cell = field_view_field('multifield', $item['#pseudo_entity'], $subtype, $settings['view_mode'], $langcode)) {
          $row[] = render($cell[0]);
        }
        else {
          $row[] = '';
        }
      }
      $rows[] = $row;
    }
    if ($rows) {
      $settings += array(
        'header_type' => 'sticky',
      );
      $element[0] = array(
        '#theme' => 'table',
        '#header' => $settings['header_type'] == 'none' ? array() : $header,
        '#rows' => $rows,
        '#sticky' => $settings['header_type'] == 'sticky',
      );
    }
  }
  return $element;
}