Skip to content
Snippets Groups Projects

Feature/istwcms 4619 ebremner variables for listing page

2 files
+ 213
115
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 189
105
@@ -8,7 +8,6 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
@@ -8,7 +8,6 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
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\simplify_menu\MenuItems;
use Drupal\simplify_menu\MenuItems;
use Drupal\path_alias\AliasManager;
use Drupal\path_alias\AliasManager;
@@ -106,120 +105,189 @@ class UWService implements UWServiceInterface {
@@ -106,120 +105,189 @@ class UWService implements UWServiceInterface {
/**
/**
* {@inheritDoc}
* {@inheritDoc}
*/
*/
public function getTeaserContent(NodeInterface $node, array $variables_to_get, string $teaser_type): array {
public function uwGetNodeContent(Node $node, string $view_mode, string $content = 'all'): array {
$variables = [];
// Step through each of the variables to get and set the variables for the
// Flags for getting teaser content.
// teaser content.
$get_header = FALSE;
foreach ($variables_to_get as $variable_to_get) {
$get_footer = FALSE;
$get_image = FALSE;
// Switch on the variable to get and set the variables for the teaser
$get_content = FALSE;
// content.
switch ($variable_to_get) {
// Setup flags based on teaser content argument.
if ($content == 'all') {
case 'title':
$get_header = TRUE;
$get_footer = TRUE;
// Set the title from the node object.
$get_content = TRUE;
$variables['title'] = $node->getTitle();
break;
if ($view_mode == 'teaser') {
$get_image = TRUE;
case 'url':
}
}
// Set the title from the node object.
else {
$variables['url'] = $node->toUrl()->toString();
if ($content == 'header') {
break;
$get_header = TRUE;
}
case 'date':
// Set the field name, all of our content types use the same format,
// field_uw_<content_type>_date.
$field_name = 'field_uw_' . trim($teaser_type) . '_date';
if (trim($teaser_type) == 'event') {
$variables['date'] = $node->$field_name->getValue();
}
else {
// Set the date variable, once returned to the calling function,
// they can change the date format as required (i.e. change it
// to long-date or date-time).
$variables['date'] = $node->$field_name->getString();
}
break;
case 'author':
// If author is selected, get both the author name and link.
$variables['author_name'] = $node->getOwner()->getDisplayName();
$variables['author_link'] = $node->getOwner()->toUrl()->toString();
break;
case 'sources':
if ($content == 'footer') {
 
$get_footer = TRUE;
 
}
 
}
// Event has a different name for the image than the rest, so ensuring
// Setup the teaser data array, based on flags.
// that we get the correct field name for the image.
switch ($node->getType()) {
// In most cases it is field_uw_<content_type>_lising_page_image.
if ($teaser_type == 'event') {
$field_name = 'field_uw_event_listing_page_img';
}
else {
$field_name = 'field_uw_' . trim($teaser_type) . '_listing_page_image';
}
// Get the image object from the node.
case 'uw_ct_blog':
$image = $node->$field_name->entity;
// Ensure that we have an image before adding variables.
// The list of tags for blogs.
if ($image !== NULL) {
$tag_list = [
 
'field_uw_blog_tags',
 
'field_uw_audience',
 
];
// Get all the image variables, including the sources using the
$content_data = [
// prepareResponsiveImage function.
'title' => $get_header ? TRUE : NULL,
$image_variables = $this->prepareResponsiveImage($image, 'uw_ris_media');
'url' => TRUE,
 
'date' => $get_header ? 'field_uw_blog_date' : NULL,
 
'author' => $get_header ? TRUE : NULL,
 
'sources' => $get_image ? 'field_uw_blog_listing_page_image' : NULL,
 
'content' => $get_content ? 'field_uw_blog_summary' : NULL,
 
'tags' => $get_footer ? $tag_list : NULL,
 
];
 
break;
// Set the responsive image variables for the teaser content.
case 'uw_ct_event':
$variables['sources'] = $image_variables['responsive_sources'];
$variables['img_element'] = $image_variables['img_element']['#uri'];
$variables['alt'] = $image->field_media_image->alt;
}
break;
case 'tags':
// The list of tags for events.
 
$tag_list = [
 
'field_uw_event_tags',
 
'field_uw_audience',
 
'field_uw_event_type',
 
];
// Set the field name for the tags which is
$content_data = [
// field_uw_<content_type>_tags.
'title' => $get_header ? TRUE : NULL,
$field_name = 'field_uw_' . trim($teaser_type) . '_tags';
'url' => TRUE,
 
'date' => $get_header ? 'field_uw_event_date' : NULL,
 
'sources' => $get_image ? 'field_uw_event_listing_page_img' : NULL,
 
'content' => $get_content ? 'field_uw_event_summary' : NULL,
 
'tags' => $get_footer ? $tag_list : NULL,
 
];
 
break;
// Get all the taxonomy terms for the blog.
case 'uw_ct_news_item':
// Step through each taxonomy term and get the tag info.
foreach ($node->$field_name as $tag) {
// Set the tags in the teaser content variables.
// The list of tags for news.
$variables['tags'][] = [
$tag_list = [
'title' => $tag->entity->name->value,
'field_uw_news_tags',
'url' => $tag->entity->toUrl()->toString(),
'field_uw_audience',
];
];
}
break;
case 'content':
$content_data = [
 
'title' => $get_header ? TRUE : NULL,
 
'url' => TRUE,
 
'date' => $get_header ? 'field_uw_news_date' : NULL,
 
'sources' => $get_image ? 'field_uw_news_listing_page_image' : NULL,
 
'content' => $get_content ? 'field_uw_news_summary' : NULL,
 
'tags' => $get_footer ? $tag_list : NULL,
 
];
 
break;
 
}
// Set the field name for the summary, which is
return $this->uwGetNodeData($node, $view_mode, $content_data);
// field_name<content_type>_summary.
}
$field_name = 'field_uw_' . $teaser_type . '_summary';
// Set the render array for the summary, we simply can not just send
/**
// the value as Drupal will just render it with no filters or HTML
* {@inheritDoc}
// applied. We need to send the full render array.
*/
$variables['content'] = [
public function uwGetNodeData(Node $node, string $view_mode, array $content_data): array {
'#type' => 'processed_text',
'#text' => $node->$field_name->value,
// Array to store the teaser data, need blank
'#format' => $node->$field_name->format,
// array in case there is no data to return.
];
$node_data = [];
break;
 
// Step through each of the teaser data, and if
 
// we are to get the data then set the variable
 
// inside the teaser array.
 
foreach ($content_data as $index => $data) {
 
 
// If there is data to get, then get it.
 
if ($data) {
 
 
// Switch on the index to get the proper value.
 
// The index will either be true (like title) or
 
// a field name.
 
switch ($index) {
 
 
case 'title':
 
$node_data['title'] = $node->getTitle();
 
break;
 
 
case 'author':
 
$node_data['author'] = $this->uwGetAuthor($node);
 
break;
 
 
case 'date':
 
if ($view_mode == 'teaser') {
 
if ($node->getType() !== 'events') {
 
$node_data['date'] = date('l, F j, Y', strtotime($node->$data->value));
 
}
 
else {
 
 
// Get all the dates.
 
// @todo figure out what date to display for events.
 
$dates = $node->$data->getValue();
 
 
$node_data['date'] = date('l, F j, Y', $dates[0]['value']);
 
}
 
}
 
break;
 
 
case 'sources':
 
 
// Get the image entity.
 
$image = $node->$data->entity;
 
 
// If there is an image, get the responsive image sources.
 
if ($image) {
 
$sources = $this->prepareResponsiveImage($image, 'uw_ris_media');
 
}
 
else {
 
$sources = NULL;
 
}
 
 
if (isset($sources['responsive_sources'])) {
 
$node_data['image']['sources'] = $sources['sources'];
 
$node_data['image']['img_element'] = $sources['img_element']['#uri'];
 
$node_data['image']['alt'] = $sources['alt'];
 
}
 
break;
 
 
case 'content':
 
if ($view_mode == 'teaser') {
 
$node_data['content'] = [
 
'#type' => 'processed_text',
 
'#text' => $node->$data->value,
 
'#format' => $node->$data->format,
 
];
 
}
 
break;
 
 
case 'tags':
 
$tags = [];
 
foreach ($data as $field) {
 
$tags = array_merge($tags, $this->uwGetTermsFromEntityField($node->$field, 'tags'));
 
}
 
$node_data['tags'] = [$tags];
 
break;
 
 
case 'url':
 
$node_data['url'] = $node->toUrl()->toString();
 
break;
 
}
}
}
}
}
return $variables;
return $node_data;
}
}
/**
/**
@@ -577,18 +645,34 @@ class UWService implements UWServiceInterface {
@@ -577,18 +645,34 @@ class UWService implements UWServiceInterface {
/**
/**
* {@inheritDoc}
* {@inheritDoc}
*/
*/
public function uwGetAuthor(Node $node): string {
public function uwGetAuthor(Node $node): array {
// Get the author field from the node, if there is
// Get the author field from the node, if there is
// no author specified the value will be NULL.
// no author specified the value will be NULL.
$author = $node->field_author->value;
$author_name = $node->field_author->value;
// If there is no author in the field, get the last
// If there is no author in the field, get the owner
// person to revise the blog post.
// of the blog post.
if (!$author) {
if (!$author_name) {
// Set the author to the person who made blog.
// Set the author to the person who made blog.
$author = $node->getOwner()->getDisplayName();
$author = [
 
'name' => $node->getOwner()->getDisplayName(),
 
'link' => NULL,
 
];
 
}
 
 
// If there is an author, get the author name and link.
 
else {
 
 
// Get the link field from the node.
 
$link = $node->field_uw_author_link->getValue();
 
 
// Set the author name and link (if any).
 
$author = [
 
'name' => $author_name,
 
'link' => empty($link) ? NULL : $link[0]['uri'],
 
];
}
}
return $author;
return $author;
Loading