Newer
Older
Eric Bremner
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace Drupal\uw_cfg_common\Service;
use Drupal\node\Entity\Node;
/**
* Class UwNodeContent.
*
* Gets the content out of a node.
*
* @package Drupal\uw_cfg_common\Service
*/
class UwNodeContent {
/**
* The UW node data service.
*
* @var UwNodeData
*/
private $uwNodeData;
/**
* Default constructor.
*
* @param UwNodeData $uwNodeData
* The UW node data service.
*/
public function __construct(UwNodeData $uwNodeData) {
$this->uwNodeData = $uwNodeData;
}
/**
* Gets the content of a node.
*
* @param \Drupal\node\Entity\Node $node
* The node.
* @param string $view_mode
* The view mode.
* @param string $content
* The content to get (either layout builder or summary).
Eric Bremner
committed
*
* @return array
* Array of content to get from the node.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
Eric Bremner
committed
*/
public function getNodeContent(Node $node, string $view_mode, string $content): array {
Eric Bremner
committed
// Get the flags for the node.
$node_flags = $this->getNodeFlags($node, $view_mode, $content);
Eric Bremner
committed
Eric Bremner
committed
// Setup the node data array, based on flags.
Eric Bremner
committed
switch ($node->getType()) {
case 'uw_ct_blog':
Eric Bremner
committed
$content_data = $this->getBlogContent($node_flags);
Eric Bremner
committed
break;
case 'uw_ct_event':
$content_data = $this->getEventContent($node_flags);
break;
case 'uw_ct_news_item':
Eric Bremner
committed
$content_data = $this->getNewsContent($node_flags);
Eric Bremner
committed
break;
case 'uw_ct_web_page':
Eric Bremner
committed
$content_data = $this->getWebPageContent($node_flags);
Eric Bremner
committed
break;
case 'uw_ct_catalog_item':
Eric Bremner
committed
$content_data = $this->getCatalogItemContent($node_flags);
Eric Bremner
committed
break;
case 'uw_ct_contact':
Eric Bremner
committed
$content_data = $this->getContactContent($node_flags);
Eric Bremner
committed
break;
case 'uw_ct_profile':
Eric Bremner
committed
$content_data = $this->getProfileContent($node_flags);
Eric Bremner
committed
break;
case 'uw_ct_service':
$content_data = $this->getServiceContent($node_flags);
break;
case 'uw_ct_opportunity':
$content_data = $this->getOpportunityContent($node_flags, $view_mode);
break;
Eric Bremner
committed
}
return $this->uwNodeData->getNodeData($node, $view_mode, $content_data);
}
/**
* Get the content types that have hero images.
*
* @return string[]
* Array of content types that can have hero images.
*/
public function getHeroImageContentTypes(): array {
return [
'uw_ct_blog' => 'field_uw_hero_image',
'uw_ct_event' => 'field_uw_hero_image',
'uw_ct_news_item' => 'field_uw_hero_image',
];
}
Eric Bremner
committed
/**
* Get the flags for the node.
*
* @param \Drupal\node\Entity\Node $node
* The node.
* @param string $view_mode
* The view mode.
* @param string $content
* The content to get (layout builder or summary).
*
* @return array
* Array of flags for the node.
*/
public function getNodeFlags(Node $node, string $view_mode, string $content): array {
Eric Bremner
committed
// Flags for getting teaser content.
$node_flags['get_header'] = FALSE;
$node_flags['get_footer'] = FALSE;
$node_flags['get_image'] = FALSE;
$node_flags['get_content'] = FALSE;
$node_flags['get_media'] = FALSE;
$node_flags['get_listing_image'] = FALSE;
$node_flags['get_tags'] = TRUE;
Eric Bremner
committed
// Setup flags based on teaser content argument.
if ($content == 'all') {
$node_flags['get_header'] = TRUE;
$node_flags['get_footer'] = TRUE;
$node_flags['get_content'] = TRUE;
if ($view_mode == 'full') {
$node_flags['get_media'] = TRUE;
if ($node->getType() == 'uw_ct_contact') {
$node_flags['get_image'] = TRUE;
}
Eric Bremner
committed
}
elseif ($view_mode == 'teaser') {
if ($node->getType() !== 'uw_ct_contact') {
$node_flags['get_footer'] = FALSE;
}
$node_flags['get_image'] = TRUE;
Eric Bremner
committed
}
else {
if ($content == 'header') {
if ($node->getType() == 'uw_ct_contact') {
$node_flags['get_image'] = TRUE;
}
Eric Bremner
committed
$node_flags['get_header'] = TRUE;
$node_flags['get_title'] = TRUE;
$node_flags['get_media'] = TRUE;
$node_flags['get_tags'] = FALSE;
Eric Bremner
committed
}
if ($content == 'footer') {
$node_flags['get_footer'] = TRUE;
$node_flags['get_title'] = FALSE;
}
}
return $node_flags;
}
Eric Bremner
committed
/**
Eric Bremner
committed
* Common elements for content data.
Eric Bremner
committed
*
Eric Bremner
committed
* The flags for the node.
*
* @return array
Eric Bremner
committed
* Array of common elements for content data.
Eric Bremner
committed
*/
public function setupContentData(array $node_flags): array {
Eric Bremner
committed
Eric Bremner
committed
$content_data['url'] = [
'type' => 'url',
Eric Bremner
committed
];
$content_data['header'] = [
'has_children' => TRUE,
];
Eric Bremner
committed
if ($node_flags['get_title']) {
$content_data['header']['title'] = [
Eric Bremner
committed
'type' => 'title',
];
}
return $content_data;
}
/**
* Functiont to add to content data array.
*
* @param string $type
* The type of field.
* @param mixed $field_name
* The actual field name(s).
Eric Bremner
committed
* The label to be used with the field.
*
* @return string[]
* Array to add to the content data.
*/
public function addToContentData(string $type, $field_name, string $label = NULL): array {
Eric Bremner
committed
return [
Eric Bremner
committed
'type' => $type,
'field' => $field_name,
'label' => $label,
Eric Bremner
committed
];
}
Eric Bremner
committed
/**
Eric Bremner
committed
* Get the node content for blog content type.
Eric Bremner
committed
*
* @param array $node_flags
* The flags for the node.
*
* @return array
* Array of content to get from the node.
*/
Eric Bremner
committed
public function getBlogContent(array $node_flags): array {
Eric Bremner
committed
Eric Bremner
committed
// Get the content data.
$content_data = $this->setupContentData($node_flags);
Eric Bremner
committed
Eric Bremner
committed
// Setup the header content.
if ($node_flags['get_header']) {
$content_data['header']['date'] = $this->addToContentData('date', 'field_uw_blog_date');
$content_data['header']['author'] = $this->addToContentData('author', 'field_author');
Eric Bremner
committed
}
// Get the media.
if ($node_flags['get_media']) {
$content_data['media'] = $this->addToContentData('media', NULL);
Eric Bremner
committed
if ($node_flags['get_image']) {
$content_data['image'] = $this->addToContentData('image', 'field_uw_blog_listing_page_image');
$content_data['image']['extra_options'] = [
'type' => 'listing_image',
'is_responsive' => TRUE,
];
Eric Bremner
committed
}
// Setup the actual content.
if ($node_flags['get_content']) {
$content_data['content'] = $this->addToContentData('content', 'field_uw_blog_summary');
}
// Get the tags, if any.
if ($node_flags['get_tags']) {
$content_data['tags'] = $this->addToContentData(
'terms',
[
'field_uw_blog_tags',
'field_uw_audience',
]
);
Eric Bremner
committed
}
Eric Bremner
committed
return $content_data;
}
/**
* Get the node content for event content type.
*
* @param array $node_flags
* The flags for the node.
*
* @return array
* Array of content to get from the node.
*/
public function getEventContent(array $node_flags): array {
// Setup the content data array.
$content_data = $this->setupContentData($node_flags);
// Setup the header content.
Eric Bremner
committed
if ($node_flags['get_header']) {
$content_data['header']['date'] = $this->addToContentData('date', 'field_uw_event_date');
Eric Bremner
committed
}
// Get the media.
if ($node_flags['get_media']) {
$content_data['media'] = $this->addToContentData('media', NULL);
Eric Bremner
committed
if ($node_flags['get_image']) {
$content_data['image'] = $this->addToContentData('image', 'field_uw_event_listing_page_img');
$content_data['image']['extra_options'] = [
'type' => 'listing_image',
'is_responsive' => TRUE,
];
Eric Bremner
committed
}
Eric Bremner
committed
// Setup the actual content.
Eric Bremner
committed
if ($node_flags['get_content']) {
Eric Bremner
committed
$content_data['content'] = $this->addToContentData('content', 'field_uw_event_summary');
Eric Bremner
committed
}
Eric Bremner
committed
// Setup the footer content.
Eric Bremner
committed
if ($node_flags['get_footer']) {
$content_data['footer']['additional_info'] = [
Eric Bremner
committed
'has_children' => TRUE,
Eric Bremner
committed
'host' => $this->addToContentData('link', 'field_uw_event_host', 'Host'),
'event_website' => $this->addToContentData('link', 'field_uw_event_website', 'Event website'),
'cost' => $this->addToContentData('plain_text', 'field_uw_event_cost', 'Cost'),
Eric Bremner
committed
];
$content_data['footer']['location_info'] = [
Eric Bremner
committed
'has_children' => TRUE,
Eric Bremner
committed
'address' => $this->addToContentData('address', 'field_uw_event_location_address', 'Location address'),
'map' => $this->addToContentData('map', 'field_uw_event_location_coord', 'Location coordinates'),
Eric Bremner
committed
];
Eric Bremner
committed
// Get the tags, if any.
if ($node_flags['get_tags']) {
$content_data['tags'] = $this->addToContentData(
Eric Bremner
committed
'terms',
[
Eric Bremner
committed
'field_uw_event_tags',
'field_uw_audience',
'field_uw_event_type',
Eric Bremner
committed
]
);
Eric Bremner
committed
}
return $content_data;
}
Eric Bremner
committed
/**
* Get the node content for news content type.
*
* @param array $node_flags
* The flags for the node.
*
* @return array
* Array of content to get from the node.
*/
public function getNewsContent(array $node_flags): array {
// Get the content data.
$content_data = $this->setupContentData($node_flags);
Eric Bremner
committed
// Setup the header content.
if ($node_flags['get_header']) {
$content_data['header']['date'] = $this->addToContentData('date', 'field_uw_news_date');
}
// Get the media.
if ($node_flags['get_media']) {
$content_data['media'] = $this->addToContentData('media', NULL);
}
// Get listing image.
if ($node_flags['get_image']) {
$content_data['image'] = $this->addToContentData('image', 'field_uw_news_listing_page_image');
$content_data['image']['extra_options'] = [
'type' => 'listing_image',
'is_responsive' => TRUE,
];
}
// Setup the actual content.
if ($node_flags['get_content']) {
$content_data['content'] = $this->addToContentData('content', 'field_uw_news_summary');
}
// Get the tags, if any.
if ($node_flags['get_tags']) {
$content_data['tags'] = $this->addToContentData(
'terms',
[
'field_uw_news_tags',
'field_uw_audience',
]
);
}
Eric Bremner
committed
}
/**
* Get the node content for web page content type.
*
* @param array $node_flags
* The flags for the node.
*
* @return array
* Array of content to get from the node.
*/
public function getWebPageContent(array $node_flags): array {
// Get the content data.
$content_data = $this->setupContentData($node_flags);
// Setup the actual content.
if ($node_flags['get_content']) {
$content_data['content'] = $this->addToContentData('content', 'layout_builder__layout');
}
if ($node_flags['get_media']) {
$content_data['media'] = $this->addToContentData('media', NULL);
}
return $content_data;
Eric Bremner
committed
}
/**
* Get the node content for catalog item content type.
*
* @param array $node_flags
* The flags for the node.
*
* @return array
* Array of content to get from the node.
*/
public function getCatalogItemContent(array $node_flags): array {
// Get the content data.
$content_data = $this->setupContentData($node_flags);
Eric Bremner
committed
// Setup the actual content.
if ($node_flags['get_content']) {
$content_data['content'] = $this->addToContentData('content', 'field_uw_news_summary');
}
// Get the footer for catalog items.
if ($node_flags['get_footer']) {
// Get the catalog terms.
$content_data['footer']['additional_info']['has_children'] = TRUE;
$content_data['footer']['additional_info']['tags'] = $this->addToContentData(
'catalog_terms',
[
'Category' => 'field_uw_catalog_category',
'Faculty' => 'field_uw_catalog_faculty',
'Audience' => 'field_uw_audience',
]
);
}
return $content_data;
Eric Bremner
committed
}
/**
* Get the node content for catalog item content type.
*
* @param array $node_flags
* The flags for the node.
*
* @return array
* Array of content to get from the node.
*/
public function getContactContent(array $node_flags): array {
// Get the content data.
$content_data = $this->setupContentData($node_flags);
// Setup the header content.
if ($node_flags['get_header']) {
$content_data['header']['position'] = $this->addToContentData('plain_text', 'field_uw_ct_contact_title');
}
if ($node_flags['get_image']) {
$content_data['image'] = $this->addToContentData('image', 'field_uw_ct_contact_image');
$content_data['image']['extra_options'] = [
'type' => 'portrait',
Eric Bremner
committed
'crop' => 'portrait',
'is_responsive' => TRUE,
];
}
// Setup the actual content.
if ($node_flags['get_content']) {
$content_data['content'] = $this->addToContentData('content', NULL);
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
}
// Get the footer data.
if ($node_flags['get_footer']) {
// Get the additional info.
$content_data['footer']['additional_info']['has_children'] = TRUE;
$content_data['footer']['additional_info']['info'] = $this->addToContentData('formatted_text', 'field_uw_ct_contact_info');
// Get the contact information.
$content_data['footer']['contact_info']['has_children'] = TRUE;
$content_data['footer']['contact_info']['email'] = $this->addToContentData('plain_text', 'field_uw_ct_contact_email');
$content_data['footer']['contact_info']['phone'] = $this->addToContentData('plain_text', 'field_uw_ct_contact_phone');
$content_data['footer']['contact_info']['location'] = $this->addToContentData('plain_text', 'field_uw_ct_contact_location', 'Location');
// Get the links for the profile.
$content_data['footer']['links']['has_children'] = TRUE;
$content_data['footer']['links']['profile'] = $this->addToContentData('link', 'field_uw_ct_contact_link_profile', 'Link to profile');
$content_data['footer']['links']['webpage'] = $this->addToContentData('link', 'field_uw_ct_contact_link_persona', 'Link to personal webpage');
// Get the contact for, for the profile.
$content_data['footer']['contact_for']['has_children'] = TRUE;
$content_data['footer']['contact_for']['contact'] = $this->addToContentData('plain_text', 'field_uw_ct_contact_contact_for');
// Get the groups for the profile.
$content_data['footer']['groups']['has_children'] = TRUE;
$content_data['footer']['groups']['groups'] = $this->addToContentData('terms', ['field_uw_ct_contact_group']);
}
return $content_data;
Eric Bremner
committed
}
/**
* Get the node content for profile content type.
*
* @param array $node_flags
* The flags for the node.
*
* @return array
* Array of content to get from the node.
*/
public function getProfileContent(array $node_flags): array {
// Get the content data.
$content_data = $this->setupContentData($node_flags);
Eric Bremner
committed
// Setup the header content.
if ($node_flags['get_header']) {
$content_data['header']['position'] = $this->addToContentData('plain_text', 'field_uw_ct_profile_title');
}
// Setup the actual content.
if ($node_flags['get_content']) {
$content_data['content'] = $this->addToContentData('content', 'field_uw_profile_summary');
}
// Get the footer for the profile.
if ($node_flags['get_footer']) {
$content_data['footer']['links']['has_children'] = TRUE;
$content_data['footer']['links']['profile'] = $this->addToContentData('link', 'field_uw_ct_profile_info_link');
$content_data['footer']['links']['webpage'] = $this->addToContentData('link', 'field_uw_ct_profile_link_persona');
}
// Get the tags, if any.
if ($node_flags['get_tags']) {
$content_data['tags'] = $this->addToContentData(
'terms',
[
'field_uw_ct_profile_type',
]
);
}
return $content_data;
Eric Bremner
committed
}
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/**
* Get the node content for service content type.
*
* @param array $node_flags
* The flags for the node.
*
* @return array
* Array of content to get from the node.
*/
public function getServiceContent(array $node_flags): array {
// Get the content data.
$content_data = $this->setupContentData($node_flags);
if ($node_flags['get_header']) {
$content_data['header']['status'] = $this->addToContentData('select', 'field_uw_service_status');
}
// Setup the actual content.
if ($node_flags['get_content']) {
$content_data['content'] = $this->addToContentData('content', 'field_uw_service_summary');
}
// Get the footer for the profile.
if ($node_flags['get_footer']) {
$content_data['footer']['service_information']['has_children'] = TRUE;
$content_data['footer']['service_information']['status'] = $this->addToContentData('select', 'field_uw_service_status');
$content_data['footer']['service_information']['categories'] = $this->addToContentData('terms', ['field_uw_service_category']);
$content_data['footer']['service_details']['has_children'] = TRUE;
$content_data['footer']['service_details']['popularity'] = $this->addToContentData('plain_text', 'field_uw_service_popularity');
$content_data['footer']['service_details']['use_service'] = $this->addToContentData(
'terms',
[
'field_uw_service_audience',
]
);
$content_data['footer']['service_details']['whats_available'] = $this->addToContentData('multiple_plain_text', 'field_uw_service_available');
$content_data['footer']['service_details']['request_service'] = $this->addToContentData('formatted_text', 'field_uw_service_request');
$content_data['footer']['service_details']['minimum_notice'] = $this->addToContentData('plain_text', 'field_uw_service_notice');
$content_data['footer']['service_details']['average_length'] = $this->addToContentData('plain_text', 'field_uw_service_length');
$content_data['footer']['service_details']['pricing_cost'] = $this->addToContentData('formatted_text', 'field_uw_service_cost');
$content_data['footer']['service_details']['support'] = $this->addToContentData('formatted_text', 'field_uw_service_support');
$content_data['footer']['service_hours']['has_children'] = TRUE;
$content_data['footer']['service_hours']['hours'] = $this->addToContentData('hours', 'field_uw_service_hours');
$content_data['footer']['location_info'] = [
'has_children' => TRUE,
'address' => $this->addToContentData('address', 'field_uw_service_location', 'Location address'),
'map' => $this->addToContentData('map', 'field_uw_service_location_coord', 'Location coordinates'),
];
}
return $content_data;
}
/**
* Get the node content for opportunity content type.
*
* @param array $node_flags
* The flags for the node.
* @param string $view_mode
* The view mode for this node.
*
* @return array
* Array of content to get from the node.
*/
public function getOpportunityContent(array $node_flags, string $view_mode): array {
// Get the content data.
$content_data = $this->setupContentData($node_flags);
// Setup the header content.
if ($node_flags['get_header'] && $view_mode !== 'teaser') {
Eric Bremner
committed
$content_data['header']['opportunity_type'] = $this->addToContentData('terms', ['field_uw_opportunity_type']);
$content_data['header']['employment_type'] = $this->addToContentData('terms', ['field_uw_opportunity_employment']);
$content_data['header']['rate_of_pay'] = $this->addToContentData('plain_text', 'field_uw_opportunity_pay_rate');
$content_data['header']['rate_of_pay_type'] = $this->addToContentData('terms', ['field_uw_opportunity_pay_type']);
$content_data['header']['job_id'] = $this->addToContentData('plain_text', 'field_uw_opportunity_job_id');
}
// If we are on a teaser, send some fields to the footer,
// so that the display after the content.
if ($view_mode == 'teaser') {
$content_data['footer']['posted'] = $this->addToContentData('date', 'field_uw_opportunity_date');
$content_data['footer']['deadline'] = $this->addToContentData('date', 'field_uw_opportunity_deadline');
}
if ($node_flags['get_footer']) {
$content_data['footer']['links']['has_children'] = TRUE;
$content_data['footer']['links']['application'] = $this->addToContentData('link', 'field_uw_opportunity_link');
$content_data['footer']['links']['additional_info'] = $this->addToContentData('link', 'field_uw_opportunity_additional');
$content_data['footer']['links']['contact'] = $this->addToContentData('link', 'field_uw_opportunity_contact');
$content_data['footer']['opportunity_details']['has_children'] = TRUE;
$content_data['footer']['opportunity_details']['posted_by'] = $this->addToContentData('plain_text', 'field_uw_opportunity_post_by');
$content_data['footer']['opportunity_details']['number_of_positions'] = $this->addToContentData('select', 'field_uw_opportunity_pos_number');
$content_data['footer']['opportunity_details']['reports_to'] = $this->addToContentData('plain_text', 'field_uw_opportunity_report');
$content_data['footer']['opportunity_dates']['has_children'] = TRUE;
$content_data['footer']['opportunity_dates']['posted'] = $this->addToContentData('date', 'field_uw_opportunity_date');
$content_data['footer']['opportunity_dates']['deadline'] = $this->addToContentData('date', 'field_uw_opportunity_deadline');
$content_data['footer']['opportunity_dates']['start_date'] = $this->addToContentData('date', 'field_uw_opportunity_start_date');
$content_data['footer']['opportunity_dates']['end_date'] = $this->addToContentData('date', 'field_uw_opportunity_end_date');
}
// Setup the actual content.
if ($node_flags['get_content']) {
$content_data['content'] = $this->addToContentData('content', 'field_uw_opportunity_position');
}
return $content_data;
}
/**
* Gets the most recent date.
*
* @param array $date
* The date.
* @param string $type
* The type of date.
*
* @return array
* The array of dates.
public function getDate(array $date, string $type): array {
return $this->uwNodeData->getDate($date, $type);