You are here

function tac_lite_node_get_terms in Taxonomy Access Control Lite 8

Same name and namespace in other branches
  1. 7 tac_lite.module \tac_lite_node_get_terms()

We organize our data structure by vid and tid.

1 call to tac_lite_node_get_terms()
_tac_lite_get_terms in ./tac_lite.module
Gets terms from a node that belong to vocabularies selected.

File

./tac_lite.module, line 122
Control access to site content based on taxonomy, roles and users.

Code

function tac_lite_node_get_terms($node) {
  $terms =& drupal_static(__FUNCTION__);
  $nid = $node
    ->id();
  if (!isset($terms[$nid])) {

    // Get fields of all node.
    $fields = \Drupal::service('entity_field.manager')
      ->getFieldDefinitions('node', $node
      ->getType());

    // Get tids from all taxonomy_term_reference fields.
    foreach ($fields as $field_name => $field) {
      $field_type = method_exists($field, 'getType') ? $field
        ->getType() : NULL;
      $target_type = method_exists($field, 'getSetting') ? $field
        ->getSetting('target_type') : NULL;

      // Get all terms, regardless of language, associated with the node.
      if ($field_type == 'entity_reference' && $target_type == 'taxonomy_term') {
        $field_name = $field
          ->get('field_name');
        if ($items = $node
          ->get($field_name)
          ->getValue()) {
          foreach ($items as $item) {

            // We need to term to determine the vocabulary id.
            if (!empty($item['target_id'])) {
              $term = Term::load($item['target_id']);
            }
            if ($term) {
              $terms[$node
                ->id()][$term
                ->getVocabularyId()][$term
                ->id()] = $term;
            }
          }
        }
      }
    }
  }
  return isset($terms[$node
    ->id()]) ? $terms[$node
    ->id()] : FALSE;
}