/**
  * Displays the bean.
  */
 public function view($bean, $content, $view_mode = 'default', $langcode = NULL)
 {
     // Retrieve the terms from the loaded entity.
     $active_entity = bean_tax_active_entity_array();
     // Check for cached content on this block.
     $cache_name = 'bean_tax:listing:' . $bean->delta . ':' . $active_entity['type'] . ':' . $active_entity['ids'][0];
     if ($cache = cache_get($cache_name)) {
         $content = $cache->data;
     } else {
         // We need to make sure that the bean is configured correctly.
         if ($active_entity['type'] != 'bean' && !empty($bean->filters['vocabulary']) && (isset($active_entity['terms']) && count($active_entity['terms']))) {
             // Reformat vocabulary list from machine names to vocabulary vids.
             $vids = array();
             foreach ($bean->filters['vocabulary'] as $vm) {
                 $query = new EntityFieldQuery();
                 $result = $query->entityCondition('entity_type', 'taxonomy_vocabulary');
                 $query->propertyCondition('machine_name', $vm);
                 global $language;
                 if ($language->language != NULL && db_field_exists('taxonomy_vocabulary', 'language')) {
                     $query->propertyCondition('language', $language->language);
                 }
                 $result = $query->execute();
                 foreach ($result['taxonomy_vocabulary'] as $vocabulary) {
                     $vids[$vocabulary->vid] = $vocabulary->vid;
                 }
             }
             $i = 0;
             $content['terms'] = array();
             // Parse terms from correct vocabularies, limit list to X results.
             foreach ($active_entity['terms'] as $term) {
                 $term = entity_load_single('taxonomy_term', $term->tid);
                 if (in_array($term->vid, $vids) && $i < $bean->settings['records_shown']) {
                     $content['terms'][$term->tid] = entity_view('taxonomy_term', array($term->tid => $term), $bean->settings['term_view_mode']);
                     $i++;
                 }
             }
             cache_set($cache_name, $content, 'cache', time() + 60 * $bean->settings['cache_duration']);
         } elseif (isset($active_entity['type']) && $active_entity['type'] == 'bean' && $bean->bid === $active_entity['object']->bid) {
             $content['#markup'] = '';
         } elseif ($bean->settings['hide_empty'] || !$active_entity['object']) {
             return;
         } else {
             $content['#markup'] = t('No terms.');
         }
     }
     return $content;
 }
 /**
  * Displays the bean.
  */
 public function view($bean, $content, $view_mode = 'default', $langcode = NULL)
 {
     // Return the active entity information.
     $active_entity = bean_tax_active_entity_array($bean->settings['related']);
     // Create a unique id to be used by the cache.
     if (isset($active_entity['type']) && !empty($active_entity['ids'])) {
         // Determine user role and append to cache.
         if ($active_entity['type'] != 'user') {
             // Grab the highest rid attached to the user.
             global $user;
             $key = max(array_keys($user->roles));
             // Use active entity type, entity id and max role to determine cache id.
             $cid = $active_entity['type'] . ':' . $active_entity['ids'][0] . ':' . $key;
         } else {
             // Use active entity type and entity id to determine cache id.
             $cid = $active_entity['type'] . ':' . $active_entity['ids'][0];
         }
     } else {
         // Create a generic cache id for use otherwise.
         $cid = date('Y:m:d:i');
     }
     // Append language prefix to end of cache id.
     global $language;
     if ($language->prefix != '') {
         $cid = $cid . ':' . $language->prefix;
     }
     // Set the cache name.
     $cache_name = 'bean_tax:related:' . $bean->delta . ':' . $cid;
     // Check for cached content.
     if ($cache = cache_get($cache_name)) {
         $content = $cache->data;
     } else {
         // We need to make sure that the bean is configured correctly.
         if (!empty($active_entity) && !empty($bean->filters['vocabulary']) && !empty($bean->settings['bundle_types'])) {
             // Determine a list of possible terms based on the set vocabulary.
             $possible_tid = $this->getPossibleTerms($bean);
             // Return a list of valid term ids based on the terms attached to the
             // active entity object.
             $valid_tid = array();
             if (isset($active_entity['terms'])) {
                 $this->getValidTerms($active_entity['terms'], $possible_tid, $valid_tid);
             }
             // Use EFQ to return all possible related entites.
             $aggregate = $this->getAggregate($bean);
             // Score and sort any valid results.
             $result = $this->scoreResults($bean, $aggregate, $valid_tid);
             // Related entities initially set to none.
             if (empty($result)) {
                 // Hide block when result is empty and 'hide_empty' option is checked.
                 if (isset($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 {
                 // If all else fails, we really must have something to show people.
                 $content['#markup'] = $this->returnMarkup($bean, $result);
                 // Cache the bean where appropriate.
                 if (isset($bean->settings['cache_duration']) && isset($bean->settings['cache_auth_user']) && isset($bean->settings['cache_anon_user'])) {
                     $cache_bean = TRUE;
                     // Check if authenticated user caching is turned off.
                     if ($bean->settings['related'] == 'user' && !$bean->settings['cache_auth_user']) {
                         $cache_bean = FALSE;
                     }
                     // Anonymous user check.
                     $anon = user_is_anonymous();
                     // Check if anonymous user caching is turned off.
                     if ($bean->settings['related'] == 'user' && !$bean->settings['cache_anon_user'] && $anon == TRUE) {
                         $cache_bean = FALSE;
                     }
                     // Check if anonymous user caching is turned on.
                     if ($bean->settings['related'] == 'user' && $bean->settings['cache_anon_user'] && $anon == TRUE) {
                         $cache_bean = TRUE;
                     }
                     // Finally, set the cache after all checks pass.
                     if ($cache_bean) {
                         cache_set($cache_name, $content, 'cache', time() + 60 * $bean->settings['cache_duration']);
                     }
                 }
             }
         }
         // Render the optional "more link" if provided.
         if (!empty($bean->more_link['text']) && !empty($bean->more_link['path'])) {
             $content['#markup'] .= theme('bean_tax_more_link', array('text' => $bean->more_link['text'], 'path' => $bean->more_link['path']));
         }
     }
     return $content;
 }
 /**
  * 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;
 }