You are here

function og_vocab_field_formatter_view in OG Vocabulary 7

Implements hook_field_formatter_view().

@todo Allow limiting by group visibilty. @todo Deal with taxonomy_select_nodes() not showing our nodes as they are not inside Taxonomy terms' tables.

File

./og_vocab.module, line 628
Give each group its own system controlled vocabularies.

Code

function og_vocab_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  global $user;
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  if (!og_vocab_is_og_vocab_field($entity_type, $field['field_name'], $bundle)) {
    return array(
      '#markup' => t('OG Vocabulary formatter should be used only on "Entity reference" fields with "OG complex" widgets.'),
    );
  }
  if (!$items) {
    return;
  }
  $tids = array();
  foreach ($items as $item) {
    $tids[] = $item['target_id'];
  }
  $terms = taxonomy_term_load_multiple($tids);
  $ordered_terms = array();
  foreach ($terms as $term) {
    $vocabulary = taxonomy_vocabulary_load($term->vid);
    $ordered_terms[] = array(
      'term' => $term,
      'weight' => $vocabulary->weight,
      'vid' => $vocabulary->vid,
    );
  }
  uasort($ordered_terms, 'og_vocab_sort_weight');

  // Re-build the terms array, as it is now ordered.
  $terms = array();
  foreach ($ordered_terms as $info) {
    $terms[] = $info['term'];
  }
  $settings = $display['settings'];
  if ($settings['concatenate']) {
    $values = array();
    foreach ($terms as $term) {
      $url = entity_uri('taxonomy_term', $term);
      $values[] = l($term->name, $url['path']);
    }
    $element[0] = array(
      '#markup' => implode(', ', $values),
    );
    return $element;
  }

  // Iterate over each term and group by vocabulary.
  $data = array();
  foreach ($terms as $term) {
    if (empty($data['vocabylary'][$term->vid])) {
      $vocab = taxonomy_vocabulary_load($term->vid);
      $data['vocabylary'][$term->vid] = check_plain($vocab->name);
    }
    $url = entity_uri('taxonomy_term', $term);
    $data['term'][$term->vid][] = l($term->name, $url['path']);
  }
  if (!$data) {
    return;
  }
  $element = array();
  foreach ($data['term'] as $vid => $values) {
    $element[0][$vid] = array(
      '#theme' => 'item_list',
      '#items' => $values,
      '#title' => $data['vocabylary'][$vid],
    );
  }
  return $element;
}