You are here

function commerce_backoffice_handler_field_term_entity_tid::post_execute in Commerce Backoffice 7

Load the entities for all rows that are about to be displayed.

Overrides views_handler::post_execute

File

includes/views/handlers/commerce_backoffice_handler_field_term_entity_tid.inc, line 72
Definition of commerce_backoffice_handler_field_term_entity_tid.

Class

commerce_backoffice_handler_field_term_entity_tid
Field handler to display all taxonomy terms of an entity.

Code

function post_execute(&$values) {
  if (!empty($values)) {

    // Load the entities.
    list($this->entity_type, $this->entities) = $this->query
      ->get_result_entities($values);
    $vocabs = array_filter($this->options['vocabularies']);
    $vocabularies_to_fetch = $this->definition['vocabularies_to_fetch'];
    $tids = array();
    foreach ($this->entities as $row_id => $entity) {

      // Retrieve the taxonomy terms associated to this entity,
      // we're also taking in account the limit terms by vocabulary option.
      foreach ($vocabularies_to_fetch as $vocabulary => $field_name) {
        if (!empty($vocabs) && !in_array($vocabulary, $vocabs)) {
          continue;
        }
        $items = field_get_items($this->entity_type, $entity, $field_name);
        if ($items) {
          foreach ($items as $item) {
            $tids[$item['tid']][$row_id] = $row_id;
          }
        }
      }
    }
    if (!empty($tids)) {

      // Load all the associated taxonomy terms.
      $terms = entity_load('taxonomy_term', array_keys($tids));
      $vocabularies = taxonomy_vocabulary_load_multiple(NULL, array(
        'machine_name' => array_keys($vocabularies_to_fetch),
      ));

      // Sort by taxonomy weight.
      uasort($terms, array(
        'commerce_backoffice_handler_field_term_entity_tid',
        'sort_taxonomy_weight',
      ));
      foreach ($terms as $tid => $term) {
        foreach ($tids[$tid] as $row_id) {
          $this->items[$row_id][$term->tid]['name'] = check_plain($term->name);
          $this->items[$row_id][$term->tid]['tid'] = $term->tid;
          $this->items[$row_id][$term->tid]['vocabulary_machine_name'] = check_plain($term->vocabulary_machine_name);
          $this->items[$row_id][$term->tid]['vocabulary'] = check_plain($vocabularies[$term->vid]->name);
          if (!empty($this->options['link_to_taxonomy'])) {
            $this->items[$row_id][$term->tid]['make_link'] = TRUE;
            $this->items[$row_id][$term->tid]['path'] = 'taxonomy/term/' . $term->tid;
          }
          $values[$row_id]->{'taxonomy_term_' . $this->options['id']} = $this->items[$row_id];
        }
      }
    }
  }
}