Skip to content
Snippets Groups Projects
Commit 2e382921 authored by Igor Biki's avatar Igor Biki Committed by Kevin Paxman
Browse files

ISTWCMS-4704: Adding new way to load terms from a node. Expanding uw service.

parent 25ad68fb
No related branches found
No related tags found
2 merge requests!111Tag 1.0.1,!107Feature/istwcms 4704 ebremner layout header footer
...@@ -5,6 +5,7 @@ namespace Drupal\uw_cfg_common\Service; ...@@ -5,6 +5,7 @@ namespace Drupal\uw_cfg_common\Service;
use Drupal\Core\Database\Connection; use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\node\Entity\Node; use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
...@@ -506,7 +507,7 @@ class UWService implements UWServiceInterface { ...@@ -506,7 +507,7 @@ class UWService implements UWServiceInterface {
$header_data = [ $header_data = [
'date' => date('l, F j, Y', strtotime($node->field_uw_blog_date->value)), 'date' => date('l, F j, Y', strtotime($node->field_uw_blog_date->value)),
'author' => $author, 'author' => $author,
'audiences' => $this->uwGetTerms($node->field_uw_audience->getValue()), 'audiences' => $this->uwGetTermsFromEntityField($node->get('field_uw_audience')),
'title' => $node->getTitle(), 'title' => $node->getTitle(),
]; ];
break; break;
...@@ -531,8 +532,7 @@ class UWService implements UWServiceInterface { ...@@ -531,8 +532,7 @@ class UWService implements UWServiceInterface {
case 'uw_ct_blog'; case 'uw_ct_blog';
$footer_data = [ $footer_data = [
'tags' => [ 'tags' => [
$this->uwGetTerms($node->get('field_uw_blog_tags')->getValue(), $this->uwGetTermsFromEntityField($node->get('field_uw_blog_tags'), 'tags'),
'tags'),
], ],
]; ];
break; break;
...@@ -579,4 +579,32 @@ class UWService implements UWServiceInterface { ...@@ -579,4 +579,32 @@ class UWService implements UWServiceInterface {
return $terms; 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;
}
} }
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Drupal\uw_cfg_common\Service; namespace Drupal\uw_cfg_common\Service;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\node\Entity\Node; use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
...@@ -203,4 +204,19 @@ interface UWServiceInterface { ...@@ -203,4 +204,19 @@ interface UWServiceInterface {
*/ */
public function uwGetTerms(array $tids, string $type = NULL): array; 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;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment