You are here

function _datalayer_get_entity_terms in dataLayer 8

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

Fetch all taxonomy terms from an entity.

All entity reference fields targeting taxonomy terms will be included.

Parameters

object $entity: Actual entity object to process.

Return value

array Array with tids of entity.

3 calls to _datalayer_get_entity_terms()
DataLayerKernelTest::testDataLayerGetEntityTermsReturnsEmptyArray in tests/src/Kernel/DataLayerKernelTest.php
Test DataLayer Get Entity Terms Returns Empty Array.
DataLayerKernelTest::testDataLayerGetEntityTermsReturnsTermArray in tests/src/Kernel/DataLayerKernelTest.php
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 447
Client-side data space.

Code

function _datalayer_get_entity_terms($entity) {
  $terms = [];

  // Use very lightweight field info list to find relevant fields.
  foreach ($entity
    ->getFieldDefinitions() as $field_name => $field_info) {
    if ($field_info
      ->getType() != "entity_reference" || $field_info
      ->getSetting('target_type') != 'taxonomy_term') {
      continue;
    }

    // Collect terms from fields for return.
    foreach ($entity->{$field_name}
      ->getValue() as $value) {
      if (isset($value['target_id'])) {
        $term = Term::load($value['target_id']);
        if ($term) {
          $terms[$term->vid
            ->getString()][(string) $term->tid
            ->getString()] = $term
            ->label();
        }
      }
    }
  }
  return $terms;
}