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-9712 og image size #169

Closed
wants to merge 4 commits into from
Closed
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"drupal/address": "~1.0",
"drupal/readonly_field_widget": "^1.0",
"drupal/twig_tweak": "^2.0 || ^3.0",
"drupal/views_infinite_scroll": "^2.0"
"drupal/views_infinite_scroll": "^2.0",
"drupal/imagecache_external": "^3.0"
},
"conflict": {
"drupal/helfi_api_base": "<2.0"
Expand Down
1 change: 1 addition & 0 deletions helfi_tpr.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
- drupal:content_translation
- drupal:language
- readonly_field_widget:readonly_field_widget
- imagecache_external:imagecache_external
- drupal:media
- twig_tweak:twig_tweak
- drupal:telephone
Expand Down
6 changes: 6 additions & 0 deletions helfi_tpr.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ services:
arguments: ['@config.factory']
tags:
- { name: event_subscriber }
helfi_tpr.og_image.tpr_entity:
class: Drupal\helfi_tpr\Token\TprImageBuilder
arguments:
- '@entity_type.manager'
tags:
- { name: helfi_api_base.og_image_builder }
6 changes: 0 additions & 6 deletions helfi_tpr.tokens.inc
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ function helfi_tpr_tokens(
$replacements[$original] = $entity->label();
}

if ($entity instanceof Unit) {
if ($name === 'picture') {
$replacements[$original] = $entity->getPictureUrl();
}
}

if ($entity instanceof Unit || $entity instanceof Service) {
if ($name === 'description:value') {
$replacements[$original] = $entity->getDescription('value');
Expand Down
26 changes: 24 additions & 2 deletions src/Entity/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\image\Entity\ImageStyle;
use Webmozart\Assert\Assert;

/**
Expand Down Expand Up @@ -149,20 +150,41 @@ protected function getServiceIndex(Service $service) {
/**
* Gets the picture url.
*
* @param \Drupal\image\Entity\ImageStyle|null $imageStyle
* URL image style. Original image is returned if style is NULL.
*
* @return string|null
* The picture url.
*/
public function getPictureUrl() : ? string {
public function getPictureUrl(?ImageStyle $imageStyle = NULL) : ? string {
/** @var \Drupal\media\MediaInterface $picture_url */
$picture_url = $this->get('picture_url_override')->entity;

// Fallback to default picture url if override is not set.
if (!$picture_url) {
return $this->get('picture_url')->value;
$url = $this->get('picture_url')->value;

// Run image url through imagecache_external so that we
// can apply image style.
if ($imageStyle) {
$image_path = imagecache_external_generate_path($url);

if ($image_path) {
return $imageStyle->buildUrl($image_path);
}

return NULL;
}

return $url;
}

if ($file = $picture_url->get('field_media_image')->entity) {
/** @var \Drupal\file\FileInterface $file */
if ($imageStyle) {
return $imageStyle->buildUrl($file->getFileUri());
}

try {
return $file->createFileUrl(FALSE) ?: NULL;
}
Expand Down
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);
}

}
Loading