Skip to content
Snippets Groups Projects
Commit 973222bf authored by Eric Bremner's avatar Eric Bremner Committed by Eric Bremner
Browse files

ISTWCMS-4619: refactoring get node content to work with all view modes

parent 0452c547
No related branches found
No related tags found
2 merge requests!111Tag 1.0.1,!110Feature/istwcms 4619 ebremner variables for listing page
......@@ -105,7 +105,7 @@ class UWService implements UWServiceInterface {
/**
* {@inheritDoc}
*/
public function getTeaserContent(Node $node, string $teaser_type, string $teaser_content = 'all'): array {
public function uwGetNodeContent(Node $node, string $content_type, string $view_mode, string $content = 'all'): array {
// Flags for getting teaser content.
$get_header = FALSE;
......@@ -114,24 +114,27 @@ class UWService implements UWServiceInterface {
$get_content = FALSE;
// Setup flags based on teaser content argument.
if ($teaser_content == 'all') {
if ($content == 'all') {
$get_header = TRUE;
$get_footer = TRUE;
$get_image = TRUE;
$get_content = TRUE;
if ($view_mode == 'teaser') {
$get_image = TRUE;
}
}
else {
if ($teaser_content == 'header') {
if ($content == 'header') {
$get_header = TRUE;
}
if ($teaser_content == 'footer') {
if ($content == 'footer') {
$get_footer = TRUE;
}
}
// Setup the teaser data array, based on flags.
switch ($teaser_type) {
switch ($content_type) {
case 'blog':
......@@ -141,7 +144,7 @@ class UWService implements UWServiceInterface {
'field_uw_audience',
];
$teaser_data = [
$content_data = [
'title' => $get_header ? TRUE : NULL,
'url' => TRUE,
'date' => $get_header ? 'field_uw_blog_date' : NULL,
......@@ -161,7 +164,7 @@ class UWService implements UWServiceInterface {
'field_uw_event_type',
];
$teaser_data = [
$content_data = [
'title' => $get_header ? TRUE : NULL,
'url' => TRUE,
'date' => $get_header ? 'field_uw_event_date' : NULL,
......@@ -179,7 +182,7 @@ class UWService implements UWServiceInterface {
'field_uw_audience',
];
$teaser_data = [
$content_data = [
'title' => $get_header ? TRUE : NULL,
'url' => TRUE,
'date' => $get_header ? 'field_uw_news_date' : NULL,
......@@ -190,22 +193,22 @@ class UWService implements UWServiceInterface {
break;
}
return $this->getTeaserData($node, $teaser_type, $teaser_data);
return $this->uwGetNodeData($node, $content_type, $view_mode, $content_data);
}
/**
* {@inheritDoc}
*/
public function getTeaserData(Node $node, string $teaser_type, array $teaser_data): array {
public function uwGetNodeData(Node $node, string $content_type, string $view_mode, array $content_data): array {
// Array to store the teaser data, need blank
// array in case there is no data to return.
$teaser = [];
$node_data = [];
// Step through each of the teaser data, and if
// we are to get the data then set the variable
// inside the teaser array.
foreach ($teaser_data as $index => $data) {
foreach ($content_data as $index => $data) {
// If there is data to get, then get it.
if ($data) {
......@@ -216,24 +219,25 @@ class UWService implements UWServiceInterface {
switch ($index) {
case 'title':
$teaser['title'] = $node->getTitle();
$node_data['title'] = $node->getTitle();
break;
case 'author':
$teaser['author'] = $this->uwGetAuthor($node);
$node_data['author'] = $this->uwGetAuthor($node);
break;
case 'date':
if ($teaser_type !== 'event') {
$teaser['date'] = date('l, F j, Y', strtotime($node->$data->value));
}
else {
if ($view_mode == 'teaser') {
if ($content_type !== '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();
// Get all the dates.
// @todo figure out what date to display for events.
$dates = $node->$data->getValue();
$teaser['date'] = date('l, F j, Y', $dates[0]['value']);
$node_data['date'] = date('l, F j, Y', $dates[0]['value']);
}
}
break;
......@@ -251,18 +255,20 @@ class UWService implements UWServiceInterface {
}
if (isset($sources['responsive_sources'])) {
$teaser['image']['sources'] = $sources['sources'];
$teaser['image']['img_element'] = $sources['img_element']['#uri'];
$teaser['image']['alt'] = $sources['alt'];
$node_data['image']['sources'] = $sources['sources'];
$node_data['image']['img_element'] = $sources['img_element']['#uri'];
$node_data['image']['alt'] = $sources['alt'];
}
break;
case 'content':
$teaser['content'] = [
'#type' => 'processed_text',
'#text' => $node->$data->value,
'#format' => $node->$data->format,
];
if ($view_mode == 'teaser') {
$node_data['content'] = [
'#type' => 'processed_text',
'#text' => $node->$data->value,
'#format' => $node->$data->format,
];
}
break;
case 'tags':
......@@ -270,17 +276,17 @@ class UWService implements UWServiceInterface {
foreach ($data as $field) {
$tags = array_merge($tags, $this->uwGetTermsFromEntityField($node->$field, 'tags'));
}
$teaser['tags'] = [$tags];
$node_data['tags'] = [$tags];
break;
case 'url':
$teaser['url'] = $node->toUrl()->toString();
$node_data['url'] = $node->toUrl()->toString();
break;
}
}
}
return $teaser;
return $node_data;
}
/**
......
......@@ -34,32 +34,34 @@ interface UWServiceInterface {
*
* @param \Drupal\node\NodeInterface $node
* Node entity.
* @param array $variables_to_get
* List of variables to return.
* @param string $teaser_type
* Teaser type.
* @param string $teaser_content
* @param string $content_type
* The content type (i.e. news, event, blog, etc).
* @param string $view_mode
* The view mode (i.e. node, teaser, etc).
* @param string $content
* The type of content to get, values are all, header or footer.
*
* @return array
* Array of variables and their values.
*/
public function getTeaserContent(Node $node, string $teaser_type, string $teaser_content = 'all'): array;
public function uwGetNodeContent(Node $node, string $content_type, string $view_mode, string $content = 'all'): array;
/**
* Gets teaser data.
*
* @param \Drupal\node\NodeInterface $node
* Node entity.
* @param string $teaser_type
* The type of teaser (i.e. blog, events, news, etc).
* @param array $teaser_data
* @param string $content_type
* The content type (i.e. news, event, blog, etc).
* @param string $view_mode
* The view mode (i.e. node, teaser, etc).
* @param array $content_data
* An array of all the data to get for the teaser.
*
* @return array
* Array of teaser values.
* Array of node values.
*/
public function getTeaserData(Node $node, string $teaser_type, array $teaser_data): array;
public function uwGetNodeData(Node $node, string $content_type, string $view_mode, array $content_data): array;
/**
* A function to get or check the attached sidebar.
......
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