You are here

function properties_field_formatter_view in Dynamic properties 7

Implements hook_field_formatter_view().

File

./properties.module, line 715
This module provides a dynamic property field that can contain an unlimited number of attribute value pairs.

Code

function properties_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $items_by_category = array();
  foreach ($items as $item) {
    if (!isset($items_by_category[$item['category']])) {
      $items_by_category[$item['category']] = array();
    }
    $items_by_category[$item['category']][] = $item;
  }
  $categories = array();
  if (!empty($items_by_category)) {
    $categories = properties_category_load_multiple(array_keys($items_by_category));
  }
  switch ($display['type']) {
    case 'properties_formatter_list':
      $element[0]['#prefix'] = '<div class="profile">';
      $element[0]['#suffix'] = '</div>';
      foreach ($items_by_category as $category => $items_in_category) {
        $element[0][] = array(
          '#type' => 'user_profile_category',
          '#title' => check_plain($categories[$category]->label),
        );
        foreach ($items_in_category as $item) {
          if (isset($categories[$category]->attributes[$item['attribute']])) {
            $label = $categories[$category]->attributes[$item['attribute']]->label;
          }
          elseif (!empty($item['attribute'])) {
            $label = properties_attribute_load($item['attribute'])->label;
          }
          else {
            continue;
          }
          $element[0][] = array(
            '#type' => 'user_profile_item',
            '#title' => $label,
            '#markup' => check_plain($item['value']),
          );
        }
      }
      break;
    case 'properties_formatter_table':
      $rows = array();
      foreach ($items_by_category as $category => $items_in_category) {
        $rows[] = array(
          array(
            'data' => check_plain($categories[$category]->label),
            'colspan' => 2,
            'header' => TRUE,
            'class' => array(
              drupal_html_class($category),
            ),
          ),
        );
        foreach ($items_in_category as $item) {
          if (isset($categories[$category]->attributes[$item['attribute']])) {
            $label = $categories[$category]->attributes[$item['attribute']]->label;
          }
          else {
            $label = properties_attribute_load($item['attribute'])->label;
          }
          $rows[] = array(
            array(
              'data' => check_plain($label),
              'class' => drupal_html_class($item['attribute']) . '-label',
            ),
            array(
              'data' => check_plain($item['value']),
              'class' => drupal_html_class($item['attribute']) . '-data',
            ),
          );
        }
      }
      $element[0]['#markup'] = theme('table', array(
        'rows' => $rows,
      ));
      break;
  }
  return $element;
}