Skip to content

Commit

Permalink
Merge pull request #336 from City-of-Helsinki/UHF-8705
Browse files Browse the repository at this point in the history
UHF-8705: Structured data for Job listings
  • Loading branch information
teroelonen authored Oct 18, 2023
2 parents 6947231 + 62c8e42 commit dd0973e
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@ declare(strict_types = 1);

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\helfi_rekry_content\Entity\JobListing;
use Drupal\media\OEmbed\ProviderException;
use Drupal\media\OEmbed\ResourceException;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\node\NodeInterface;
use Drupal\paragraphs\ParagraphInterface;

/**
* Implements hook_entity_bundle_info_alter().
*/
function helfi_rekry_content_entity_bundle_info_alter(array &$bundles): void {
if (isset($bundles['node']['job_listing'])) {
$bundles['node']['job_listing']['class'] = JobListing::class;
}
}

/**
* Implements hook_form_FORM_ID_alter().
*/
Expand Down Expand Up @@ -436,3 +447,90 @@ function helfi_rekry_content_preprocess_block(&$variables) {
$first_paragraph_gray = 'has-first-gray-bg-block';
}
}

/**
* Implements hook_page_attachments().
*/
function helfi_rekry_content_page_attachments(array &$attachments) : void {

// Get current entity and entity version.
$entity_matcher = \Drupal::service('helfi_platform_config.entity_version_matcher')->getType();

/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $entity_matcher['entity'];

// No need to continue if entity is not job listing.
if (!$entity instanceof JobListing) {
return;
}

/** @var \Drupal\helfi_rekry_content\Entity\JobListing $job_listing */
$job_listing = $entity;

/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = Drupal::service('date.formatter');

$publication_starts_datetime = $job_listing->get('field_publication_starts')->date->getTimestamp();
$publication_starts_formatted = $date_formatter->format($publication_starts_datetime, 'html_date');

/** @var Drupal\Core\Extension\ThemeHandler $theme_handler */
$theme_handler = Drupal::service('theme_handler');

// Get the default share image as structured data logo.
if ($theme_handler->themeExists('hdbt')) {
$theme = $theme_handler->getTheme('hdbt');
$current_language = \Drupal::languageManager()
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
$logo_file_name = $current_language === 'sv' ? 'og-global-sv.png' : 'og-global.png';

/** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
$file_url_generator = \Drupal::service('file_url_generator');
$logo_image_url = $file_url_generator->generate("{$theme->getPath()}/src/images/{$logo_file_name}")
->setAbsolute()
->toString();
}

$structured_data = json_encode([
'@context' => 'https://schema.org/',
'@type' => 'JobPosting',
'title' => $job_listing->getTitle(),
'description' => $job_listing->getJobDescription(),
'identifier' => [
'@type' => 'PropertyValue',
'name' => t('City of Helsinki'),
'value' => $job_listing->get('field_recruitment_id')->value,
],
'datePosted' => $publication_starts_formatted,
'validThrough' => $job_listing->get('field_publication_ends')->value,
'employmentType' => $job_listing->getEmploymentType(),
'hiringOrganization' => [
'@type' => 'Organization',
'name' => $job_listing->getOrganizationName(),
'sameAs' => 'https://hel.fi/',
'logo' => $logo_image_url,
],
'jobLocation' => [
'@type' => 'Place',
'address' => [
'@type' => 'PostalAddress',
'streetAddress' => $job_listing->get('field_address')->value,
'addressRegion' => $job_listing->get('field_postal_area')->value,
'postalCode' => $job_listing->get('field_postal_code')->value,
'addressCountry' => t('Finland'),
],
],
]);

if (array_key_exists('#attached', $attachments)) {
$attachments['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#attributes' => [
'type' => 'application/ld+json',
],
'#value' => $structured_data,
],
'structured_job_listing_data',
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Drupal\helfi_rekry_content\Entity;

use Drupal\node\Entity\Node;

/**
* Bundle class for hel_map paragraph.
*/
class JobListing extends Node {

/**
* Get job description or override value.
*
* @return string
* Job description.
*/
public function getJobDescription() : string {
return $this->get('field_job_description_override')->value ?: $this->get('job_description')->value;
}

/**
* Get translated organization name if available or override value.
*
* @return string
* Organization name.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\TypedData\Exception\MissingDataException
*/
public function getOrganizationName() : string {
if (!$this->get('field_organization_override')->first()) {
return $this->get('field_organization_name')->value;
}

$storage = $this->entityTypeManager()
->getStorage('taxonomy_term');

$organization_entity = $storage->load($this->get('field_organization_override')->first()->target_id);

if (!$organization_entity->hasTranslation($this->get('langcode')->value)) {
return $organization_entity->getName();
}

$translated_organization_entity = $organization_entity->getTranslation($this->get('langcode')->value);
return $translated_organization_entity->getName();
}

/**
* Get translated employment type if available.
*
* @return string
* Employment type.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\TypedData\Exception\MissingDataException
*/
public function getEmploymentType() : string {
if (!$this->get('field_employment_type')->first()) {
return '';
}

$storage = $this->entityTypeManager()
->getStorage('taxonomy_term');

$employment_type_entity = $storage->load($this->get('field_employment_type')->first()->target_id);

if (!$employment_type_entity->hasTranslation($this->get('langcode')->value)) {
return $employment_type_entity->getName();
}

$translated_employment_type_entity = $employment_type_entity->getTranslation($this->get('langcode')->value);
return $translated_employment_type_entity->getName();

}

}

0 comments on commit dd0973e

Please sign in to comment.