You are here

function _datalayer_get_entity_terms in dataLayer 7

Same name and namespace in other branches
  1. 8 datalayer.module \_datalayer_get_entity_terms()

Fetch all taxonomy terms from an entity.

All fields of field type "taxonomy_term_reference" will be included. Idea found at https://api.drupal.org/comment/50393#comment-50393

Parameters

string $entity_type: What type of entity.

string $bundle: What bundle within entity type.

object $entity: Actual entity object to process.

Return value

array Array with tids of entity.

3 calls to _datalayer_get_entity_terms()
DataLayerUnitTests::testDataLayerGetEntityTermsReturnsEmptyArray in tests/datalayer.unit.test
Test DataLayer Get Entity Terms Returns Empty Array.
DataLayerUnitTests::testDataLayerGetEntityTermsReturnsTermArray in tests/datalayer.unit.test
Test DataLayer Get Entity Terms Returns Term Array.
_datalayer_get_entity_data in ./datalayer.module
Collect entity data for output and altering.

File

./datalayer.module, line 431
Client-side data space.

Code

function _datalayer_get_entity_terms($entity_type, $bundle, $entity) {
  $terms = array();

  // Use very lightweight field info list to find relevant fields.
  foreach (field_info_field_map() as $field_name => $field_info) {
    if ($field_info['type'] != "taxonomy_term_reference") {
      continue;
    }
    if (array_key_exists($entity_type, $field_info['bundles'])) {
      if (in_array($bundle, $field_info['bundles'][$entity_type])) {
        if (isset($entity->{$field_name})) {

          // Collect terms from fields for return.
          $values = field_get_items($entity_type, $entity, $field_name);
          foreach ((array) $values as $term_array) {

            // Limit to existant tids.
            if (isset($term_array['tid'])) {
              $term = taxonomy_term_load($term_array['tid']);
              if (isset($term->tid) && isset($term->name)) {
                $terms[$term->vocabulary_machine_name][$term->tid] = $term->name;
              }
            }
          }
        }
      }
    }
  }
  return $terms;
}