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

allow returning both string and translatablemarkup #889

Merged
merged 7 commits into from
Jan 24, 2025
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
13 changes: 7 additions & 6 deletions modules/helfi_media_map/src/Entity/HelMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Drupal\helfi_media_map\Entity;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\helfi_media\Entity\MediaEntityBundle;
use Drupal\media\MediaInterface;

Expand All @@ -16,10 +15,10 @@ class HelMap extends MediaEntityBundle implements MediaInterface {
/**
* Get service provider url.
*
* @return string|null
* @return string
* Url of the service provider.
*/
public function getServiceUrl(): ?string {
public function getServiceUrl(): string {
$map_url = $this->get('field_media_hel_map')->first()->getString();
$url_parts = parse_url($map_url);
return $url_parts['scheme'] . "://" . $url_parts['host'];
Expand All @@ -28,14 +27,16 @@ public function getServiceUrl(): ?string {
/**
* Get the title of map.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup|null
* @return string|null
* The title of the map.
*/
public function getMediaTitle(): ?TranslatableMarkup {
return $this->get('field_media_hel_map')
public function getMediaTitle(): ?string {
$title = (string) $this->get('field_media_hel_map')
->first()
->get('title')
->getValue();

return empty($title) ? NULL : $title;
}

}
78 changes: 78 additions & 0 deletions modules/helfi_media_map/tests/src/Kernel/Entity/HelpMapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_media_map\Kernel\Entity;

use Drupal\helfi_media_map\Entity\HelMap;
use Drupal\KernelTests\KernelTestBase;

/**
* Tests HelMap entity bundle class.
*
* @group helfi_media_map
*/
class HelpMapTest extends KernelTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'link',
'path',
'field',
'file',
'image',
'user',
'views',
'media',
'datetime',
'media_library',
'helfi_media',
'helfi_media_map',
];

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['system', 'media', 'media_library']);
$this->installEntitySchema('user');
$this->installEntitySchema('media');
$this->installEntitySchema('file');
$this->installSchema('file', ['file_usage']);
$this->installConfig('helfi_media_map');
}

/**
* Tests Hel Map bundle class.
*/
public function testBundleClass() : void {
/** @var \Drupal\media\MediaStorage $storage */
$storage = $this->container->get('entity_type.manager')
->getStorage('media');

$data = [
'uri' => 'https://kartta.hel.fi/embed?&setlanguage=fi&link=eptE2g',
];
$entity = $storage->create([
'name' => 'test',
'bundle' => 'hel_map',
'field_media_hel_map' => $data,
]);
$entity->save();
$this->assertInstanceOf(HelMap::class, $entity);

$this->assertEquals($entity->getServiceUrl(), 'https://kartta.hel.fi');
$this->assertNull($entity->getMediaTitle());

$data['title'] = 'Test title';
$entity->set('field_media_hel_map', $data);
$entity->save();

$this->assertEquals($data['title'], $entity->getMediaTitle());
}

}
Loading