You are here

function shs_field_formatter_view in Simple hierarchical select 7

Implements hook_field_formatter_view().

File

./shs.module, line 781
Provides an additional widget for term fields to create hierarchical selects.

Code

function shs_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $elements = array();
  $settings = $display['settings'];
  if (empty($items) || $instance['widget']['type'] != 'taxonomy_shs') {
    return $elements;
  }
  switch ($display['type']) {
    case 'shs_default':
      foreach ($items as $delta => $item) {
        if (empty($item['term'])) {
          continue;
        }
        $list_items = array();

        // Add parent term names.
        foreach ($item['parents'] as $parent) {
          $list_items[] = array(
            'data' => $settings['linked'] ? l($parent->name, "taxonomy/term/{$parent->tid}") : $parent->name,
            'class' => array(
              'shs-parent',
            ),
          );
        }

        // Add name of selected term.
        $list_items[] = array(
          'data' => $settings['linked'] ? l($item['term']->name, "taxonomy/term/{$item['term']->tid}") : $item['term']->name,
          'class' => array(
            'shs-term-selected',
          ),
        );
        $elements[$delta] = array(
          '#items' => $list_items,
          '#theme' => 'item_list',
          '#attributes' => array(
            'class' => 'shs-hierarchy',
          ),
        );
      }

      // Add basic style.
      $elements['#attached']['css'][] = drupal_get_path('module', 'shs') . '/theme/shs.formatter.css';
      break;
  }
  return $elements;
}