From 64fce0be255474383789d2c814ed526001384e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Mon, 4 Nov 2024 12:38:25 +0200 Subject: [PATCH] Refactored the organization information block to use the JobListing bundle class. --- .../src/Entity/JobListing.php | 153 ++++++++++++++++++ .../Plugin/Block/OrganizationInformation.php | 16 +- 2 files changed, 157 insertions(+), 12 deletions(-) diff --git a/public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php b/public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php index f466a7bb..03fd365b 100644 --- a/public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php +++ b/public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php @@ -4,7 +4,9 @@ namespace Drupal\helfi_rekry_content\Entity; +use Drupal\Core\Entity\EntityInterface; use Drupal\node\Entity\Node; +use Drupal\taxonomy\TermInterface; /** * Bundle class for JobListing paragraph. @@ -80,4 +82,155 @@ public function getEmploymentType() : string { } + /** + * Get city description fields. + * + * @return array + * City descriptions as an array. + */ + public function getCityDescriptions() : array { + $job_listings_config = \Drupal::config('helfi_rekry_content.job_listings'); + + return [ + '#city_description_title' => $job_listings_config->get('city_description_title'), + '#city_description_text' => $job_listings_config->get('city_description_text'), + ]; + } + + /** + * Get organization entity. + * + * @param string $organization_id + * Organization ID. + * + * @return \Drupal\Core\Entity\EntityInterface|false + * Organization entity. + */ + protected function getOrganization(string $organization_id) : EntityInterface|bool { + try { + return \Drupal::entityTypeManager() + ->getStorage('taxonomy_term') + ->load($organization_id); + } + catch (\Exception $e) { + return FALSE; + } + } + + /** + * Get organization override. + * + * @return \Drupal\taxonomy\TermInterface|bool + * Returns the organization taxonomy term or false if not set. + * + * @throws \Drupal\Core\TypedData\Exception\MissingDataException + */ + protected function getOrganizationOverride() : TermInterface|bool { + $organization_id = ''; + + // Get the organization id from the migrated field. + if (!$this->get('field_organization')->isEmpty()) { + $organization_id = $this->get('field_organization') + ->first() + ->get('target_id') + ->getValue(); + } + + // Use the organization override value if it is set. + if (!$this->get('field_organization_override')->isEmpty()) { + $organization_id = $this->get('field_organization_override') + ->first() + ->get('target_id') + ->getValue(); + } + + return $this->getOrganization($organization_id); + } + + /** + * Get organization default image. + * + * @param \Drupal\taxonomy\TermInterface|false $organization + * Organization term. + * + * @return array + * Returns a render array of the image. + * + * @throws \Drupal\Core\TypedData\Exception\MissingDataException + */ + public function getOrganizationDefaultImage(TermInterface|false $organization) : array { + if ($organization && !$organization->get('field_default_image')->isEmpty()) { + return $organization->get('field_default_image')->first()->view([ + 'type' => 'responsive_image', + 'label' => 'hidden', + 'settings' => [ + 'responsive_image_style' => 'job_listing_org', + 'image_link' => '', + 'image_loading' => [ + 'attribute' => 'eager', + ], + ], + ]); + } + + // Return the JobListing image. + return $this->get('field_image')->value ?? []; + } + + /** + * Get organization description. + * + * @param \Drupal\taxonomy\TermInterface|false $organization + * Organization entity. + * + * @return array + * Organization description as a render array. + */ + public function getOrganizationDescription(TermInterface|false $organization) : array { + // Set organization description from node. + $organization_description = $this->get('field_organization_description'); + + // Check if the organization description override is set and use it. + if (!$this->get('field_organization_description_o')->isEmpty()) { + $organization_description = $this->get('field_organization_description_o'); + } + // If not and the organization description is empty, + // check if the organization taxonomy description is set and use it. + elseif ($organization_description->isEmpty() && !$organization->get('description')->isEmpty()) { + $organization_description = $organization->get('description'); + } + + return $organization_description->processed ?? $organization_description->value; + } + + /** + * Build the organization information render array. + * + * @return array + * Returns the render array. + * + * @throws \Drupal\Core\TypedData\Exception\MissingDataException + */ + public function buildOrganization() : array { + $build = []; + + // Get the City's title and description from the configuration. + $build = $build + $this->getCityDescriptions(); + + // Get the organization entity. + $organization = $this->getOrganizationOverride(); + + if ($organization) { + // Set organization image. + $build['#organization_image'] = $this->getOrganizationDefaultImage($organization); + + // Set the organization title. + $build['#organization_title'] = $organization->getName(); + + // Set the organization description. + $build['#organization_description'] = $this->getOrganizationDescription($organization); + } + return $build; + } + } diff --git a/public/modules/custom/helfi_rekry_content/src/Plugin/Block/OrganizationInformation.php b/public/modules/custom/helfi_rekry_content/src/Plugin/Block/OrganizationInformation.php index eed18f41..85731bdf 100644 --- a/public/modules/custom/helfi_rekry_content/src/Plugin/Block/OrganizationInformation.php +++ b/public/modules/custom/helfi_rekry_content/src/Plugin/Block/OrganizationInformation.php @@ -5,7 +5,7 @@ namespace Drupal\helfi_rekry_content\Plugin\Block; use Drupal\helfi_platform_config\Plugin\Block\ContentBlockBase; -use Drupal\node\Entity\Node; +use Drupal\helfi_rekry_content\Entity\JobListing; /** * Provides a 'OrganizationInformation' block. @@ -22,9 +22,7 @@ final class OrganizationInformation extends ContentBlockBase { */ public function build() : array { $build = [ - 'sidebar_content' => [ - '#theme' => 'sidebar_content_block', - ], + '#theme' => 'organization_information_block', '#cache' => ['tags' => $this->getCacheTags()], ]; @@ -35,14 +33,8 @@ public function build() : array { $entity = $entity_matcher['entity']; // Add the organization information to render array. - if ( - $entity instanceof Node && - $entity->hasField('field_organization') && - $entity->hasField('field_organization_description') - ) { - $view_builder = $this->entityTypeManager->getViewBuilder('node'); - $build['sidebar_content']['#computed'] = $view_builder->view($entity); - $build['sidebar_content']['#computed']['#theme'] = 'organization_information_block'; + if ($entity instanceof JobListing) { + $build = $build + $entity->buildOrganization(); } return $build;