Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-X: Use OGImageBuilder service for news node tokens #595

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions public/modules/custom/helfi_etusivu/helfi_etusivu.module
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\helfi_platform_config\DTO\ParagraphTypeCollection;
use Drupal\image\Entity\ImageStyle;
use Drupal\media\MediaInterface;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\Plugin\views\query\Sql;
use Drupal\views\ViewExecutable;
Expand Down Expand Up @@ -61,51 +58,6 @@ function helfi_etusivu_views_query_alter(ViewExecutable $view, QueryPluginBase $
}
}

/**
* Implements hook_tokens().
*/
function helfi_etusivu_tokens(
$type,
$tokens,
array $data,
array $options,
BubbleableMetadata $bubbleable_metadata
) : array {
$replacements = [];

foreach ($tokens as $name => $original) {

// Custom token for shareable-image.
if ($name === 'shareable-image' && !empty($data['node'])) {
/** @var \Drupal\node\Entity\Node $node */
$node = $data['node'];

if (
$node->hasField('field_main_image') &&
isset($node->field_main_image->entity) &&
$node->field_main_image->entity instanceof MediaInterface &&
$node->field_main_image->entity->hasField('field_media_image')
) {
$image_style = ImageStyle::load('og_image');

// If main image has an image set, use it as the shareable image.
// @phpstan-ignore-next-line
$image_entity = $node->get('field_main_image')->entity->field_media_image;

// Skip current entity if it's empty.
if ($image_entity->isEmpty()) {
break;
}

$image_path = $image_entity->entity->getFileUri();
$replacements[$original] = $image_style->buildUrl($image_path);
}
}
}

return $replacements;
}

/**
* Implements hook_cron().
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
Drupal\helfi_etusivu\Token\NewsNodeImageBuilder:
class: Drupal\helfi_etusivu\Token\NewsNodeImageBuilder
tags:
- { name: helfi_platform_config.og_image_builder }
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

tuutti marked this conversation as resolved.
Show resolved Hide resolved
declare(strict_types=1);

namespace Drupal\helfi_etusivu\Token;

use Drupal\Core\Entity\EntityInterface;
use Drupal\helfi_platform_config\Token\OGImageBuilderInterface;
use Drupal\media\MediaInterface;
use Drupal\node\NodeInterface;

/**
* OG image for nodes.
*/
class NewsNodeImageBuilder implements OGImageBuilderInterface {

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

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

if (
$entity->hasField('field_main_image') &&
isset($entity->field_main_image->entity) &&
$entity->field_main_image->entity instanceof MediaInterface &&
$entity->field_main_image->entity->hasField('field_media_image')
) {
// If main image has an image set, use it as the shareable image.
// @phpstan-ignore-next-line
$image_entity = $entity->get('field_main_image')->entity->field_media_image;

// Skip current entity if it's empty.
if (!$image_entity->isEmpty()) {
return $image_entity->entity->getFileUri();
}
}

return NULL;
}

}
38 changes: 37 additions & 1 deletion tests/dtt/src/ExistingSite/NewsContentTypeTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\Tests\dtt\ExistingSite;

use Drupal\Core\Session\AccountInterface;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use Drupal\Tests\helfi_api_base\Functional\ExistingSiteTestBase;
use Drupal\Tests\TestFileCreationTrait;

/**
* Tests news endpoint.
Expand All @@ -14,6 +17,8 @@
*/
class NewsContentTypeTest extends ExistingSiteTestBase {

use TestFileCreationTrait;

/**
* The administrator account.
*
Expand Down Expand Up @@ -83,4 +88,35 @@ public function testEndpointPermissions() : void {
$this->assertJsonApiList();
}

/**
* Metatag og:image should work with news content.
*/
public function testNewsOgImage() : void {
$uri = $this->getTestFiles('image')[0]->uri;

$file = File::create([
'uri' => $uri,
]);
$file->save();
$this->markEntityForCleanup($file);

$media = Media::create([
'bundle' => 'image',
'name' => 'Custom name',
'field_media_image' => $file->id(),
]);
$media->save();
$this->markEntityForCleanup($file);

$node = $this->createNode([
'type' => 'news_item',
'status' => 1,
'langcode' => 'fi',
'field_main_image' => $media->id(),
]);

$this->drupalGet($node->toUrl());
$this->assertSession()->elementAttributeContains('css', 'meta[property="og:image"]', 'content', $file->getFilename());
}

}