diff --git a/src/Service/UWService.php b/src/Service/UWService.php index f986c8538b24633410a8af76eaea32f0069d2b1e..cee85ba2e615197a1f1ce25a294d6cfd497b2da8 100644 --- a/src/Service/UWService.php +++ b/src/Service/UWService.php @@ -5,9 +5,12 @@ namespace Drupal\uw_cfg_common\Service; use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Url; +use Drupal\node\Entity\Node; use Drupal\node\NodeInterface; use Drupal\simplify_menu\MenuItems; use Drupal\path_alias\AliasManager; +use Drupal\taxonomy\Entity\Term; /** * Class UWService. @@ -473,4 +476,100 @@ class UWService implements UWServiceInterface { } } + /** + * {@inheritDoc} + */ + public function uwGetHeaderData(Node $node): array { + + // Get the header data, depending on the content type. + switch ($node->getType()) { + + case 'uw_ct_blog'; + + // Get the author field from the node, if there is + // no author specified the value will be NULL. + $author = $node->field_author->value; + + // If there is no author in the field, get the last + // person to revise teh blog post. + if (!$author) { + + // Set the author to the person who made blog. + $author = $node->getOwner()->getDisplayName(); + } + + // Set the header data. + $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()), + 'title' => $node->getTitle(), + ]; + break; + } + + // Return the header data. + return $header_data; + } + + /** + * {@inheritDoc} + */ + public function uwGetFooterData(Node $node): array { + + // Array to hold the footer data, need to set to + // null in case there are no footer data to be + // returned. + $footer_data = []; + + // Get the footer data, depending on the content type. + switch ($node->getType()) { + case 'uw_ct_blog'; + $footer_data = [ + 'tags' => $this->uwGetTerms($node->get('field_uw_blog_tags')->getValue(), 'tags'), + ]; + break; + } + + // Return the header data. + return $footer_data; + } + + /** + * {@inheritDoc} + */ + public function uwGetTerms(array $tids, string $type = NULL): array { + + // Array to hold the terms, need to set to + // null in case there are no terms to be + // returned. + $terms = []; + + // Step through each of the tids and get the term name. + foreach ($tids as $tid) { + + // Load the term. + $term = Term::load($tid['target_id']); + + // If this is a tags term type, we have to include + // url and title. If not just the tag name. + if ($type == 'tags') { + + $terms[] = [ + 'title' => $term->getName(), + 'url' => Url::fromRoute( + 'entity.taxonomy_term.canonical', + ['taxonomy_term' => $tid['target_id']] + )->toString(), + ]; + } + else { + $terms[] = $term->getName(); + } + } + + // Return an array of term names. + return $terms; + } + } diff --git a/src/Service/UWServiceInterface.php b/src/Service/UWServiceInterface.php index e61893790b51b22049b61ec9f92716df5bcb6e93..8ef04ad1b22806dedc328e15f052a248ddd89f4f 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\node\Entity\Node; use Drupal\node\NodeInterface; /** @@ -167,4 +168,38 @@ interface UWServiceInterface { */ public function uwMonthNameShort(int $month = NULL); + /** + * A function to get the header data for a node/content type. + * + * @param Node $node + * The node object. + * + * @return array + * An array containing the header data for a node/content type. + */ + public function uwGetHeaderData(Node $node): array; + + /** + * A function to get the footer data for a node/content type. + * + * @param Node $node + * The node object. + * + * @return array + * An array containing the footer data for a node/content type. + */ + public function uwGetFooterData(Node $node): array; + + /** + * A function get the taxonomy terms. + * + * @param array $tids + * An array of term ids (tids). + * @param string $type + * The type of terms to get, if none provided just term name returned. + * + * @return array + * An array of terms with name and link. + */ + public function uwGetTerms(array $tids, string $type = NULL): array; }