Skip to content

Commit

Permalink
Refactored the organization information block to use the JobListing b…
Browse files Browse the repository at this point in the history
…undle class.
  • Loading branch information
khalima committed Nov 4, 2024
1 parent 72dc03a commit 64fce0b
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 12 deletions.
153 changes: 153 additions & 0 deletions public/modules/custom/helfi_rekry_content/src/Entity/JobListing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()],
];

Expand All @@ -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;
Expand Down

0 comments on commit 64fce0b

Please sign in to comment.