From 2e3829210259a66e46da42b719d42e5db1ba39f8 Mon Sep 17 00:00:00 2001
From: Igor Biki <ibiki@uwaterloo.ca>
Date: Mon, 7 Jun 2021 14:11:11 -0400
Subject: [PATCH] ISTWCMS-4704: Adding new way to load terms from a node.
 Expanding uw service.

---
 src/Service/UWService.php          | 34 +++++++++++++++++++++++++++---
 src/Service/UWServiceInterface.php | 16 ++++++++++++++
 2 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/src/Service/UWService.php b/src/Service/UWService.php
index 2381912c..cdf8d256 100644
--- a/src/Service/UWService.php
+++ b/src/Service/UWService.php
@@ -5,6 +5,7 @@ namespace Drupal\uw_cfg_common\Service;
 use Drupal\Core\Database\Connection;
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
+use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
 use Drupal\Core\Url;
 use Drupal\node\Entity\Node;
 use Drupal\node\NodeInterface;
@@ -506,7 +507,7 @@ class UWService implements UWServiceInterface {
         $header_data = [
           'date' => date('l, F j, Y', strtotime($node->field_uw_blog_date->value)),
           'author' => $author,
-          'audiences' => $this->uwGetTerms($node->field_uw_audience->getValue()),
+          'audiences' => $this->uwGetTermsFromEntityField($node->get('field_uw_audience')),
           'title' => $node->getTitle(),
         ];
         break;
@@ -531,8 +532,7 @@ class UWService implements UWServiceInterface {
       case 'uw_ct_blog';
         $footer_data = [
           'tags' => [
-            $this->uwGetTerms($node->get('field_uw_blog_tags')->getValue(),
-              'tags'),
+            $this->uwGetTermsFromEntityField($node->get('field_uw_blog_tags'), 'tags'),
           ],
         ];
         break;
@@ -579,4 +579,32 @@ class UWService implements UWServiceInterface {
     return $terms;
   }
 
+  /**
+   * {@inheritDoc}
+   */
+  public function uwGetTermsFromEntityField(EntityReferenceFieldItemListInterface $values, string $type = NULL): array {
+    $terms = [];
+
+    // List of referenced taxonomy terms.
+    foreach ($values as $term_ref) {
+      /** @var \Drupal\taxonomy\Entity\Term $term_entity */
+      $term_entity = $term_ref->entity;
+
+      if ($term_entity) {
+        if ($type === 'tags') {
+          $terms[] = [
+            'title' => $term_entity->getName(),
+            // By default function toUrl on entity uses canonical url.
+            'url' => $term_entity->toUrl()->toString(),
+          ];
+        }
+        else {
+          $terms[] = $term_entity->getName();
+        }
+      }
+    }
+
+    return $terms;
+  }
+
 }
diff --git a/src/Service/UWServiceInterface.php b/src/Service/UWServiceInterface.php
index ec3a1b6c..7786e377 100644
--- a/src/Service/UWServiceInterface.php
+++ b/src/Service/UWServiceInterface.php
@@ -3,6 +3,7 @@
 namespace Drupal\uw_cfg_common\Service;
 
 use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
 use Drupal\node\Entity\Node;
 use Drupal\node\NodeInterface;
 
@@ -203,4 +204,19 @@ interface UWServiceInterface {
    */
   public function uwGetTerms(array $tids, string $type = NULL): array;
 
+  /**
+   * Function that parses terms and returns a list.
+   *
+   * @param \Drupal\Core\Field\EntityReferenceFieldItemListInterface $values
+   *   List of values for the provided field.
+   * @param string|null $type
+   *   The type of terms to get, if none provided just term name returned.
+   *
+   * @return array
+   *   List of terms with name and link.
+   *
+   * @throws \Drupal\Core\Entity\EntityMalformedException
+   */
+  public function uwGetTermsFromEntityField(EntityReferenceFieldItemListInterface $values, string $type = NULL): array;
+
 }
-- 
GitLab