/**
  * Create a taxonomy related "score" for each result's matching terms.
  */
 private function scoreResults($bean, $aggregate, $valid_tid)
 {
     $result = array();
     if (isset($aggregate[$bean->settings['entity_type']])) {
         foreach ($aggregate[$bean->settings['entity_type']] as $key => $value) {
             $entity_terms = bean_tax_get_entity_terms($bean->settings['entity_type'], $key, $value->vid);
             $score = 0;
             // The actual scoring to determine valid taxonomy term matching.
             foreach ($entity_terms as $term) {
                 if (isset($valid_tid[$term->tid])) {
                     $score++;
                 }
             }
             $item['id'] = $key;
             $item['score'] = $score;
             // A score of 1 or greater adds to the array of matching entities.
             if ($score != 0) {
                 $result[] = $item;
             } elseif ($score == 0 && $bean->settings['unmatch_add']) {
                 $result[] = $item;
             }
         }
     }
     // Calculate an overall score.
     $all = 0;
     foreach ($result as $item) {
         $all = $item['score'] + $all;
     }
     // If overall score is not zero, sort based on score.
     if ($all != 0) {
         // Invoke comparison function to determine highest ranked results.
         usort($result, "bean_tax_cmp");
     }
     return $this->removeActive($result, $bean->settings['entity_type']);
 }
 /**
  * Displays the bean.
  */
 public function view($bean, $content, $view_mode = 'default', $langcode = NULL)
 {
     // We need to make sure that the bean is configured correctly.
     if (!empty($bean->filters['vocabulary']) && !empty($bean->settings['bundle_types'])) {
         // Define an array of all taxonomy terms in the defined vocabularies.
         $possible_tid = array();
         foreach ($bean->filters['vocabulary'] as $vm) {
             $query = new EntityFieldQuery();
             $result = $query->entityCondition('entity_type', 'taxonomy_vocabulary')->propertyCondition('machine_name', $vm)->execute();
             foreach ($result['taxonomy_vocabulary'] as $vocabulary) {
                 $vid = $vocabulary->vid;
             }
             $tree = taxonomy_get_tree($vid);
             foreach ($tree as $term) {
                 $possible_tid[$term->tid] = $term->tid;
             }
         }
         // Compare possible terms to those attached to the menu object or current
         // user depending on 'related' settings.
         $active_entity = bean_tax_active_entity_array($bean->settings['related']);
         if (isset($active_entity['terms'])) {
             $valid_tid = array();
             foreach ($active_entity['terms'] as $term) {
                 if (isset($possible_tid[$term->tid])) {
                     $valid_tid[$term->tid] = $term->tid;
                 }
             }
             // Store Entity type.
             $type = $bean->settings['entity_type'];
             // Entity field query for entities of the defined bundle.
             $aggregate = array();
             foreach ($bean->settings['bundle_types'] as $bundle) {
                 $query = new EntityFieldQuery();
                 $query->entityCondition('entity_type', $type);
                 $query->entityCondition('bundle', $bundle);
                 $query->propertyOrderBy('created', 'DESC');
                 if ($type == 'node') {
                     $query->propertyCondition('status', 1);
                 }
                 // Additional conditions for node based translations.
                 global $language;
                 if ($language->language != NULL && $type == 'node') {
                     $query->propertyCondition('language', $language->language);
                     $query->propertyCondition('tnid', 0, "<>");
                 }
                 $results[$bundle] = $query->execute();
                 // For nodes using field based translation.
                 if ($language->language != NULL && $type == 'node') {
                     $query = new EntityFieldQuery();
                     $query->entityCondition('entity_type', $type);
                     $query->entityCondition('bundle', $bundle);
                     $query->propertyOrderBy('created', 'DESC');
                     $query->propertyCondition('tnid', 0);
                     $query->propertyCondition('status', 1);
                     $field_translated = $query->execute();
                     // Reassign the result array or merge arrays if necessary
                     if (empty($results[$bundle][$type]) && !empty($field_translated[$type])) {
                         $results[$bundle][$type] = $field_translated[$type];
                     } elseif (!empty($results[$bundle][$type]) && !empty($field_translated[$type])) {
                         $combined = $results[$bundle][$type] + $field_translated[$type];
                         ksort($combined);
                         $results[$bundle][$type] = $combined;
                     }
                 }
                 // Store the results in an aggregated array of entities.
                 if (isset($results[$bundle][$bean->settings['entity_type']])) {
                     foreach ($results[$bundle][$bean->settings['entity_type']] as $id => $result) {
                         $aggregate[$bean->settings['entity_type']][$id] = $result;
                     }
                 }
             }
             // Create a taxonomy related "score" for each result's matching terms.
             $result = array();
             $unmatching = array();
             if (isset($aggregate[$bean->settings['entity_type']])) {
                 foreach ($aggregate[$bean->settings['entity_type']] as $key => $value) {
                     $entity_terms = bean_tax_get_entity_terms($bean->settings['entity_type'], $key);
                     $score = 0;
                     // The actual scoring to determine valid taxonomy term matching.
                     foreach ($entity_terms as $term) {
                         if (isset($valid_tid[$term->tid])) {
                             $score++;
                         }
                     }
                     $item['id'] = $key;
                     $item['score'] = $score;
                     // A score of 1 or greater adds to the array of matching entities.
                     if ($score != 0) {
                         $result[] = $item;
                     } elseif ($score == 0 && $bean->settings['unmatch_add']) {
                         $result[] = $item;
                     }
                 }
             }
             // Calculate an overall score.
             $all = 0;
             foreach ($result as $item) {
                 $all = $item['score'] + $all;
             }
             // If overall score is none, do sort.
             if ($all != 0) {
                 // Invoke comparison function to determine highest ranked results.
                 usort($result, "bean_tax_cmp");
             }
         }
         // Remove active page from results.
         if (!empty($result)) {
             foreach ($result as $key => $entity) {
                 $active_page = bean_tax_active_entity_array('page');
                 if (isset($active_page['ids']) && $active_page['ids'][0] == $entity['id'] && $active_page['type'] == $bean->settings['entity_type']) {
                     unset($result[$key]);
                 }
             }
         }
         // Related entities initially set to none.
         if (empty($result)) {
             // Hide block when result is empty and 'hide_empty' option is checked.
             if ($bean->settings['hide_empty'] || !$active_entity['object']) {
                 return;
             }
             // There are no related nodes. Set Empty array for theme output.
             $content['#markup'] = t('No Results');
         } elseif (isset($active_entity['type']) && $active_entity['type'] == 'bean' && $bean->bid === $active_entity['object']->bid) {
             $content['#markup'] = '';
         } else {
             // Start counting results at index of 0.
             $i = 0;
             // Set and index for actual results shown.
             $shown = 0;
             // Set markup index as empty.
             $content['#markup'] = '';
             // Load and render the related entities.
             foreach ($result as $entity) {
                 if (isset($entity['id']) && $shown < $bean->filters['records_shown'] && $i >= $bean->filters['offset_results']) {
                     $entity = entity_load_single($bean->settings['entity_type'], $entity['id']);
                     $entity_view = entity_view($bean->settings['entity_type'], array($entity), $bean->settings['entity_view_mode']);
                     $content['#markup'] .= drupal_render($entity_view);
                     $shown++;
                 }
                 // Count continues along...
                 $i++;
             }
         }
     }
     if (!empty($bean->more_link['text']) && !empty($bean->more_link['path'])) {
         // Invoke the theme function to show the additional information "more link"
         $content['#markup'] .= theme('bean_tax_more_link', array('text' => $bean->more_link['text'], 'path' => $bean->more_link['path']));
     }
     return $content;
 }