Skip to content

Commit

Permalink
UHF-9712: Move OGImageManager to helfi_platform_config
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrsky committed Mar 12, 2024
1 parent ae5f6d5 commit b5218e0
Show file tree
Hide file tree
Showing 14 changed files with 354 additions and 18 deletions.
51 changes: 51 additions & 0 deletions documentation/tokens.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Tokens

Helfi platform config implements `hook_tokens()`, and with the help of `helfi_platform_config.og_image_manager`, the `[*:shareable-image]` token is provided. Modules may implement services that handle this token for their entity types.

Modules should still implement `hook_tokens_info` to provide information about the implemented token.

## Defining image builder service

Add a new service:

```yml
# yourmodule/yourmodule.services.yml
yourmodule.og_image.your_entity_type:
class: Drupal\yourmodule\Token\YourEntityImageBuilder
arguments: []
tags:
- { name: helfi_platform_config.og_image_builder, priority: 100 }
```
```php
# yourmodule/src/Token/YourEntityImageBuilder.php
<?php

declare(strict_types=1);

namespace Drupal\yourmodule\Token;

use Drupal\helfi_platform_config\Token\OGImageBuilderInterface;

/**
* Handles token hooks.
*/
final class YourEntityImageBuilder implements OGImageBuilderInterface {

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

/**
* {@inheritDoc}
*/
public function buildUrl(EntityInterface $entity): ?string {
assert($entity instanceof YourEntity);
return $entity->field_image->entity->toUrl();
}

}
```
14 changes: 6 additions & 8 deletions helfi_platform_config.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,16 @@ services:
arguments:
- 'helfi_platform_config'

helfi_platform_config.og_image_manager:
class: Drupal\helfi_platform_config\Token\OGImageManager
tags:
- { name: service_collector, call: add, tag: helfi_platform_config.og_image_builder }

helfi_platform_config.og_image.default:
class: Drupal\helfi_platform_config\Token\DefaultImageBuilder
arguments:
- '@module_handler'
- '@language_manager'
- '@file_url_generator'
tags:
- { name: helfi_api_base.og_image_builder, priority: 100 }

helfi_platform_config.og_image.node:
class: Drupal\helfi_platform_config\Token\NodeImageBuilder
arguments:
- '@entity_type.manager'
tags:
- { name: helfi_api_base.og_image_builder }
- { name: helfi_platform_config.og_image_builder, priority: 100 }
41 changes: 41 additions & 0 deletions helfi_platform_config.tokens.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* @file
* Contains token data for helfi api base.
*/

declare(strict_types=1);

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\BubbleableMetadata;

/**
* Implements hook_tokens().
*
* @see \Drupal\helfi_platform_config\Token\OGImageManager
*/
function helfi_platform_config_tokens(
$type,
$tokens,
array $data,
array $options,
BubbleableMetadata $bubbleable_metadata
) : array {
$replacements = [];

foreach ($tokens as $name => $original) {
if ($name === 'shareable-image' && isset($data[$type])) {
$entity = $data[$type];

if ($entity instanceof EntityInterface) {
/** @var \Drupal\helfi_platform_config\Token\OGImageManager $image_manager */
$image_manager = \Drupal::service('helfi_platform_config.og_image_manager');

$replacements[$original] = $image_manager->buildUrl($entity);
}
}
}

return $replacements;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
helfi_platform_config_base.og_image.node:
class: Drupal\helfi_platform_config_base\Token\NodeImageBuilder
arguments:
- '@entity_type.manager'
tags:
- { name: helfi_platform_config.og_image_builder }
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Drupal\helfi_platform_config\Token;
namespace Drupal\helfi_platform_config_base\Token;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\file\FileInterface;
use Drupal\helfi_api_base\Token\OGImageBuilderInterface;
use Drupal\helfi_platform_config\Token\OGImageBuilderInterface;
use Drupal\media\MediaInterface;
use Drupal\node\NodeInterface;

Expand Down
1 change: 1 addition & 0 deletions modules/helfi_tpr_config/helfi_tpr_config.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies:
- 'helfi_base_content:helfi_base_content'
- 'helfi_media:helfi_media'
- 'helfi_paragraphs_content_liftup:helfi_paragraphs_content_liftup'
- 'helfi_image_styles:helfi_image_styles'
- 'helfi_tpr:helfi_tpr'
- 'imagecache_external:imagecache_external'
- 'views:views'
Expand Down
4 changes: 4 additions & 0 deletions modules/helfi_tpr_config/helfi_tpr_config.module
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use Drupal\Core\Url;
use Drupal\helfi_platform_config\DTO\ParagraphTypeCollection;
use Drupal\helfi_tpr_config\Entity\ServiceList;
use Drupal\helfi_tpr_config\Entity\ServiceListSearch;
use Drupal\helfi_tpr_config\Entity\Unit;
use Drupal\helfi_tpr_config\Entity\UnitSearch;
use Drupal\linkit\Entity\Profile;
use Drupal\paragraphs\Entity\Paragraph;
Expand All @@ -23,6 +24,9 @@ use Drupal\paragraphs\Entity\Paragraph;
* Implements hook_entity_bundle_info_alter().
*/
function helfi_tpr_config_entity_bundle_info_alter(array &$bundles): void {
if (isset($bundles['tpr_unit']['tpr_unit'])) {
$bundles['tpr_unit']['tpr_unit']['class'] = Unit::class;
}
if (isset($bundles['paragraph']['service_list'])) {
$bundles['paragraph']['service_list']['class'] = ServiceList::class;
}
Expand Down
7 changes: 7 additions & 0 deletions modules/helfi_tpr_config/helfi_tpr_config.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
helfi_tpr_config.og_image.tpr_entity:
class: Drupal\helfi_tpr_config\Token\TprImageBuilder
arguments:
- '@entity_type.manager'
tags:
- { name: helfi_platform_config.og_image_builder }
57 changes: 57 additions & 0 deletions modules/helfi_tpr_config/src/Entity/Unit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Drupal\helfi_tpr_config\Entity;

use \Drupal\helfi_tpr\Entity\Unit as BaseUnit;
use Drupal\image\Entity\ImageStyle;

class Unit extends BaseUnit {

/**
* Gets the picture url with image style.
*
* @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 getPictureUrlWithImageStyle(ImageStyle $imageStyle = NULL) : ? string {
/** @var \Drupal\media\MediaInterface $picture_url */
$picture_url = $this->get('picture_url_override')->entity;

//
if (!$picture_url) {
$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;
}
catch (\Exception) {
}
}
return NULL;
}

}
46 changes: 46 additions & 0 deletions modules/helfi_tpr_config/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_platform_config\Token\OGImageBuilderInterface;
use Drupal\helfi_tpr_config\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->getPictureUrlWithImageStyle($image_style);
}

}
14 changes: 7 additions & 7 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,30 @@ parameters:
path: modules/hdbt_admin_tools/src/Controller/ListController.php
-
message: '#^Access to an undefined property Drupal\\media\\MediaInterface::\$field_media_image.#'
path: src/Token/NodeImageBuilder.php
path: modules/helfi_platform_config_base/src/Token/NodeImageBuilder.php
reportUnmatched: false
-
message: '#^Access to an undefined property Drupal\\node\\NodeInterface::\$field_organization.#'
path: src/Token/NodeImageBuilder.php
path: modules/helfi_platform_config_base/src/Token/NodeImageBuilder.php
reportUnmatched: false
-
message: '#^Access to an undefined property Drupal\\Core\\Entity\\EntityInterface::\$field_default_image.#'
path: src/Token/NodeImageBuilder.php
path: modules/helfi_platform_config_base/src/Token/NodeImageBuilder.php
reportUnmatched: false
-
message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface::getFileUri\(\).#'
path: src/Token/NodeImageBuilder.php
path: modules/helfi_platform_config_base/src/Token/NodeImageBuilder.php
reportUnmatched: false
-
message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface::getFileUri\(\).#'
path: src/Token/NodeImageBuilder.php
path: modules/helfi_platform_config_base/src/Token/NodeImageBuilder.php
reportUnmatched: false
-
message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface::hasField\(\).#'
path: src/Token/NodeImageBuilder.php
path: modules/helfi_platform_config_base/src/Token/NodeImageBuilder.php
-
message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface::get\(\).#'
path: src/Token/NodeImageBuilder.php
path: modules/helfi_platform_config_base/src/Token/NodeImageBuilder.php
-
message: '#^Call to an undefined method Drupal\\Core\\TypedData\\TypedDataInterface::getTarget\(\).#'
path: modules/hdbt_admin_tools/hdbt_admin_tools.tokens.inc
Expand Down
1 change: 0 additions & 1 deletion src/Token/DefaultImageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\helfi_api_base\Token\OGImageBuilderInterface;

/**
* Default og image for all entities.
Expand Down
34 changes: 34 additions & 0 deletions src/Token/OGImageBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Drupal\helfi_platform_config\Token;

use Drupal\Core\Entity\EntityInterface;

/**
* Open graph image builder.
*/
interface OGImageBuilderInterface {

/**
* Checks whether this builder is applicable for given entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* An entity to check.
*
* @return bool
* TRUE if this instance should handle the given entity.
*/
public function applies(EntityInterface $entity) : bool;

/**
* Generate image URL.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity to use for generation.
*
* @return string|null
* Image url or NULL on failure.
*/
public function buildUrl(EntityInterface $entity) : ?string;

}
Loading

0 comments on commit b5218e0

Please sign in to comment.