You are here

function hs_taxonomy_field_formatter_prepare_view in Hierarchical Select 7.3

Implements hook_field_formatter_prepare_view().

File

modules/hs_taxonomy.module, line 391
Implementation of the Hierarchical Select API for the Taxonomy module.

Code

function hs_taxonomy_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {

  // Extract required field information.
  $vocabulary = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][0]['vocabulary']);
  $vid = $vocabulary->vid;

  // Get the config for this field.
  module_load_include('inc', 'hierarchical_select', 'includes/common');
  $config_id = hs_taxonomy_get_config_id($field['field_name']);
  $config = hierarchical_select_common_config_get($config_id);
  $config += array(
    'module' => 'hs_taxonomy',
    'params' => array(
      'vid' => $vid,
    ),
  );

  // Collect every possible term attached to any of the fieldable entities.
  // Copied from taxonomy_field_formatter_prepare_view().
  foreach ($entities as $id => $entity) {
    $selection = array();
    foreach ($items[$id] as $delta => $item) {

      // Force the array key to prevent duplicates.
      if ($item['tid'] != 'autocreate') {
        $key = in_array($item['tid'], $selection) ? array_search($item['tid'], $selection) : $delta;
        $selection[$key] = $item['tid'];
      }
    }

    // Generate a dropbox out of the selection. This will automatically
    // calculate all lineages for us.
    $dropbox = _hierarchical_select_dropbox_generate($config, $selection);

    // Store additional information in each item that's required for
    // Hierarchical Select's custom formatters that are compatible with the
    // save_lineage functionality.
    if (!empty($dropbox->lineages)) {
      foreach (array_keys($dropbox->lineages) as $lineage) {
        foreach ($dropbox->lineages[$lineage] as $level => $details) {
          $tid = $details['value'];

          // Look up where this term (tid) is stored in the items array.
          $key = array_search($tid, $selection);

          // Store the additional information. One term can occur in multiple
          // lineages: when Taxonomy's "multiple parents" functionality is
          // being used.
          $items[$id][$key]['hs_lineages'][] = array(
            'lineage' => $lineage,
            'level' => $level,
            'label' => $details['label'],
            'tid' => $tid,
          );
        }
      }
    }
  }
}