Skip to content

Commit

Permalink
Merge pull request #588 from City-of-Helsinki/UHF-10906_job_listing_c…
Browse files Browse the repository at this point in the history
…herry_pick

UHF-10906 Job listing refactor
  • Loading branch information
khalima authored Nov 4, 2024
2 parents a5c71bd + abff0f7 commit 18cf9f4
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,16 @@ function _helfi_rekry_content_add_schema(string|NULL $url = NULL): ?string {
/**
* Implements hook_theme().
*/
function helfi_rekry_content_theme() {
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,
],
],
];
}
Expand Down
161 changes: 161 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,10 @@

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;

/**
* Bundle class for JobListing paragraph.
Expand Down Expand Up @@ -103,4 +106,162 @@ public function getFormattedStartTime(): string {
return $date_formatter->format($publication_starts_datetime, 'html_date');
}

/**
* 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 taxonomy term.
*
* @return \Drupal\taxonomy\TermInterface|bool
* Returns the organization taxonomy term or false if not set.
*
* @throws \Drupal\Core\TypedData\Exception\MissingDataException
*/
public function getOrganization() : 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();
}

try {
/** @var \Drupal\taxonomy\TermInterface $organization */
$organization = $this->entityTypeManager()
->getStorage('taxonomy_term')
->load($organization_id);
return $organization;
}
catch (\Exception $e) {
return FALSE;
}
}

/**
* Get organization default image.
*
* @return array
* Returns a render array of the image.
*
* @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($image_style);
}

// Return an empty array if no image is found.
return [];
}

/**
* 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();

// 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,93 +4,28 @@

namespace Drupal\helfi_rekry_content\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\helfi_platform_config\EntityVersionMatcher;
use Drupal\node\Entity\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Block\Attribute\Block;
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"),
* )
*/
final class OrganizationInformation extends BlockBase implements ContainerFactoryPluginInterface {

/**
* Constructs a new instance.
*
* @param array $configuration
* The configuration.
* @param string $plugin_id
* The plugin id.
* @param array $plugin_definition
* The plugin definition.
* @param \Drupal\helfi_platform_config\EntityVersionMatcher $entityVersionMatcher
* The entity version matcher.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
private readonly EntityVersionMatcher $entityVersionMatcher,
private readonly EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}

/**
* {@inheritdoc}
*/
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition,
) : self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('helfi_platform_config.entity_version_matcher'),
$container->get('entity_type.manager'),
);
}

/**
* {@inheritdoc}
*/
public function getCacheTags() {
$matcher = $this->entityVersionMatcher->getType();

if (
!$matcher['entity'] ||
$matcher['entity_version'] == EntityVersionMatcher::ENTITY_VERSION_REVISION
) {
return parent::getCacheTags();
}
return Cache::mergeTags(parent::getCacheTags(), $matcher['entity']->getCacheTags());
}

/**
* {@inheritDoc}
*/
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), ['route']);
}
#[Block(
id: "organization_information_block",
admin_label: new TranslatableMarkup("Organization information block"),
)]
final class OrganizationInformation extends ContentBlockBase {

/**
* {@inheritdoc}
*/
public function build() {
$build = [];
public function build() : array {
$build = [
'#theme' => 'organization_information_block',
'#cache' => ['tags' => $this->getCacheTags()],
];

// Get current entity and entity version.
$entity_matcher = $this->entityVersionMatcher->getType();
Expand All @@ -99,14 +34,18 @@ public function build() {
$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['organization_information'] = $view_builder->view($entity);
$build['organization_information']['#theme'] = 'organization_information_block';
if ($entity instanceof JobListing) {
// Get the City's title and description from the configuration.
$build = $build + $entity->getCityDescriptions();

// Get the organization entity and set the necessary variables.
try {
$build['#organization_image'] = $entity->getOrganizationDefaultImage();
$build['#organization_title'] = $entity->getTranslatedOrganisationName();
$build['#organization_description'] = $entity->getOrganizationDescription();
}
catch (\Exception $e) {
}
}

return $build;
Expand Down
Loading

0 comments on commit 18cf9f4

Please sign in to comment.