Skip to content

Commit

Permalink
Merge pull request #587 from City-of-Helsinki/UHF-10906_job_listing_r…
Browse files Browse the repository at this point in the history
…efactor

UHF-10906 job listing refactor
  • Loading branch information
khalima authored Nov 4, 2024
2 parents 88dde65 + c15773f commit db64b0f
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
],
];
}
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 @@ -80,4 +83,162 @@ 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 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,27 +4,26 @@

namespace Drupal\helfi_rekry_content\Plugin\Block;

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\helfi_platform_config\Plugin\Block\ContentBlockBase;
use Drupal\node\Entity\Node;
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 {

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

Expand All @@ -35,14 +34,18 @@ 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) {
// 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
61 changes: 0 additions & 61 deletions public/themes/custom/hdbt_subtheme/hdbt_subtheme.theme
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -51,66 +50,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().
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
<div class="job-listing__sidebar">
<div class="job-listing__image">
{% 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 %}
</div>
<div class="job-listing__organization-information">
{% if content.field_organization|render or content.field_organization_override|render %}
{% if organization_title %}
<h2 class="job-listing__organization">
{% if content.field_organization_override|render %}
{{ content.field_organization_override }}
{% else %}
{{ content.field_organization }}
{% endif %}
{{ organization_title }}
</h2>
{% endif %}
{% if content.field_organization_description|render or content.field_organization_description_o|render %}
{% if organization_description|render %}
<div class="job-listing__organization-description user-edited-content">
{{ content.field_organization_description }}
{{ organization_description }}
</div>
{% endif %}
</div>
{% if content.city_description_title|render or content.city_description_text|render %}
{% if city_description_title|render or city_description_text|render %}
<div class="job-listing__city-description">
<h2 class="job-listing__city-description-title">
{{ content.city_description_title }}
{{ city_description_title }}
</h2>
<div class="job-listing__city-description-text user-edited-content">
{{ content.city_description_text|nl2br }}
{{ city_description_text|nl2br }}
</div>
</div>
{% endif %}
Expand Down

0 comments on commit db64b0f

Please sign in to comment.