From ea898566d1ef767e08305842e7e0f76b47d1a285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Mon, 4 Nov 2024 12:35:13 +0200 Subject: [PATCH 1/9] Removed the organization information block preprocess. --- .../custom/hdbt_subtheme/hdbt_subtheme.theme | 60 ------------------- 1 file changed, 60 deletions(-) diff --git a/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme b/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme index a861fee9..95bb67f9 100644 --- a/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme +++ b/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme @@ -51,66 +51,6 @@ function hdbt_subtheme_preprocess_block__views_block__of_interest(&$variables): } } -/** - * Implements hook_preprocess_HOOK(). - * - * Default template: organization-information-block.html.twig. - * - * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException - * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException - */ -function hdbt_subtheme_preprocess_organization_information_block(array &$variables): void { - $variables['view_mode'] = $variables['elements']['#view_mode']; - - // Helpful $content variable for template. - foreach (Element::children($variables['elements']) as $key) { - $variables['content'][$key] = $variables['elements'][$key]; - } - - // Set city description title and text variables. - $job_listings_config = \Drupal::config('helfi_rekry_content.job_listings'); - $variables['content']['city_description_title'] = $job_listings_config->get('city_description_title'); - $variables['content']['city_description_text'] = $job_listings_config->get('city_description_text'); - - if (!empty($variables['elements']['field_organization_override']['#items'])) { - $organization_field = $variables['elements']['field_organization_override']['#items']; - } - else { - $organization_field = $variables['elements']['field_organization']['#items']; - } - - // Get organization. - if (!empty($organization_field)) { - $organization_id = $organization_field->target_id; - $organization = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($organization_id); - } - - // Set organization default image variable. - if (!empty($organization) && !empty($organization->get('field_default_image')->first())) { - $variables['content']['organization_default_image'] = $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', - ], - ], - ]); - } - - // Set organization description from taxonomy term if it's missing from node. - if (!empty($organization) && $node = \Drupal::routeMatch()->getParameter('node')) { - if (!$node->field_organization_description_o->isEmpty()) { - $variables['content']['field_organization_description'] = $variables['content']['field_organization_description_o']; - } - elseif ($node->field_organization_description->isEmpty() && !empty($org_description = $organization->getDescription())) { - $variables['content']['field_organization_description'] = strip_tags($org_description); - } - } -} - /** * Implements hook_preprocess_HOOK(). * From 72dc03a4c35adf847838f27fc47ef82988698969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Mon, 4 Nov 2024 12:37:10 +0200 Subject: [PATCH 2/9] Use variables instead of render element information in organization information block template. --- .../helfi_rekry_content.module | 8 ++++++- .../organization-information-block.html.twig | 24 +++++++------------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module b/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module index f8b140e2..8c7ac2c6 100644 --- a/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module +++ b/public/modules/custom/helfi_rekry_content/helfi_rekry_content.module @@ -290,7 +290,13 @@ function _helfi_rekry_content_add_schema(string|NULL $url = NULL): ?string { function helfi_rekry_content_theme() : array { return [ 'organization_information_block' => [ - 'render element' => 'elements', + 'variables' => [ + 'city_description_title' => NULL, + 'city_description_text' => NULL, + 'organization_title' => NULL, + 'organization_image' => NULL, + 'organization_description' => NULL, + ], ], ]; } diff --git a/public/themes/custom/hdbt_subtheme/templates/block/organization-information-block.html.twig b/public/themes/custom/hdbt_subtheme/templates/block/organization-information-block.html.twig index dcd181c9..22ce6f53 100644 --- a/public/themes/custom/hdbt_subtheme/templates/block/organization-information-block.html.twig +++ b/public/themes/custom/hdbt_subtheme/templates/block/organization-information-block.html.twig @@ -1,36 +1,30 @@
- {% if content.field_image|render %} - {{ content.field_image }} - {% elseif content.organization_default_image|render %} - {{ content.organization_default_image }} + {% if organization_image|render %} + {{ organization_image }} {% else %} {% include '@hdbt/media/image--card.html.twig' with {content: '' } %} {% endif %}
- {% if content.field_organization|render or content.field_organization_override|render %} + {% if organization_title %}

- {% if content.field_organization_override|render %} - {{ content.field_organization_override }} - {% else %} - {{ content.field_organization }} - {% endif %} + {{ organization_title }}

{% endif %} - {% if content.field_organization_description|render or content.field_organization_description_o|render %} + {% if organization_description|render %}
- {{ content.field_organization_description }} + {{ organization_description }}
{% endif %}
- {% if content.city_description_title|render or content.city_description_text|render %} + {% if city_description_title|render or city_description_text|render %}

- {{ content.city_description_title }} + {{ city_description_title }}

- {{ content.city_description_text|nl2br }} + {{ city_description_text|nl2br }}
{% endif %} 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 3/9] 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; From 0c8790199c382676ff954cd47e98233a62e689ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Mon, 4 Nov 2024 12:45:33 +0200 Subject: [PATCH 4/9] Moved the render array build to the block build. --- .../src/Entity/JobListing.php | 37 ++----------------- .../Plugin/Block/OrganizationInformation.php | 21 ++++++++++- 2 files changed, 24 insertions(+), 34 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 03fd365b..737fdf12 100644 --- a/public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php +++ b/public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php @@ -5,6 +5,7 @@ namespace Drupal\helfi_rekry_content\Entity; use Drupal\Core\Entity\EntityInterface; +use Drupal\filter\Render\FilteredMarkup; use Drupal\node\Entity\Node; use Drupal\taxonomy\TermInterface; @@ -125,7 +126,7 @@ protected function getOrganization(string $organization_id) : EntityInterface|bo * * @throws \Drupal\Core\TypedData\Exception\MissingDataException */ - protected function getOrganizationOverride() : TermInterface|bool { + public function getOrganizationOverride() : TermInterface|bool { $organization_id = ''; // Get the organization id from the migrated field. @@ -183,10 +184,10 @@ public function getOrganizationDefaultImage(TermInterface|false $organization) : * @param \Drupal\taxonomy\TermInterface|false $organization * Organization entity. * - * @return array + * @return \Drupal\filter\Render\FilteredMarkup|string * Organization description as a render array. */ - public function getOrganizationDescription(TermInterface|false $organization) : array { + public function getOrganizationDescription(TermInterface|false $organization) : FilteredMarkup|string { // Set organization description from node. $organization_description = $this->get('field_organization_description'); @@ -203,34 +204,4 @@ public function getOrganizationDescription(TermInterface|false $organization) : 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 85731bdf..054386a7 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 @@ -34,7 +34,26 @@ public function build() : array { // Add the organization information to render array. if ($entity instanceof JobListing) { - $build = $build + $entity->buildOrganization(); + // Get the City's title and description from the configuration. + $build = $build + $entity->getCityDescriptions(); + + // Get the organization entity. + try { + $organization = $entity->getOrganizationOverride(); + + if ($organization) { + // Set organization image. + $build['#organization_image'] = $entity->getOrganizationDefaultImage($organization); + + // Set the organization title. + $build['#organization_title'] = $organization->getName(); + + // Set the organization description. + $build['#organization_description'] = $entity->getOrganizationDescription($organization); + } + } + catch (\Exception $e) { + } } return $build; From daff5d48bcc00c22e637f38f06355ef0870f53f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Mon, 4 Nov 2024 13:02:38 +0200 Subject: [PATCH 5/9] Simplify the organization information getters. --- .../src/Entity/JobListing.php | 50 +++++++------------ .../Plugin/Block/OrganizationInformation.php | 17 ++----- 2 files changed, 21 insertions(+), 46 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 737fdf12..dcd57708 100644 --- a/public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php +++ b/public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php @@ -7,7 +7,6 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\filter\Render\FilteredMarkup; use Drupal\node\Entity\Node; -use Drupal\taxonomy\TermInterface; /** * Bundle class for JobListing paragraph. @@ -99,34 +98,14 @@ public function getCityDescriptions() : array { } /** - * Get organization entity. + * Get organization taxonomy term. * - * @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 + * @return \Drupal\Core\Entity\EntityInterface|bool * Returns the organization taxonomy term or false if not set. * * @throws \Drupal\Core\TypedData\Exception\MissingDataException */ - public function getOrganizationOverride() : TermInterface|bool { + public function getOrganization() : EntityInterface|bool { $organization_id = ''; // Get the organization id from the migrated field. @@ -145,21 +124,27 @@ public function getOrganizationOverride() : TermInterface|bool { ->getValue(); } - return $this->getOrganization($organization_id); + try { + return \Drupal::entityTypeManager() + ->getStorage('taxonomy_term') + ->load($organization_id); + } + catch (\Exception $e) { + return FALSE; + } } /** * 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 { + public function getOrganizationDefaultImage() : array { + $organization = $this->getOrganization(); + if ($organization && !$organization->get('field_default_image')->isEmpty()) { return $organization->get('field_default_image')->first()->view([ 'type' => 'responsive_image', @@ -181,13 +166,12 @@ public function getOrganizationDefaultImage(TermInterface|false $organization) : /** * Get organization description. * - * @param \Drupal\taxonomy\TermInterface|false $organization - * Organization entity. - * * @return \Drupal\filter\Render\FilteredMarkup|string * Organization description as a render array. */ - public function getOrganizationDescription(TermInterface|false $organization) : FilteredMarkup|string { + public function getOrganizationDescription() : FilteredMarkup|string { + $organization = $this->getOrganization(); + // Set organization description from node. $organization_description = $this->get('field_organization_description'); 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 054386a7..1834167a 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 @@ -37,20 +37,11 @@ public function build() : array { // Get the City's title and description from the configuration. $build = $build + $entity->getCityDescriptions(); - // Get the organization entity. + // Get the organization entity and set the necessary variables. try { - $organization = $entity->getOrganizationOverride(); - - if ($organization) { - // Set organization image. - $build['#organization_image'] = $entity->getOrganizationDefaultImage($organization); - - // Set the organization title. - $build['#organization_title'] = $organization->getName(); - - // Set the organization description. - $build['#organization_description'] = $entity->getOrganizationDescription($organization); - } + $build['#organization_image'] = $entity->getOrganizationDefaultImage(); + $build['#organization_title'] = $entity->getOrganizationName(); + $build['#organization_description'] = $entity->getOrganizationDescription(); } catch (\Exception $e) { } From 774c10448ac98b97473491600e5263e95ad8e6e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Mon, 4 Nov 2024 13:22:07 +0200 Subject: [PATCH 6/9] Use the term interface instead of entity interface. --- .../helfi_rekry_content/src/Entity/JobListing.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 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 dcd57708..1dc1b259 100644 --- a/public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php +++ b/public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php @@ -4,9 +4,9 @@ namespace Drupal\helfi_rekry_content\Entity; -use Drupal\Core\Entity\EntityInterface; use Drupal\filter\Render\FilteredMarkup; use Drupal\node\Entity\Node; +use Drupal\taxonomy\TermInterface; /** * Bundle class for JobListing paragraph. @@ -100,12 +100,12 @@ public function getCityDescriptions() : array { /** * Get organization taxonomy term. * - * @return \Drupal\Core\Entity\EntityInterface|bool + * @return \Drupal\taxonomy\TermInterface|bool * Returns the organization taxonomy term or false if not set. * * @throws \Drupal\Core\TypedData\Exception\MissingDataException */ - public function getOrganization() : EntityInterface|bool { + public function getOrganization() : TermInterface|bool { $organization_id = ''; // Get the organization id from the migrated field. @@ -125,9 +125,11 @@ public function getOrganization() : EntityInterface|bool { } try { - return \Drupal::entityTypeManager() + /** @var \Drupal\taxonomy\TermInterface $organization */ + $organization = $this->entityTypeManager() ->getStorage('taxonomy_term') ->load($organization_id); + return $organization; } catch (\Exception $e) { return FALSE; From 2981bc449126ef881cecec696962163ceb7712e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Mon, 4 Nov 2024 14:41:24 +0200 Subject: [PATCH 7/9] New method for getting a translated taxonomy term title. --- .../src/Entity/JobListing.php | 27 +++++++++++++++++++ .../Plugin/Block/OrganizationInformation.php | 14 +++++----- 2 files changed, 35 insertions(+), 6 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 1dc1b259..381b9cc0 100644 --- a/public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php +++ b/public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php @@ -4,6 +4,7 @@ namespace Drupal\helfi_rekry_content\Entity; +use Drupal\Core\Language\LanguageInterface; use Drupal\filter\Render\FilteredMarkup; use Drupal\node\Entity\Node; use Drupal\taxonomy\TermInterface; @@ -165,11 +166,37 @@ public function getOrganizationDefaultImage() : array { return $this->get('field_image')->value ?? []; } + /** + * Get the translated organization name. + * + * @return string + * Returns the translated organization name. + * + * @throws \Drupal\Core\TypedData\Exception\MissingDataException + */ + public function getTranslatedOrganisationName() : string { + $organization = $this->getOrganization(); + $organization_name = ''; + + if ($organization) { + $langcode = $this->languageManager() + ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) + ->getId(); + + $organization_name = $organization->hasTranslation($langcode) + ? $organization->getTranslation($langcode)->getName() + : $organization->getName() ?? ''; + } + return $organization_name; + } + /** * Get organization description. * * @return \Drupal\filter\Render\FilteredMarkup|string * Organization description as a render array. + * + * @throws \Drupal\Core\TypedData\Exception\MissingDataException */ public function getOrganizationDescription() : FilteredMarkup|string { $organization = $this->getOrganization(); 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 1834167a..2b19d650 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 @@ -4,17 +4,19 @@ namespace Drupal\helfi_rekry_content\Plugin\Block; +use Drupal\Core\Block\Attribute\Block; +use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\helfi_platform_config\Plugin\Block\ContentBlockBase; use Drupal\helfi_rekry_content\Entity\JobListing; /** * Provides a 'OrganizationInformation' block. - * - * @Block( - * id = "organization_information_block", - * admin_label = @Translation("Organization information block"), - * ) */ +#[Block( + id: "organization_information_block", + admin_label: new TranslatableMarkup("Organization information block"), +)] final class OrganizationInformation extends ContentBlockBase { /** @@ -40,7 +42,7 @@ public function build() : array { // Get the organization entity and set the necessary variables. try { $build['#organization_image'] = $entity->getOrganizationDefaultImage(); - $build['#organization_title'] = $entity->getOrganizationName(); + $build['#organization_title'] = $entity->getTranslatedOrganisationName(); $build['#organization_description'] = $entity->getOrganizationDescription(); } catch (\Exception $e) { From 73375de77a2210b483306a73d997b8c43cbe9a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Mon, 4 Nov 2024 15:27:47 +0200 Subject: [PATCH 8/9] Fixed the render array for the joblisting image field. --- .../src/Entity/JobListing.php | 50 ++++++++++++++----- .../Plugin/Block/OrganizationInformation.php | 1 - 2 files changed, 37 insertions(+), 14 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 381b9cc0..89fef6f7 100644 --- a/public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php +++ b/public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php @@ -146,24 +146,48 @@ public function getOrganization() : TermInterface|bool { * @throws \Drupal\Core\TypedData\Exception\MissingDataException */ public function getOrganizationDefaultImage() : array { + $image_style = [ + 'type' => 'responsive_image', + 'label' => 'hidden', + 'settings' => [ + 'responsive_image_style' => 'job_listing_org', + 'image_link' => '', + 'image_loading' => [ + 'attribute' => 'eager', + ], + ], + ]; + + // Return the JobListing image field if it is set. + if (!$this->get('field_image')->isEmpty()) { + + /** @var \Drupal\Core\Entity\Plugin\DataType\EntityReference $entity_reference */ + $entity_reference = $this->get('field_image') + ?->first() + ?->get('entity'); + + /** @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter $entity_adapter */ + $entity_adapter = $entity_reference?->getTarget(); + + /** @var \Drupal\media\Entity\Media $media */ + $media = $entity_adapter?->getEntity(); + + // Render array of the image. + return $media + ?->get('field_media_image') + ?->first() + ?->view($image_style) ?? []; + } + $organization = $this->getOrganization(); + // Return the organization default image if it is set. 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 $organization->get('field_default_image')->first()->view($image_style); } - // Return the JobListing image. - return $this->get('field_image')->value ?? []; + // Return an empty array if no image is found. + return []; } /** 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 2b19d650..f8df3edf 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,6 @@ namespace Drupal\helfi_rekry_content\Plugin\Block; use Drupal\Core\Block\Attribute\Block; -use Drupal\Core\Language\LanguageInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\helfi_platform_config\Plugin\Block\ContentBlockBase; use Drupal\helfi_rekry_content\Entity\JobListing; From c15773f5a5272e8931a8ccb372c038a014c86b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Mon, 4 Nov 2024 15:34:38 +0200 Subject: [PATCH 9/9] Removed unused use statement. --- public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme | 1 - 1 file changed, 1 deletion(-) diff --git a/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme b/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme index 95bb67f9..555e9d6c 100644 --- a/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme +++ b/public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme @@ -8,7 +8,6 @@ declare(strict_types=1); use Drupal\Core\Language\LanguageInterface; -use Drupal\Core\Render\Element; use Drupal\Core\Url; use Drupal\node\NodeInterface;