-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UHF-9712: Move OGImageManager to helfi_platform_config
- Loading branch information
Showing
14 changed files
with
354 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
7 changes: 7 additions & 0 deletions
7
modules/helfi_platform_config_base/helfi_platform_config_base.services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
Oops, something went wrong.