You are here

public function BlogapiCommunicator::getTaxonomyTerms in Blog API 8

Helper method to get terms from taxonomy fields on a node.

Parameters

$node: A node object.

$taxonomy_fields: An array of taxonomy field IDs.

Return value

array An array of taxonomy terms.

1 call to BlogapiCommunicator::getTaxonomyTerms()
BlogapiCommunicator::getNodeCategories in src/BlogapiCommunicator.php
Returns taxonomy terms saved in the defined taxonomy field on a node.

File

src/BlogapiCommunicator.php, line 205

Class

BlogapiCommunicator
Class BlogapiCommunicator.

Namespace

Drupal\blogapi

Code

public function getTaxonomyTerms($node, $taxonomy_fields) {
  $terms = [];
  if (is_array($taxonomy_fields) && !empty($taxonomy_fields)) {
    foreach ($taxonomy_fields as $field) {
      $field_load = $node
        ->get($field);
      $values = $field_load
        ->getValue();
      if (!empty($values)) {
        foreach ($values as $term) {
          $term_load = Term::load($term['target_id']);
          $terms[] = [
            'categoryName' => $term_load
              ->label(),
            'categoryId' => (int) $term_load
              ->id(),
            'isPrimary' => TRUE,
          ];
        }
      }
    }
  }
  return $terms;
}