From f3d678bbd6fef3dc7a93aeef589963d89ffb6e23 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Tue, 12 Mar 2024 08:59:12 +0200 Subject: [PATCH] UHF-9712: Implement OGImageBuilderInterface --- helfi_tpr.services.yml | 6 +- helfi_tpr.tokens.inc | 20 ++++-- src/EventSubscriber/TokenSubscriber.php | 89 ------------------------- src/Token/TprImageBuilder.php | 46 +++++++++++++ 4 files changed, 65 insertions(+), 96 deletions(-) delete mode 100644 src/EventSubscriber/TokenSubscriber.php create mode 100644 src/Token/TprImageBuilder.php diff --git a/helfi_tpr.services.yml b/helfi_tpr.services.yml index 1de3d50..06520fc 100644 --- a/helfi_tpr.services.yml +++ b/helfi_tpr.services.yml @@ -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 } diff --git a/helfi_tpr.tokens.inc b/helfi_tpr.tokens.inc index 71a77db..d89916c 100644 --- a/helfi_tpr.tokens.inc +++ b/helfi_tpr.tokens.inc @@ -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 = []; @@ -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'] = [ @@ -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. diff --git a/src/EventSubscriber/TokenSubscriber.php b/src/EventSubscriber/TokenSubscriber.php deleted file mode 100644 index 9536da7..0000000 --- a/src/EventSubscriber/TokenSubscriber.php +++ /dev/null @@ -1,89 +0,0 @@ -type, $entity_types) || empty($event->data[$event->type])) { - return; - } - - /** @var \Drupal\helfi_tpr\Entity\TprEntityBase $entity */ - $entity = $event->data[$event->type]; - - switch ($event->token) { - case 'label': - $event->setReplacementValue($entity->label()); - break; - - case 'description:value': - if ($entity instanceof Unit || $entity instanceof Service) { - $event->setReplacementValue($entity->getDescription('value')); - } - break; - - case 'description:summary': - if ($entity instanceof Unit || $entity instanceof Service) { - $event->setReplacementValue($entity->getDescription('summary')); - } - break; - - // Picture token is replaced for backwards compatability. The - // shareable-image token is handled by other event subscribers - // in helfi platform and may already have default image that this - // handler overrides. - case 'picture': - case 'shareable-image': - if ($entity instanceof Unit) { - /** @var \Drupal\image\ImageStyleInterface $image_style */ - $image_style = $this->entityTypeManager - ->getStorage('image_style') - ->load('og_image'); - - $event->setReplacementValue($entity->getPictureUrl($image_style)); - } - break; - } - } - - /** - * {@inheritdoc} - */ - public static function getSubscribedEvents(): array { - return [ - TokenEvent::class => ['handleTokens'], - ]; - } - -} diff --git a/src/Token/TprImageBuilder.php b/src/Token/TprImageBuilder.php new file mode 100644 index 0000000..7e53258 --- /dev/null +++ b/src/Token/TprImageBuilder.php @@ -0,0 +1,46 @@ +entityTypeManager + ->getStorage('image_style') + ->load('og_image'); + + return $entity->getPictureUrl($image_style); + } + +}