diff --git a/src/Service/UWService.php b/src/Service/UWService.php index 9e2852550a17f8a72c20856fa4d75e4217c2c4cb..81d953292954eeb275b4e7f6292b7be19736184c 100644 --- a/src/Service/UWService.php +++ b/src/Service/UWService.php @@ -112,6 +112,9 @@ class UWService implements UWServiceInterface { $get_footer = FALSE; $get_image = FALSE; $get_content = FALSE; + $get_title = TRUE; + + $featured_image = $this->uwGetFeaturedImageContentTypes(); // Setup flags based on teaser content argument. if ($content == 'all') { @@ -122,14 +125,25 @@ class UWService implements UWServiceInterface { if ($view_mode == 'teaser') { $get_image = TRUE; } + + if ($view_mode == 'full') { + $get_title = FALSE; + + if (in_array($node->getType(), array_keys($featured_image))) { + $get_image = TRUE; + $get_title = TRUE; + } + } } else { if ($content == 'header') { $get_header = TRUE; + $get_image = TRUE; } if ($content == 'footer') { $get_footer = TRUE; + $get_title = FALSE; } } @@ -145,7 +159,7 @@ class UWService implements UWServiceInterface { ]; $content_data = [ - 'title' => $get_header ? TRUE : NULL, + 'title' => $get_title ? TRUE : NULL, 'url' => TRUE, 'date' => $get_header ? 'field_uw_blog_date' : NULL, 'author' => $get_header ? TRUE : NULL, @@ -165,7 +179,7 @@ class UWService implements UWServiceInterface { ]; $content_data = [ - 'title' => $get_header ? TRUE : NULL, + 'title' => $get_title ? TRUE : NULL, 'url' => TRUE, 'date' => $get_header ? 'field_uw_event_date' : NULL, 'sources' => $get_image ? 'field_uw_event_listing_page_img' : NULL, @@ -183,7 +197,7 @@ class UWService implements UWServiceInterface { ]; $content_data = [ - 'title' => $get_header ? TRUE : NULL, + 'title' => $get_title ? TRUE : NULL, 'url' => TRUE, 'date' => $get_header ? 'field_uw_news_date' : NULL, 'sources' => $get_image ? 'field_uw_news_listing_page_image' : NULL, @@ -191,6 +205,61 @@ class UWService implements UWServiceInterface { 'tags' => $get_footer ? $tag_list : NULL, ]; break; + + case 'uw_ct_web_page': + + $content_data = [ + 'title' => $get_title ? TRUE : NULL, + 'content' => $get_content ? 'layout_builder__layout' : NULL, + ]; + break; + + case 'uw_ct_catalog_item': + + $content_data = [ + 'title' => $get_title ? TRUE : NULL, + 'content' => $get_content ? 'layout_builder__layout' : NULL, + ]; + break; + + case 'uw_ct_contact': + + $content_data = [ + 'title' => $get_title ? TRUE : NULL, + 'sub_title' => $get_header ? 'field_uw_ct_contact_title' : NULL, + 'affiliation' => $get_header ? 'field_uw_ct_contact_affiliation' : NULL, + 'image' => $get_image ? 'field_uw_ct_contact_image' : NULL, + 'content' => $get_content ? 'layout_builder__layout' : NULL, + 'email' => $get_footer ? 'field_uw_ct_contact_email' : NULL, + 'location' => $get_footer ? 'field_uw_ct_contact_location' : NULL, + 'phone' => $get_footer ? 'field_uw_ct_contact_phone' : NULL, + 'additional_info' => $get_footer ? 'field_uw_ct_contact_info' : NULL, + 'link_profile' => $get_footer ? 'field_uw_ct_contact_link_profile' : NULL, + 'personal_webpage' => $get_footer ? 'field_uw_ct_contact_link_persona' : NULL, + 'contact_for' => $get_footer ? 'field_uw_ct_contact_contact_for' : NULL, + 'groups' => $get_footer ? 'field_uw_ct_contact_group' : NULL, + 'url' => TRUE, + ]; + break; + + case 'uw_ct_profile': + + $tag_list = [ + 'field_uw_ct_profile_type', + ]; + + $content_data = [ + 'title' => $get_title ? TRUE : NULL, + 'sub_title' => $get_header ? 'field_uw_ct_profile_title' : NULL, + 'affiliation' => $get_header ? 'field_uw_ct_profile_affiliation' : NULL, + 'content' => $get_content ? 'field_uw_profile_summary' : NULL, + 'image' => $get_header ? 'field_uw_ct_profile_image' : NULL, + 'tags' => $get_footer ? $tag_list : NULL, + 'link_profile' => $get_footer ? 'field_uw_ct_profile_info_link' : NULL, + 'personal_webpage' => $get_footer ? 'field_uw_ct_profile_link_persona' : NULL, + 'url' => TRUE, + ]; + break; } return $this->uwGetNodeData($node, $view_mode, $content_data); @@ -218,58 +287,50 @@ class UWService implements UWServiceInterface { // a field name. switch ($index) { - case 'title': - $node_data['title'] = $node->getTitle(); + case 'additional_info': + $additional_info = $node->field_uw_ct_contact_info->getValue(); + if ($additional_info) { + $node_data['additional_info'] = [ + '#type' => 'processed_text', + '#text' => $additional_info[0]['value'], + '#format' => $additional_info[0]['format'], + ]; + } break; case 'author': $node_data['author'] = $this->uwGetAuthor($node); break; - case 'date': + case 'content': 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']); - } + $node_data['content'] = [ + '#type' => 'processed_text', + '#text' => $node->$data->value, + '#format' => $node->$data->format, + ]; } break; - case 'sources': + case 'date': + $node_data['date'] = $this->uwGetDates($node, $data); + break; - // Get the image entity. - $image = $node->$data->entity; + case 'groups': + $node_data['groups'] = $this->uwGetTermsFromEntityField($node->field_uw_ct_contact_group, 'tags'); + break; - // If there is an image, get the responsive image sources. - if ($image) { - $sources = $this->prepareResponsiveImage($image, 'uw_ris_media'); - } - else { - $sources = NULL; - } + case 'image': + $node_data['image'] = $this->uwGetImage($node, $data); + break; - 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']; - } + case 'link_profile': + case 'personal_webpage': + $node_data[$index] = $this->uwGetLinkInfo($node, $data); break; - case 'content': - if ($view_mode == 'teaser') { - $node_data['content'] = [ - '#type' => 'processed_text', - '#text' => $node->$data->value, - '#format' => $node->$data->format, - ]; - } + case 'sources': + $node_data['image'] = $this->uwGetSources($node, $data); break; case 'tags': @@ -280,9 +341,17 @@ class UWService implements UWServiceInterface { $node_data['tags'] = [$tags]; break; + case 'title': + $node_data['title'] = $node->getTitle(); + break; + case 'url': $node_data['url'] = $node->toUrl()->toString(); break; + + default: + $node_data[$index] = $node->$data->value; + break; } } } @@ -290,6 +359,182 @@ class UWService implements UWServiceInterface { return $node_data; } + /** + * {@inheritDoc} + */ + public function uwGetFeaturedImageContentTypes(): array { + return [ + 'uw_ct_blog' => 'field_uw_blog_listing_page_image', + 'uw_ct_event' => 'field_uw_event_listing_page_img', + 'uw_ct_news_item' => 'field_uw_news_listing_page_image', + ]; + } + + /** + * {@inheritDoc} + */ + public function uwCheckNodeForFeaturedImage(Node $node): string { + + // Set the node type. + $node_type = $node->getType(); + + // Get the list of content types that are allowed to have + // feature images from our service. + $featured_image = $this->uwGetFeaturedImageContentTypes(); + + // If node is allowed to have a featured image, make sure that + // node actually has an image. + if (in_array($node_type, array_keys($featured_image))) { + + // Get the field name. + $field_name = $featured_image[$node_type]; + + // Get the image object values from the node. + $image = $node->$field_name->getValue(); + + // If there is an image present, set the variable so that + // the page title will not be displayed. + if ($image) { + return 'yes'; + } + else { + return 'no'; + } + } + + return 'no'; + } + + /** + * {@inheritDoc} + */ + public function uwGetLinkInfo(Node $node, string $field_name): array { + + $return_link_data = []; + + // Get the link from the node. + $link_data = $node->$field_name->getValue(); + + // If there is data in the link, get the variables. + if ($link_data) { + $return_link_data['uri'] = $link_data[0]['uri']; + $return_link_data['title'] = $link_data[0]['title']; + } + + return $return_link_data; + } + + /** + * {@inheritDoc} + */ + public function uwGetImage(Node $node, string $field_name): array { + + $image = []; + + // Get the media id. + $mid = $node->$field_name->getValue(); + + // If there is an image, process it. + if ($mid) { + + // Load in the media item. + $media = $this->entityTypeManager->getStorage('media')->load($mid[0]['target_id']); + + // Get the file id from the media object. + $fid = $media->getSource()->getSourceFieldValue($media); + + // If there is a file id, then get the uri, + // using the thumbnail image style. + if ($fid) { + $file = $this->entityTypeManager->getStorage('file')->load($fid); + $image['uri'] = $this->entityTypeManager->getStorage('image_style')->load('thumbnail')->buildUrl($file->getFileUri()); + $image['alt'] = $media->field_media_image->alt; + } + } + + return $image; + } + + /** + * {@inheritDoc} + */ + public function uwGetDates(Node $node, string $field_name): array { + + $return_dates = []; + + // If this is not and event, just get the date. + if ($node->getType() !== 'uw_ct_event') { + $return_dates[] = date('l, F j, Y', strtotime($node->$field_name->value)); + } + else { + + // Get all the dates. + $dates = $node->$field_name->getValue(); + + // Step through each of the dates and get + // out correct values. + foreach ($dates as $date) { + + // The all day case, duration is always 1439. + if ($date['duration'] == '1439' && $date['end_value'] > strtotime("now")) { + $return_dates[] = date('l, F j, Y', $date['value']) . ' (all day)'; + } + else { + + // If the date is upcoming, meaning greater than right now. + // Taking this out for now, will be putting it back, if + // we figure out how to do this in the view. + // if ($date['end_value'] > strtotime("today")) {. + // If this is the same day, get the date and the start + // and end times. + if ($date['duration'] < '1439') { + $start_date = date('l, F j, Y g:i A', $date['value']); + $end_date = date('g:i A', $date['end_value']); + } + + // This is not the day, get the start and end date with time. + else { + $start_date = date('l, F j, Y g:i A', $date['value']); + $end_date = date('l, F j, Y g:i A', $date['end_value']); + } + + // Add the start and end date with timezone. + $return_dates[] = $start_date . ' - ' . $end_date . ' ' . date('T', $date['end_value']); + // } + } + } + } + + return $return_dates; + } + + /** + * {@inheritDoc} + */ + public function uwGetSources(Node $node, string $field_name): array { + + $return_sources = []; + + // Get the image entity. + $image = $node->$field_name->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'])) { + $return_sources['sources'] = $sources['sources']; + $return_sources['img_element'] = $sources['img_element']['#uri']; + $return_sources['alt'] = $sources['alt']; + } + + return $return_sources; + } + /** * {@inheritDoc} */ @@ -544,104 +789,6 @@ 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'; - - // Set the header data. - $header_data = [ - 'date' => date('l, F j, Y', strtotime($node->field_uw_blog_date->value)), - 'author' => $this->uwGetAuthor($node), - 'title' => $node->getTitle(), - ]; - break; - - case 'uw_ct_event': - - // Get all the dates. - // @todo figure out what date to display. - $dates = $node->field_uw_event_date->getValue(); - - // Set the header data. - $header_data = [ - 'date' => date('l, F j, Y', $dates[0]['value']), - 'title' => $node->getTitle(), - ]; - break; - - case 'uw_ct_news_item': - - // Set the header data. - $header_data = [ - 'date' => date('l, F j, Y', strtotime($node->field_uw_news_date->value)), - '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'; - $tags = $this->uwGetTermsFromEntityField($node->get('field_uw_blog_tags'), 'tags'); - $audiences = $this->uwGetTermsFromEntityField($node->get('field_uw_audience'), 'tags'); - $footer_data = [ - 'tags' => [ - array_merge($tags, $audiences), - ], - ]; - break; - - case 'uw_ct_event'; - $tags = $this->uwGetTermsFromEntityField($node->get('field_uw_event_tags'), 'tags'); - $audiences = $this->uwGetTermsFromEntityField($node->get('field_uw_audience'), 'tags'); - $types = $this->uwGetTermsFromEntityField($node->get('field_uw_event_type'), 'tags'); - $footer_data = [ - 'tags' => [ - array_merge($tags, $audiences, $types), - ], - ]; - break; - - case 'uw_ct_news_item'; - $tags = $this->uwGetTermsFromEntityField($node->get('field_uw_news_tags'), 'tags'); - $audiences = $this->uwGetTermsFromEntityField($node->get('field_uw_audience'), 'tags'); - $footer_data = [ - 'tags' => [ - array_merge($tags, $audiences), - ], - ]; - break; - } - - // Return the header data. - return $footer_data; - } - /** * {@inheritDoc} */ diff --git a/src/Service/UWServiceInterface.php b/src/Service/UWServiceInterface.php index c4818aa19e7a93524f8dcf99eb53fa9658026503..00ede8c0879086843043f99cda6a834d40cbe33d 100644 --- a/src/Service/UWServiceInterface.php +++ b/src/Service/UWServiceInterface.php @@ -58,6 +58,77 @@ interface UWServiceInterface { */ public function uwGetNodeData(Node $node, string $view_mode, array $content_data): array; + /** + * Gets content types that have feature images. + * + * @return array + * Array of content types that has featured images. + */ + public function uwGetFeaturedImageContentTypes(): array; + + /** + * Gets dates from node. + * + * @param \Drupal\node\Node $node + * Node entity. + * + * @return string + * Yes or no. + */ + public function uwCheckNodeForFeaturedImage(Node $node): string; + + /** + * Gets dates from node. + * + * @param \Drupal\node\Node $node + * Node entity. + * @param string $field_name + * The field name that has the date(s). + * + * @return array + * Array of dates. + */ + public function uwGetDates(Node $node, string $field_name): array; + + /** + * Gets image from node. + * + * @param \Drupal\node\Node $node + * Node entity. + * @param string $field_name + * The field name that has the date(s). + * + * @return array + * array with image information. + */ + public function uwGetImage(Node $node, string $field_name): array; + + /** + * Gets sources from node. + * + * @param \Drupal\node\Node $node + * Node entity. + * @param string $field_name + * The field name that has the date(s). + * + * @return array + * Either array with responsive image. + */ + public function uwGetSources(Node $node, string $field_name): array; + + /** + * Gets link info from node. + * + * @param \Drupal\node\Node $node + * Node entity. + * @param string $field_name + * The field name that has the date(s). + * + * @return array + * Array with link info. + */ + public function uwGetLinkInfo(Node $node, string $field_name): array; + /** * A function to get or check the attached sidebar. * @@ -183,28 +254,6 @@ 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. * diff --git a/uw_cfg_common.module b/uw_cfg_common.module index b44da4c44b71d71a3d178d430f0b27d7202fae67..8b4c2586791b5fbe1f13ceb5871e589399e376f3 100644 --- a/uw_cfg_common.module +++ b/uw_cfg_common.module @@ -287,7 +287,9 @@ function uw_cfg_common_preprocess_node(&$variables) { // If the layout builder style is set to full width, then set // the classes variable for the node and exit the loop. - if ($settings['layout_builder_styles_style'] == "uw_lbs_full_width") { + if (isset($settings['layout_builder_styles_style']) && + $settings['layout_builder_styles_style'] == "uw_lbs_full_width" + ) { // Add a class to the node for full width on a section. $variables['attributes']['class'][] = 'uw-section-has-full-width';