From 7f57d8755c809635b97aea3ecda5d731b723e25f Mon Sep 17 00:00:00 2001 From: ebremner <ebremner@uwaterloo.ca> Date: Tue, 15 Jun 2021 21:41:26 -0400 Subject: [PATCH] ISTWCMS-4704: refactoring code for easier reability and adding data for contacts --- src/Service/UWService.php | 256 +++++++++++++++++++++-------- src/Service/UWServiceInterface.php | 52 ++++++ 2 files changed, 241 insertions(+), 67 deletions(-) diff --git a/src/Service/UWService.php b/src/Service/UWService.php index 1fcc13db..7caba5ee 100644 --- a/src/Service/UWService.php +++ b/src/Service/UWService.php @@ -7,6 +7,9 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\EntityReferenceFieldItemListInterface; use Drupal\Core\Url; +use Drupal\file\Entity\File; +use Drupal\image\Entity\ImageStyle; +use Drupal\media\Entity\Media; use Drupal\node\Entity\Node; use Drupal\simplify_menu\MenuItems; use Drupal\path_alias\AliasManager; @@ -207,6 +210,26 @@ class UWService implements UWServiceInterface { 'content' => $get_content ? 'layout_builder__layout' : NULL, ]; break; + + case 'uw_ct_contact': + + $content_data = [ + 'title' => $get_header ? 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; } return $this->uwGetNodeData($node, $view_mode, $content_data); @@ -234,79 +257,21 @@ 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': - - // If this is not and event, just get the date. - if ($node->getType() !== 'uw_ct_event') { - $node_data['date'] = date('l, F j, Y', strtotime($node->$data->value)); - } - else { - - // Get all the dates. - $dates = $node->$data->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")) { - $node_data['date'][] = date('l, F j, Y', $date['value']) . ' (all day)'; - } - else { - - // If the date is upcoming, meaning greater than right now. - if ($date['end_value'] > strtotime("now")) { - - // 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. - $node_data['date'][] = $start_date . ' - ' . $end_date . ' ' . date('T', $date['end_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'] = [ @@ -317,6 +282,27 @@ class UWService implements UWServiceInterface { } break; + case 'date': + $node_data['date'] = $this->uwGetDates($node, $data); + break; + + case 'groups': + $node_data['groups'] = $this->uwGetTermsFromEntityField($node->field_uw_ct_contact_group, 'tags'); + break; + + case 'image': + $node_data['image'] = $this->uwGetImage($node, $data); + break; + + case 'link_profile': + case 'personal_webpage': + $node_data[$index] = $this->uwGetLinkInfo($node, $data); + break; + + case 'sources': + $node_data['image'] = $this->uwGetSources($node, $data); + break; + case 'tags': $tags = []; foreach ($data as $field) { @@ -325,9 +311,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; } } } @@ -335,6 +329,134 @@ class UWService implements UWServiceInterface { return $node_data; } + /** + * {@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_uw_ct_contact_image->getValue(); + + // If there is an image, process it. + if ($mid) { + + // Load in the media item. + $media = 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 = File::load($fid); + $image['uri'] = ImageStyle::load('thumbnail')->buildUrl($file->getFileUri()); + } + } + + 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. + 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} */ diff --git a/src/Service/UWServiceInterface.php b/src/Service/UWServiceInterface.php index 0f21e9a6..60b58a64 100644 --- a/src/Service/UWServiceInterface.php +++ b/src/Service/UWServiceInterface.php @@ -58,6 +58,58 @@ interface UWServiceInterface { */ public function uwGetNodeData(Node $node, string $view_mode, array $content_data): array; + /** + * 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. * -- GitLab