diff --git a/src/Service/UWService.php b/src/Service/UWService.php index 2381912c2f25983e9cf901527fd4526bb10fad2b..cdf8d25664319a3c1d683638f4df247c3d02a8ee 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 ec3a1b6cb0268883a967dd864de056468fc3303b..7786e3779681a7495358760015c35b7cb0a7b45a 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; + }