Skip to content

Commit

Permalink
Merge pull request #161 from City-of-Helsinki/UHF-9159_subgroup
Browse files Browse the repository at this point in the history
UHF-9159 subgroup
  • Loading branch information
dire authored Nov 21, 2023
2 parents afed5db + 84d0c42 commit 1c35432
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 0 deletions.
19 changes: 19 additions & 0 deletions helfi_tpr.install
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,22 @@ function helfi_tpr_update_8045() : void {
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('topical', 'tpr_unit', 'helfi_tpr', $topical_field);
}

/**
* UHF-9159 Add SUBGROUP field to Unit entity.
*/
function helfi_tpr_update_8046() : void {
$subgroup_field = BaseFieldDefinition::create('tpr_connection')
->setLabel(new TranslatableMarkup('Contact details of daycare centre groups', [], ['context' => 'TPR Unit field label']))
->setDescription(new TranslatableMarkup('The "SUBGROUP" connection type'))
->setTranslatable(TRUE)
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayOptions('form', [
'type' => 'readonly_field_widget',
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);

\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('subgroup', 'tpr_unit', 'helfi_tpr', $subgroup_field);
}
12 changes: 12 additions & 0 deletions migrations/tpr_unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ process:
value: value
data: data
type: type
_subgroup_connections:
plugin: array_element_equals
source: connections
value: SUBGROUP
key: type
subgroup:
plugin: sub_process
source: '@_subgroup_connections'
process:
value: value
data: data
type: type
_contacts_connections:
plugin: array_element_equals
source: connections
Expand Down
4 changes: 4 additions & 0 deletions src/Entity/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) :
'description' => 'TOPICAL',
'label' => new TranslatableMarkup('Topical', [], ['context' => 'TPR Unit field label']),
],
'subgroup' => [
'description' => 'SUBGROUP',
'label' => new TranslatableMarkup('Contact details of daycare centre groups', [], ['context' => 'TPR Unit field label']),
],
];

foreach ($connectionFields as $name => $data) {
Expand Down
1 change: 1 addition & 0 deletions src/Field/Connection/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class Repository {
Link::TYPE_NAME => Link::class,
PhoneOrEmail::TYPE_NAME => PhoneOrEmail::class,
Topical::TYPE_NAME => Topical::class,
Subgroup::TYPE_NAME => Subgroup::class,
];

/**
Expand Down
79 changes: 79 additions & 0 deletions src/Field/Connection/Subgroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types = 1);

namespace Drupal\helfi_tpr\Field\Connection;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;

/**
* Provides a domain object for TPR connection type of Subgroup.
*/
final class Subgroup extends Connection {

/**
* The type name.
*
* @var string
*/
public const TYPE_NAME = 'SUBGROUP';

/**
* {@inheritdoc}
*/
public function getFields(): array {
return [
'name',
'contact_person',
'phone',
'email',
];
}

/**
* {@inheritdoc}
*/
public function build(): array {
$fields = $this->getFields();
$fields_data = [];

foreach ($fields as $field) {
if (!$this->get($field)) {
continue;
}

$data = Html::escape($this->get($field));

$fields_data[] = match ($field) {
'name' => [
'#markup' => '<strong>' . $data . '</strong>',
],
'contact_person' => [
'#markup' => '<span>' . $data . '</span>',
'#prefix' => '<br />',
],
'email' => [
'#url' => Url::fromUri('mailto:' . $data),
'#title' => new FormattableMarkup($data, []),
'#type' => 'link',
'#prefix' => '<br />',
],
'phone' => [
'#url' => Url::fromUri('tel:' . $data),
'#title' => new FormattableMarkup($data, []),
'#type' => 'link',
'#prefix' => '<br />',
],
};
}

$build = [
'subgroup' => $fields_data,
];

return $build;
}

}
20 changes: 20 additions & 0 deletions src/Fixture/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,26 @@ public function getMockData() : array {
'www_en' => 'https://localhost/en',
'www_sv' => 'https://localhost/sv',
],
[
'unit_id' => 1,
'section_type' => 'SUBGROUP',
'name_fi' => 'subgroup fi 1',
'name_en' => 'subgroup en 1',
'name_sv' => 'subgroup sv 1',
'contact_person' => 'subgroup contact person name',
'phone' => '0406543210',
'email' => '[email protected]',
],
[
'unit_id' => 1,
'section_type' => 'SUBGROUP',
'name_fi' => 'subgroup fi 2',
'name_en' => 'subgroup en 2',
'name_sv' => 'subgroup sv 2',
'contact_person' => 'subgroup contact person name 2',
'phone' => '0506543210',
'email' => '[email protected]',
],
],
'provided_languages' => [
'fi',
Expand Down
8 changes: 8 additions & 0 deletions tests/src/Kernel/UnitMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Drupal\helfi_tpr\Field\Connection\OtherInfo;
use Drupal\helfi_tpr\Field\Connection\PhoneOrEmail;
use Drupal\helfi_tpr\Field\Connection\Price;
use Drupal\helfi_tpr\Field\Connection\Subgroup;
use Drupal\helfi_tpr\Field\Connection\Topical;

/**
Expand Down Expand Up @@ -72,6 +73,7 @@ public function testUnitMigration() : void {
$this->assertEquals("price $langcode $delta", $translation->get('price_info')->get($i)->value);
$this->assertEquals("other info $langcode $delta", $translation->get('other_info')->get($i)->value);
$this->assertEquals("topical $langcode $delta", $translation->get('topical')->get($i)->value);
$this->assertEquals("subgroup $langcode $delta", $translation->get('subgroup')->get($i)->value);
}

$opening_hour = $translation->get('opening_hours')->get(1)->data;
Expand Down Expand Up @@ -102,6 +104,12 @@ public function testUnitMigration() : void {
$topical = $translation->get('topical')->get(0)->data;
$this->assertInstanceOf(Topical::class, $topical);
$this->assertEquals("https://localhost/$langcode", $links->get('www'));

$subgroup = $translation->get('subgroup')->get(0)->data;
$this->assertInstanceOf(Subgroup::class, $subgroup);
$this->assertEquals("subgroup contact person name", $subgroup->get('contact_person'));
$this->assertEquals("0406543210", $subgroup->get('phone'));
$this->assertEquals("[email protected]", $subgroup->get('email'));
}

// Re-run migrate and make sure migrate map hash doesn't change.
Expand Down
2 changes: 2 additions & 0 deletions tests/src/Unit/ConnectionRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Drupal\helfi_tpr\Field\Connection\PhoneOrEmail;
use Drupal\helfi_tpr\Field\Connection\Price;
use Drupal\helfi_tpr\Field\Connection\Repository;
use Drupal\helfi_tpr\Field\Connection\Subgroup;
use Drupal\helfi_tpr\Field\Connection\Topical;
use Drupal\Tests\UnitTestCase;

Expand Down Expand Up @@ -74,6 +75,7 @@ public function getTestData() : array {
[Price::TYPE_NAME],
[PhoneOrEmail::TYPE_NAME],
[Topical::TYPE_NAME],
[Subgroup::TYPE_NAME],
];
}

Expand Down
31 changes: 31 additions & 0 deletions tests/src/Unit/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Drupal\helfi_tpr\Field\Connection\OtherInfo;
use Drupal\helfi_tpr\Field\Connection\PhoneOrEmail;
use Drupal\helfi_tpr\Field\Connection\Price;
use Drupal\helfi_tpr\Field\Connection\Subgroup;
use Drupal\helfi_tpr\Field\Connection\TextWithLink;
use Drupal\helfi_tpr\Field\Connection\Topical;

Expand Down Expand Up @@ -198,6 +199,36 @@ public function testTopical() : void {
$this->assertNotEmpty($object->build());
}

/**
* Tests subgroups.
*
* @covers \Drupal\helfi_tpr\Field\Connection\Subgroup::build
* @covers \Drupal\helfi_tpr\Field\Connection\Subgroup::getFields
* @covers ::set
* @covers ::get
* @covers ::isValidField
*/
public function testSubgroup() : void {
$object = new Subgroup();
$object->set('name', 'Some information.');
$this->assertNotEmpty($object->build());

// Make sure we can override data.
$object->set('name', 'override');
$this->assertNotEmpty($object->build());

$this->assertEquals(['name', 'contact_person', 'phone', 'email'], $object->getFields());

$this->assertNull($object->get('www'));

$object->set('contact_person', 'John Doe');
$this->assertNotEmpty($object->build());
$object->set('phone', '040123456');
$this->assertNotEmpty($object->build());
$object->set('email', '[email protected]');
$this->assertNotEmpty($object->build());
}

/**
* Tests invalid field name.
*
Expand Down
4 changes: 4 additions & 0 deletions translations/fi.po
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,7 @@ msgstr "Ajankohtaista"
msgctxt "TPR Unit field label"
msgid "Highlights"
msgstr "Nostot"

msgctxt "TPR Unit field label"
msgid "Contact details of daycare centre groups"
msgstr "Päiväkotiryhmien yhteystiedot"
4 changes: 4 additions & 0 deletions translations/sv.po
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,7 @@ msgstr "Aktuell"
msgctxt "TPR Unit field label"
msgid "Highlights"
msgstr "Höjdpunkt"

msgctxt "TPR Unit field label"
msgid "Contact details of daycare centre groups"
msgstr "Barngruppernas kontaktuppgifter"

0 comments on commit 1c35432

Please sign in to comment.