Skip to content
Snippets Groups Projects
Commit c57d2a56 authored by Kevin Paxman's avatar Kevin Paxman
Browse files

Merge branch 'feature/ISTWCMS-4704-ebremner-layout-header-footer' into '1.0.x'

Feature/istwcms 4704 ebremner layout header footer

See merge request !107
parents bd857ba9 df593082
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,9 @@ 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;
use Drupal\simplify_menu\MenuItems;
use Drupal\path_alias\AliasManager;
......@@ -473,4 +476,149 @@ class UWService implements UWServiceInterface {
}
}
/**
* {@inheritDoc}
*/
public function uwGetHeaderData(Node $node): array {
// Array to hold the header data, need to set to
// null in case there are no header data to be
// returned.
$header_data = [];
// 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->uwGetTermsFromEntityField($node->get('field_uw_audience')),
'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->uwGetTermsFromEntityField($node->get('field_uw_blog_tags'), '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 = $this->entityTypeManager->getStorage('taxonomy_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;
}
/**
* {@inheritDoc}
*/
public function uwGetTermsFromEntityField(EntityReferenceFieldItemListInterface $values, 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 values which is a
// list of referenced taxonomy terms.
foreach ($values as $term_ref) {
// Get the term entity, which is the same as
// loading the term.
$term_entity = $term_ref->entity;
// If there is an entity, set the variable.
if ($term_entity) {
// If this is a tags term type, we have to include
// url and title. If not just the tag name.
if ($type === 'tags') {
// Set the variables. By default function
// toUrl on entity uses canonical url.
$terms[] = [
'title' => $term_entity->getName(),
'url' => $term_entity->toUrl()->toString(),
];
}
else {
$terms[] = $term_entity->getName();
}
}
}
// Return an array of term names.
return $terms;
}
}
......@@ -3,6 +3,8 @@
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;
/**
......@@ -167,4 +169,54 @@ interface UWServiceInterface {
*/
public function uwMonthNameShort(int $month = NULL);
/**
* A function to get the header data for a node/content type.
*
* @param \Drupal\node\Entity\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 \Drupal\node\Entity\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;
/**
* 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