From 0d2a6ffc613064c88b379515c1c4b64fd0e7865b Mon Sep 17 00:00:00 2001 From: Eric Bremner <ebremner@uwaterloo.ca> Date: Tue, 9 Nov 2021 17:56:20 +0000 Subject: [PATCH] ISTWCMS-5195: updating code to make cleaner when adding to content data array --- src/Service/UwNodeContent.php | 163 ++++++++++++++++++++-------------- 1 file changed, 97 insertions(+), 66 deletions(-) diff --git a/src/Service/UwNodeContent.php b/src/Service/UwNodeContent.php index ed035032..c4d1b523 100644 --- a/src/Service/UwNodeContent.php +++ b/src/Service/UwNodeContent.php @@ -144,36 +144,52 @@ class UwNodeContent { } /** - * Get the node content for blog content type. + * Common elements for content data. * - * @param array $node_flags + * @param $node_flags * The flags for the node. * * @return array - * Array of content to get from the node. + * Array of common elements for content data. */ - public function getBlogContent(array $node_flags): array { + public function setupContentData($node_flags): array { - // The list of tags for blogs. - $tag_list = [ - 'field_uw_blog_tags', - 'field_uw_audience', + $content_data['url'] = [ + 'type' => 'url', ]; + if ($node_flags['get_title']) { + $content_data['title'] = [ + '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). + * @param string $label + * 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 { return [ - 'title' => $node_flags['get_title'] ? TRUE : NULL, - 'url' => TRUE, - 'date' => $node_flags['get_header'] ? 'field_uw_blog_date' : NULL, - 'author' => $node_flags['get_header'] ? TRUE : NULL, - 'sources' => $node_flags['get_image'] ? 'field_uw_blog_listing_page_image' : NULL, - 'hero' => $node_flags['get_image'] ? 'field_uw_hero_image' : NULL, - 'content' => $node_flags['get_content'] ? 'field_uw_blog_summary' : NULL, - 'tags' => $node_flags['get_footer'] ? $tag_list : NULL, + 'type' => $type, + 'field' => $field_name, + 'label' => $label, ]; } /** - * Get the node content for event content type. + * Get the node content for blog content type. * * @param array $node_flags * The flags for the node. @@ -181,81 +197,96 @@ class UwNodeContent { * @return array * Array of content to get from the node. */ - public function getEventContent(array $node_flags): array { + public function getBlogContent(array $node_flags): array { - $content_data['url'] = [ - 'type' => 'url', - ]; + // Get the content data. + $content_data = $this->setupContentData($node_flags); - if ($node_flags['get_title']) { - $content_data['title'] = [ - 'type' => 'title', + // Setup the header content. + if ($node_flags['get_header']) { + $content_data['date'] = $this->addToContentData('date', 'field_uw_blog_date'); + } + + // Setup the image content. + if ($node_flags['get_image']) { + $content_data['sources'] = $this->addToContentData('sources', 'field_uw_event_listing_page_img'); + + $content_data['hero'] = $this->addToContentData('sources', 'field_uw_hero_image'); + } + + // Setup the actual content. + if ($node_flags['get_content']) { + $content_data['content'] = $this->addToContentData('content', 'field_uw_blog_summary'); + } + + // Setup the footer. + if ($node_flags['get_footer']) { + + $terms = [ + 'field_uw_blog_tags', + 'field_uw_audience', ]; + + $content_data['tags'] = $this->addToContentData('terms', $terms); } + 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. if ($node_flags['get_header']) { - $content_data['date'] = [ - 'type' => 'date', - 'field' => 'field_uw_event_date', - ]; + $content_data['date'] = $this->addToContentData('date', 'field_uw_event_date'); } + // Setup the image content. if ($node_flags['get_image']) { - $content_data['sources'] = [ - 'type' => 'sources', - 'field' => 'field_uw_event_listing_page_img', - ]; + $content_data['sources'] = $this->addToContentData('sources', 'field_uw_event_listing_page_img'); + + $content_data['hero'] = $this->addToContentData('sources', 'field_uw_hero_image'); } + // Setup the actual content. if ($node_flags['get_content']) { - $content_data['content'] = [ - 'type' => 'content', - 'field' => 'field_uw_event_summary', - ]; + $content_data['content'] = $this->addToContentData('content', 'field_uw_event_summary'); } + // Setup the footer content. if ($node_flags['get_footer']) { $content_data['additional_info'] = [ 'has_children' => TRUE, - 'host' => [ - 'type' => 'link', - 'field' => 'field_uw_event_host', - 'label' => 'Host', - ], - 'event_website' => [ - 'type' => 'link', - 'field' => 'field_uw_event_website', - 'label' => 'Event website', - ], - 'cost' => [ - 'type' => 'plain_text', - 'field' => 'field_uw_event_cost', - 'label' => 'Cost', - ], + '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'), ]; $content_data['location_info'] = [ 'has_children' => TRUE, - 'address' => [ - 'type' => 'address', - 'field' => 'field_uw_event_location_address', - 'name' => 'Location address', - ], - 'map' => [ - 'type' => 'map', - 'field' => 'field_uw_event_location_coord', - 'name' => 'Location coordinates', - ], + 'address' => $this->addToContentData('address', 'field_uw_event_location_address', 'Location address'), + 'map' => $this->addToContentData('map', 'field_uw_event_location_coord', 'Location coordinates'), ]; - $content_data['tags'] = [ - 'type' => 'terms', - 'field' => [ + $content_data['tags'] = $this->addToContentData( + 'terms', + [ 'field_uw_event_tags', 'field_uw_audience', 'field_uw_event_type', - ], - ]; + ] + ); } return $content_data; -- GitLab