diff --git a/config/install/layout_builder_browser.layout_builder_browser_blockcat.uw_bc_teasers.yml b/config/install/layout_builder_browser.layout_builder_browser_blockcat.uw_bc_teasers.yml index bdf4c21f8d577beb369a2c08e21793f88aa82d6f..02458cf188ed0d4f117daf609bb5a4db162c5ab7 100644 --- a/config/install/layout_builder_browser.layout_builder_browser_blockcat.uw_bc_teasers.yml +++ b/config/install/layout_builder_browser.layout_builder_browser_blockcat.uw_bc_teasers.yml @@ -6,17 +6,17 @@ label: Teasers weight: null blocks: - - block_id: 'views_block:uw_view_blogs-blog_teaser_block' + block_id: uw_cbl_blog_teaser weight: 0 image_path: /profiles/uw_base_profile/themes/uw_fdsu_theme_resp/images/layout_builder_browser/blogteaser.svg image_alt: '' - - block_id: 'views_block:uw_view_events-events_teaser' + block_id: uw_cbl_news_teaser weight: 0 - image_path: /profiles/uw_base_profile/themes/uw_fdsu_theme_resp/images/layout_builder_browser/eventteaser.svg + image_path: /profiles/uw_base_profile/themes/uw_fdsu_theme_resp/images/layout_builder_browser/newsteaser.svg image_alt: '' - - block_id: 'views_block:uw_view_news_items-news_item_teaser' + block_id: uw_cbl_event_teaser weight: 0 - image_path: /profiles/uw_base_profile/themes/uw_fdsu_theme_resp/images/layout_builder_browser/newsteaser.svg + image_path: /profiles/uw_base_profile/themes/uw_fdsu_theme_resp/images/layout_builder_browser/eventteaser.svg image_alt: '' diff --git a/src/Service/UWService.php b/src/Service/UWService.php new file mode 100644 index 0000000000000000000000000000000000000000..b23eba3785ea5a819ac828c5c347d5be7c4c00a8 --- /dev/null +++ b/src/Service/UWService.php @@ -0,0 +1,150 @@ +<?php + +namespace Drupal\uw_cfg_common\Service; + +use Drupal\Core\Entity\EntityInterface; +use Drupal\node\Entity\Node; +use Drupal\node\NodeInterface; + +/** + * Class UWService + * + * @package Drupal\uw_cfg_common\Service + */ +class UWService implements UWServiceInterface { + + /** + * {@inheritDoc} + */ + public function prepareResponsiveImage(EntityInterface $entity, string $image_style): array { + + // Load in the file object if we have one. + if ($file = $entity->field_media_image->entity) { + + // Need to set these variables so that responsive image function, + // has all the necessary info to process the image style. + $variables['uri'] = $file->getFileUri(); + $variables['responsive_image_style_id'] = $image_style; + + // These is a function from the responsive image module that sets all + // the variables for the sources of the responsive image. + template_preprocess_responsive_image($variables); + + // Step through each of the sources and setup our own sources array. + foreach ($variables['sources'] as $source) { + $variables['responsive_sources'][] = [ + 'srcset' => $source->storage()['srcset']->value(), + 'media' => $source->storage()['media']->value(), + 'type' => $source->storage()['type']->value(), + ]; + } + + return $variables; + } + + return []; + } + + /** + * {@inheritDoc} + */ + public function getTeaserContent(NodeInterface $node, array $variables_to_get, string $teaser_type): array { + + // Step through each of the variables to get and set the variables for the teaser content. + foreach ($variables_to_get as $variable_to_get) { + + // Switch on the variable to get and set the variables for the teaser content. + switch ($variable_to_get) { + + case 'title': + + // Set the title from the node object. + $variables['title'] = $node->getTitle(); + break; + + case 'url': + + // Set the title from the node object. + $variables['url'] = $node->toUrl()->toString(); + break; + + case 'date': + + // Set the field name, all of our content types use the same format, + // field_uw_<content_type>_date. + $field_name = 'field_uw_' . trim($teaser_type) . '_date'; + + // Set the date variable, once returned to the calling function, they can + // change the date format as required (i.e. change it to long-date or date-time). + $variables['date'] = $node->$field_name->getString(); + break; + + case 'author': + + // If author is selected, get both the author name and link. + $variables['author_name'] = $node->getOwner()->getDisplayName(); + $variables['author_link'] = $node->getOwner()->toUrl()->toString(); + break; + + case 'sources': + + // Event has a different name for the image than the rest, so ensuring + // that we get the correct field name for the image. + // In most cases it is field_uw_<content_type>_lising_page_image. + if ($teaser_type == 'event') { + $field_name = 'field_uw_event_listing_page_img'; + } + else { + $field_name = 'field_uw_' . trim($teaser_type) . '_listing_page_image'; + } + + // Get the image object from the node. + $image = $node->$field_name->entity; + + // Get all the image variables, including the sources using the prepareResponsiveImage function. + $image_variables = $this->prepareResponsiveImage($image, 'uw_ris_media'); + + // Set the responsive image variables for the teaser content. + $variables['sources'] = $image_variables['responsive_sources']; + $variables['img_element'] = $image_variables['img_element']['#uri']; + $variables['alt'] = $image->field_media_image->alt; + break; + + case 'tags': + + // Set the field name for the tags which is field_uw_<content_type>_tags'. + $field_name = 'field_uw_' . trim($teaser_type) . '_tags'; + + // Get all the taxonomy terms for the blog. + // Step through each taxonomy term and get the tag info. + foreach ($node->$field_name as $tag) { + + // Set the tags in the teaser content variables. + $variables['tags'][] = [ + 'title' => $tag->entity->name->value, + 'url' => $tag->entity->toUrl()->toString(), + ]; + } + break; + + case 'content': + + // Set the field name for the summary, which is field_name<content_type>_summary. + $field_name = 'field_uw_' . $teaser_type . '_summary'; + + // Set the render array for the summary, we simply can not just send the + // value as Drupal will just render it with no filters or HTML applied. + // We need to send the full render array. + $variables['content'] = [ + '#type' => 'processed_text', + '#text' => $node->$field_name->value, + '#format' => $node->$field_name->format, + ]; + break; + } + } + + return $variables; + } + +} diff --git a/src/Service/UWServiceInterface.php b/src/Service/UWServiceInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..3e5d48f4c8394b4406f38c770be2c063eeb2dc59 --- /dev/null +++ b/src/Service/UWServiceInterface.php @@ -0,0 +1,21 @@ +<?php + +namespace Drupal\uw_cfg_common\Service; + +use Drupal\Core\Entity\EntityInterface; + +interface UWServiceInterface { + + /** + * Prepares responsive image. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * Image entity. + * @param string $image_style + * Image style to be used for responsive image. + * + * @return array + * Array with details for responsive image. + */ + public function prepareResponsiveImage(EntityInterface $entity, string $image_style): array; +} diff --git a/uw_cfg_common.services.yml b/uw_cfg_common.services.yml new file mode 100644 index 0000000000000000000000000000000000000000..028112fd4441ca9753b508f8a80cddf7720bfb90 --- /dev/null +++ b/uw_cfg_common.services.yml @@ -0,0 +1,4 @@ +services: + uw_cfg_common.uw_service: + class: Drupal\uw_cfg_common\Service\UWService + arguments: []