Skip to content

Commit

Permalink
HPC-9191: Use in-link image crop for any node that uses our custom he…
Browse files Browse the repository at this point in the history
…ro image field
  • Loading branch information
berliner committed Nov 21, 2023
1 parent 3d2900c commit 9595432
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
6 changes: 3 additions & 3 deletions config/metatag.metatag_defaults.node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ label: Content
tags:
canonical_url: '[node:url]'
description: '[node:summary]'
image_src: '[node:field_hero_image]'
og_image: '[node:field_hero_image]'
image_src: '[node:field_hero_image:16x9_1200]'
og_image: '[node:field_hero_image:16x9_1200]'
title: '[node:title] | [site:name]'
twitter_cards_donottrack: 'on'
twitter_cards_image: '[node:field_hero_image]'
twitter_cards_image: '[node:field_hero_image:16x9_1200]'
twitter_cards_site: '@UNOCHA'
twitter_cards_title: '[node:title]'
twitter_cards_type: summary_large_image
19 changes: 19 additions & 0 deletions html/modules/custom/ghi_hero_image/ghi_hero_image.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\ghi_hero_image\HeroImageWidgetCropManager;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
Expand Down Expand Up @@ -166,3 +167,21 @@ function ghi_hero_image_normalize_content_alter(&$data) {
unset($data[$field_name][0]['height']);
}
}

/**
* Implements hook_tokens_alter().
*/
function ghi_hero_image_tokens_alter(array &$replacements, array $context, BubbleableMetadata $bubbleable_metadata) {
if ($context['type'] != 'node' || empty($context['data']['node'])) {
return;
}
/** @var \Drupal\node\NodeInterface $node */
$node = $context['data']['node'];
if (!$node->hasField('field_hero_image')) {
return;
}

/** @var \Drupal\ghi_hero_image\HeroImageManager $hero_image_manager */
$hero_image_manager = \Drupal::service('hero_image.manager');
$hero_image_manager->tokenAlter($replacements, $node);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
hero_image.manager:
class: Drupal\ghi_hero_image\HeroImageManager
arguments: ['@plugin.manager.endpoint_query_manager', '@smugmug_api.image', '@file_system']
arguments: ['@entity_type.manager', '@plugin.manager.endpoint_query_manager', '@smugmug_api.image', '@file_system']
hero_image_widget_crop.manager:
class: Drupal\ghi_hero_image\HeroImageWidgetCropManager
arguments: ['@entity_type.manager', '@config.factory', '@hero_image.manager', '@webp.webp', '@file.mime_type.guesser']
33 changes: 31 additions & 2 deletions html/modules/custom/ghi_hero_image/src/HeroImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\ghi_hero_image;

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterInterface;
use Drupal\Core\File\FileSystemInterface;
Expand All @@ -16,6 +17,13 @@
*/
class HeroImageManager {

/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* The attachment query.
*
Expand All @@ -38,9 +46,10 @@ class HeroImageManager {
protected $fileSystem;

/**
* Constructs a section create form.
* Constructs a manager class for hero images.
*/
public function __construct(EndpointQueryManager $endpoint_query_manager, Image $smugmug_image, FileSystemInterface $file_system) {
public function __construct(EntityTypeManagerInterface $entity_type_manager, EndpointQueryManager $endpoint_query_manager, Image $smugmug_image, FileSystemInterface $file_system) {
$this->entityTypeManager = $entity_type_manager;
$this->entitiesQuery = $endpoint_query_manager->createInstance('plan_entities_query');
$this->smugmugImage = $smugmug_image;
$this->fileSystem = $file_system;
Expand Down Expand Up @@ -190,4 +199,24 @@ public function deleteImageFile($uri) {
$this->fileSystem->delete($uri);
}

/**
* Alter tokens for hero images.
*/
public function tokenAlter(&$replacements, $node) {
if (!$node->hasField('field_hero_image')) {
return;
}

$image_properties = $this->getImageProperties($node->field_hero_image);
if (empty($image_properties['file_uri'])) {
return;
}

/** @var \Drupal\image\Entity\ImageStyle[] $image_styles */
$image_styles = $this->entityTypeManager->getStorage('image_style')->loadMultiple();
foreach ($image_styles as $id => $style) {
$replacements['[node:field_hero_image:' . $id . ']'] = $style->buildUrl($image_properties['file_uri']);
}
}

}

0 comments on commit 9595432

Please sign in to comment.