Skip to content
Snippets Groups Projects
Commit 15946fe9 authored by Eric Bremner's avatar Eric Bremner Committed by Kevin Paxman
Browse files

ISTWCMS-4704: adding functions to get header and footer data for node/content types

parent bd857ba9
No related branches found
No related tags found
2 merge requests!111Tag 1.0.1,!107Feature/istwcms 4704 ebremner layout header footer
...@@ -5,9 +5,12 @@ namespace Drupal\uw_cfg_common\Service; ...@@ -5,9 +5,12 @@ 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\Url;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
use Drupal\simplify_menu\MenuItems; use Drupal\simplify_menu\MenuItems;
use Drupal\path_alias\AliasManager; use Drupal\path_alias\AliasManager;
use Drupal\taxonomy\Entity\Term;
/** /**
* Class UWService. * Class UWService.
...@@ -473,4 +476,100 @@ class UWService implements UWServiceInterface { ...@@ -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;
}
} }
...@@ -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\node\Entity\Node;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
/** /**
...@@ -167,4 +168,38 @@ interface UWServiceInterface { ...@@ -167,4 +168,38 @@ interface UWServiceInterface {
*/ */
public function uwMonthNameShort(int $month = NULL); 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;
} }
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