Skip to content

Commit

Permalink
UHF-9712: Implement OGImageBuilderInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrsky committed Mar 12, 2024
1 parent f417fc4 commit f3d678b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 96 deletions.
6 changes: 3 additions & 3 deletions helfi_tpr.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ services:
arguments: ['@config.factory']
tags:
- { name: event_subscriber }
helfi_tpr.token_subscriber:
class: Drupal\helfi_tpr\EventSubscriber\TokenSubscriber
helfi_tpr.og_image.tpr_entity:
class: Drupal\helfi_tpr\Token\TprImageBuilder
arguments:
- '@entity_type.manager'
tags:
- { name: event_subscriber }
- { name: helfi_api_base.og_image_builder }
20 changes: 16 additions & 4 deletions helfi_tpr.tokens.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*/

use Drupal\Core\Render\BubbleableMetadata;
use Drupal\helfi_tpr\Entity\Service;
use Drupal\helfi_tpr\Entity\Unit;

/**
* Implements hook_token_info().
*
* @see \Drupal\helfi_tpr\Token\TokenSubscriber
*/
function helfi_tpr_token_info() {
$info = [];
Expand Down Expand Up @@ -43,7 +43,7 @@ function helfi_tpr_token_info() {
}
}

$info['tokens']['tpr_unit']['shareable-image'] = [
$info['tokens']['tpr_unit']['picture'] = [
'name' => t('Picture'),
];
$info['tokens']['tpr_unit']['description:value'] = [
Expand Down Expand Up @@ -76,12 +76,24 @@ function helfi_tpr_tokens(
if (!in_array($type, $entity_types) || empty($data[$type])) {
return [];
}

/** @var \Drupal\helfi_tpr\Entity\TprEntityBase $entity */
$entity = $data[$type];
$replacements = [];

foreach ($tokens as $name => $original) {
if ($name === 'label') {
$replacements[$original] = $entity->label();
}

if ($entity instanceof Unit || $entity instanceof Service) {
if ($name === 'description:value') {
$replacements[$original] = $entity->getDescription('value');
}
if ($name === 'description:summary') {
$replacements[$original] = $entity->getDescription('summary');
}
}

// Custom token for entity URL alias with menu parents.
if ($name == 'menu-link-parents') {
// Get entity menu link.
Expand Down
89 changes: 0 additions & 89 deletions src/EventSubscriber/TokenSubscriber.php

This file was deleted.

46 changes: 46 additions & 0 deletions src/Token/TprImageBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_tpr\Token;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\helfi_api_base\Token\OGImageBuilderInterface;
use Drupal\helfi_tpr\Entity\Unit;

/**
* OG image for tpr entities.
*/
class TprImageBuilder implements OGImageBuilderInterface {

/**
* Constructs a new instance.
*/
public function __construct(
private readonly EntityTypeManagerInterface $entityTypeManager,
) {
}

/**
* {@inheritDoc}
*/
public function applies(EntityInterface $entity): bool {
return $entity instanceof Unit;
}

/**
* {@inheritDoc}
*/
public function buildUrl(EntityInterface $entity): ?string {
assert($entity instanceof Unit);

/** @var \Drupal\image\ImageStyleInterface $image_style */
$image_style = $this->entityTypeManager
->getStorage('image_style')
->load('og_image');

return $entity->getPictureUrl($image_style);
}

}

0 comments on commit f3d678b

Please sign in to comment.