Skip to content

Commit

Permalink
Merge pull request #814 from City-of-Helsinki/UHF-8670
Browse files Browse the repository at this point in the history
UHF-8670 Hero image author
  • Loading branch information
khalima authored Sep 24, 2024
2 parents 3a3d6c2 + 45163b6 commit ad39a88
Show file tree
Hide file tree
Showing 12 changed files with 435 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @FieldType(
* id = "select2_icon",
* label = @Translation("Select Icon"),
* category = @Translation("Helfi"),
* category = "Helfi",
* default_widget = "select_icon_widget",
* default_formatter = "select_icon_formatter"
* )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,16 @@ function _helfi_media_remote_video_remote_video_url_handler(string $video_url):
// Try to convert the URL if it's of form 'player/event/view'.
if (str_contains($video_url, 'player/event/view')) {
preg_match('/helsinkikanava.fi\/((?i)[a-z]{2})/', $video_url, $language_matches);
$lang_code = $language_matches[1];
// Default to 'fi' if no match.
$lang_code = $language_matches[1] ?? 'fi';
preg_match('/assetId=(\d+)/', $video_url, $asset_id_matches);
$asset_id = $asset_id_matches[1];
$asset_id = $asset_id_matches[1] ?? NULL;

if (empty($asset_id)) {
return FALSE;
}

$lang_code = empty($lang_code) ? 'fi' : $lang_code;
// Assemble the converted URL.
$url_parts = explode('*', $helsinki_kanava_url_pattern);
$converted_url = $url_parts[0] . $lang_code . $url_parts[1] . $asset_id;
}
Expand Down
9 changes: 9 additions & 0 deletions modules/helfi_paragraphs_hero/helfi_paragraphs_hero.module
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,12 @@ function helfi_paragraphs_hero_design_allowed_values(FieldStorageDefinitionInter
$moduleHandler->alter('helfi_hero_design', $designs, $definition, $entity);
return $designs;
}

/**
* Implements hook_preprocess().
*/
function helfi_paragraphs_hero_preprocess_paragraph__hero(array &$variables): void {
$paragraph = $variables['paragraph'];
assert($paragraph instanceof Hero);
$variables['image_author'] = $paragraph->getImageAuthor();
}
80 changes: 80 additions & 0 deletions modules/helfi_paragraphs_hero/src/Entity/Hero.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
namespace Drupal\helfi_paragraphs_hero\Entity;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\Exception\MissingDataException;
use Drupal\media\Entity\Media;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphInterface;

Expand All @@ -13,6 +18,8 @@
*/
class Hero extends Paragraph implements ParagraphInterface {

use StringTranslationTrait;

/**
* Get paragraph design.
*
Expand All @@ -23,6 +30,79 @@ public function getDesign(): string {
return $this->get('field_hero_design')->value;
}

/**
* Get the Hero image.
*
* @return \Drupal\media\Entity\Media|false
* Returns the media entity or false if not found.
*/
public function getImage(): Media|false {
if ($this->get('field_hero_image')->isEmpty()) {
return FALSE;
}

try {
$media = FALSE;
/** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItemInterface $hero_image */
$hero_image = $this->get('field_hero_image')->first();
if ($hero_image instanceof EntityReferenceItem) {
/** @var \Drupal\Core\Entity\Plugin\DataType\EntityReference $media */
$entity_reference = $hero_image->get('entity');
/** @var \Drupal\media\Entity\Media $media */
$media = $entity_reference->getValue();
}
return $media;
}
catch (MissingDataException $e) {
return FALSE;
}
}

/**
* Get the image author if any.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup|false
* The image author as a translatable markup or false.
*/
public function getImageAuthor(): TranslatableMarkup|FALSE {
$image = $this->getImage();
if (!$image || $image->get('field_photographer')->isEmpty()) {
return FALSE;
}

try {
$image_author = $image->get('field_photographer')->first()->getString();
return $this->t(
'Image: @image_author',
['@image_author' => $image_author],
['context' => 'Helfi Paragraphs Hero']
);
}
catch (MissingDataException $e) {
return FALSE;
}
}

/**
* Gets the paragraph title.
*
* @return string
* The title.
*/
public function getTitle() : string {
return $this->get('field_hero_title')->value;
}

/**
* Gets the paragraph description.
*
* @return string
* The description.
*/
public function getDescription() : string {
return $this->get('field_hero_desc')->value;
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: 'Helfi Paragraphs Hero Test'
core_version_requirement: '^10 || ^11'
type: module
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* @file
* Contains test alter for helfi_paragraphs_hero.
*/

declare(strict_types=1);

/**
* Implements hook_helfi_hero_design_alter().
*/
function helfi_paragraphs_hero_test_helfi_hero_design_alter(array &$designs) {
$designs['test-design'] = t('Test design');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Drupal\Tests\helfi_paragraphs_hero\Kernel;

use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\KernelTests\KernelTestBase;

/**
* Tests allowed values alter for hero design.
*
* @group helfi_paragraphs_hero
*/
class HeroDesignAllowedValuesAlterTest extends KernelTestBase {

use StringTranslationTrait;

/**
* {@inheritdoc}
*/
protected static $modules = [
'helfi_paragraphs_hero',
'helfi_paragraphs_hero_test',
'field',
'entity',
'user',
];

/**
* Test the hero design allowed values alter.
*/
public function testHeroDesignAlter() {
// Load the required definition and entity mocks.
$definition = $this->createMock(FieldStorageDefinitionInterface::class);
$entity = $this->createMock(FieldableEntityInterface::class);

// Call the allowed designs function.
$allowed_designs = helfi_paragraphs_hero_design_allowed_values($definition, $entity);

// Call the function that triggers the drupal_alter.
helfi_paragraphs_hero_test_helfi_hero_design_alter($allowed_designs);

// Define the expected outcome after the alter hook.
$expected_designs = $allowed_designs + ['test-design' => $this->t('Test design')];

// Assert that the design data has been altered as expected.
$this->assertEquals($expected_designs, $allowed_designs);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Drupal\Tests\helfi_paragraphs_hero\Kernel;

use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\KernelTests\KernelTestBase;

/**
* Tests allowed values for hero design.
*
* @group helfi_paragraphs_hero
*/
class HeroDesignAllowedValuesTest extends KernelTestBase {

use StringTranslationTrait;

/**
* {@inheritdoc}
*/
protected static $modules = [
'helfi_paragraphs_hero',
'field',
'entity',
'user',
];

/**
* Test the hero design allowed values function.
*/
public function testAllowedValues() {
// Load the required definition and entity mocks.
$definition = $this->createMock(FieldStorageDefinitionInterface::class);
$entity = $this->createMock(FieldableEntityInterface::class);

// Call the allowed designs function.
$allowed_designs = helfi_paragraphs_hero_design_allowed_values($definition, $entity);

// Define the expected allowed designs.
$expected_designs = [
'without-image-left' => $this->t('Without image, align left'),
'with-image-right' => $this->t('Image on the right'),
'with-image-left' => $this->t('Image on the left'),
'with-image-bottom' => $this->t('Image on the bottom'),
'diagonal' => $this->t('Diagonal'),
];

// Assert that the allowed values match the expected values.
$this->assertEquals($expected_designs, $allowed_designs);
}

}
Loading

0 comments on commit ad39a88

Please sign in to comment.