From 7bdfa226329954eaf82787ef7355c0846b284e73 Mon Sep 17 00:00:00 2001
From: mmyllynen <4696381+dire@users.noreply.github.com>
Date: Mon, 13 Nov 2023 13:09:17 +0200
Subject: [PATCH 1/8] UHF-9159: Added new field subgroup to units.
---
helfi_tpr.install | 19 ++++++++
migrations/tpr_unit.yml | 12 ++++++
src/Entity/Unit.php | 4 ++
src/Field/Connection/Repository.php | 1 +
src/Field/Connection/Subgroup.php | 67 +++++++++++++++++++++++++++++
5 files changed, 103 insertions(+)
create mode 100644 src/Field/Connection/Subgroup.php
diff --git a/helfi_tpr.install b/helfi_tpr.install
index 96318ab..26c68de 100644
--- a/helfi_tpr.install
+++ b/helfi_tpr.install
@@ -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('Subgroup', [], ['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);
+}
diff --git a/migrations/tpr_unit.yml b/migrations/tpr_unit.yml
index 89ea5e1..79a7fc0 100644
--- a/migrations/tpr_unit.yml
+++ b/migrations/tpr_unit.yml
@@ -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
diff --git a/src/Entity/Unit.php b/src/Entity/Unit.php
index 4489230..d7f5255 100644
--- a/src/Entity/Unit.php
+++ b/src/Entity/Unit.php
@@ -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('Subgroup', [], ['context' => 'TPR Unit field label']),
+ ],
];
foreach ($connectionFields as $name => $data) {
diff --git a/src/Field/Connection/Repository.php b/src/Field/Connection/Repository.php
index dd238eb..2ad81f4 100644
--- a/src/Field/Connection/Repository.php
+++ b/src/Field/Connection/Repository.php
@@ -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,
];
/**
diff --git a/src/Field/Connection/Subgroup.php b/src/Field/Connection/Subgroup.php
new file mode 100644
index 0000000..fed229e
--- /dev/null
+++ b/src/Field/Connection/Subgroup.php
@@ -0,0 +1,67 @@
+getFields();
+ $fields_data = [];
+ $name = '';
+
+ foreach ($fields as $field) {
+ if (!$this->get($field)) {
+ continue;
+ }
+
+ $data = Html::escape($this->get($field));
+
+ if ($field === 'name') {
+ $fields_data[] = '' . $data . '';
+ }
+ else {
+ $fields_data[] = $data;
+ }
+ }
+
+ $markup = '
' . implode('
', $fields_data) . '
';
+
+ $build = [
+ 'contact' => [
+ '#markup' => $markup,
+ ],
+ ];
+
+ return $build;
+ }
+
+}
From 3e4a71bdbbeb02cfcacfce5676288d3746f290df Mon Sep 17 00:00:00 2001
From: mmyllynen <4696381+dire@users.noreply.github.com>
Date: Mon, 13 Nov 2023 13:09:32 +0200
Subject: [PATCH 2/8] UHF-9159: Added tests for the new subgroup field.
---
src/Fixture/Unit.php | 18 ++++++++++++
tests/src/Unit/ConnectionRepositoryTest.php | 2 ++
tests/src/Unit/ConnectionTest.php | 31 +++++++++++++++++++++
3 files changed, 51 insertions(+)
diff --git a/src/Fixture/Unit.php b/src/Fixture/Unit.php
index 8af98f1..ec3f8e2 100644
--- a/src/Fixture/Unit.php
+++ b/src/Fixture/Unit.php
@@ -690,6 +690,24 @@ 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',
+ ],
+ [
+ 'unit_id' => 1,
+ 'section_type' => 'SUBGROUP',
+ 'name_fi' => 'subgroup fi 2',
+ 'name_en' => 'subgroup en 2',
+ 'name_sv' => 'subgroup sv 2',
+ 'contact_person' => 'contact person name 2',
+ 'phone' => '0506543210',
+ ],
],
'provided_languages' => [
'fi',
diff --git a/tests/src/Unit/ConnectionRepositoryTest.php b/tests/src/Unit/ConnectionRepositoryTest.php
index 33633aa..ba60c6d 100644
--- a/tests/src/Unit/ConnectionRepositoryTest.php
+++ b/tests/src/Unit/ConnectionRepositoryTest.php
@@ -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;
@@ -74,6 +75,7 @@ public function getTestData() : array {
[Price::TYPE_NAME],
[PhoneOrEmail::TYPE_NAME],
[Topical::TYPE_NAME],
+ [Subgroup::TYPE_NAME],
];
}
diff --git a/tests/src/Unit/ConnectionTest.php b/tests/src/Unit/ConnectionTest.php
index 51a37e1..bb01a7a 100644
--- a/tests/src/Unit/ConnectionTest.php
+++ b/tests/src/Unit/ConnectionTest.php
@@ -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;
@@ -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', 'john.doe@example.com');
+ $this->assertNotEmpty($object->build());
+ }
+
/**
* Tests invalid field name.
*
From 39139a74bd5b791844b3c3087af4f88ef651c6b4 Mon Sep 17 00:00:00 2001
From: mmyllynen <4696381+dire@users.noreply.github.com>
Date: Tue, 14 Nov 2023 10:05:43 +0200
Subject: [PATCH 3/8] UHF-9159: Added correct html tags for email and phone
fields.
---
src/Field/Connection/Subgroup.php | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/src/Field/Connection/Subgroup.php b/src/Field/Connection/Subgroup.php
index fed229e..97c9013 100644
--- a/src/Field/Connection/Subgroup.php
+++ b/src/Field/Connection/Subgroup.php
@@ -48,6 +48,12 @@ public function build(): array {
if ($field === 'name') {
$fields_data[] = '' . $data . '';
}
+ elseif ($field === 'email') {
+ $fields_data[] = '' . $data . '';
+ }
+ elseif ($field === 'phone') {
+ $fields_data[] = '' . $data . '';
+ }
else {
$fields_data[] = $data;
}
From bf17543a7883fdfb93f5fdad970d2d75044dc081 Mon Sep 17 00:00:00 2001
From: mmyllynen <4696381+dire@users.noreply.github.com>
Date: Wed, 15 Nov 2023 12:55:53 +0200
Subject: [PATCH 4/8] UHF-9159: Added subgroup to the unit migration test.
---
src/Fixture/Unit.php | 2 ++
tests/src/Kernel/UnitMigrationTest.php | 8 ++++++++
2 files changed, 10 insertions(+)
diff --git a/src/Fixture/Unit.php b/src/Fixture/Unit.php
index ec3f8e2..f16ac85 100644
--- a/src/Fixture/Unit.php
+++ b/src/Fixture/Unit.php
@@ -698,6 +698,7 @@ public function getMockData() : array {
'name_sv' => 'subgroup sv 1',
'contact_person' => 'subgroup contact person name',
'phone' => '0406543210',
+ 'email' => 'subgroup@example.com',
],
[
'unit_id' => 1,
@@ -707,6 +708,7 @@ public function getMockData() : array {
'name_sv' => 'subgroup sv 2',
'contact_person' => 'contact person name 2',
'phone' => '0506543210',
+ 'email' => 'subgroup2@example.com',
],
],
'provided_languages' => [
diff --git a/tests/src/Kernel/UnitMigrationTest.php b/tests/src/Kernel/UnitMigrationTest.php
index 115a91c..2500573 100644
--- a/tests/src/Kernel/UnitMigrationTest.php
+++ b/tests/src/Kernel/UnitMigrationTest.php
@@ -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;
/**
@@ -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;
@@ -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(1)->data;
+ $this->assertInstanceOf(Subgroup::class, $subgroup);
+ $this->assertEquals("subgroup contact person name", $subgroup->get('contact_person'));
+ $this->assertEquals("0406543210", $subgroup->get('phone'));
+ $this->assertEquals("subgroup@example.com", $subgroup->get('email'));
}
// Re-run migrate and make sure migrate map hash doesn't change.
From 37e2c11762819db86640c2e9ada2556b85597105 Mon Sep 17 00:00:00 2001
From: mmyllynen <4696381+dire@users.noreply.github.com>
Date: Wed, 15 Nov 2023 13:05:09 +0200
Subject: [PATCH 5/8] UHF-9159: Fixed the test.
---
src/Fixture/Unit.php | 2 +-
tests/src/Kernel/UnitMigrationTest.php | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/Fixture/Unit.php b/src/Fixture/Unit.php
index f16ac85..83616f2 100644
--- a/src/Fixture/Unit.php
+++ b/src/Fixture/Unit.php
@@ -706,7 +706,7 @@ public function getMockData() : array {
'name_fi' => 'subgroup fi 2',
'name_en' => 'subgroup en 2',
'name_sv' => 'subgroup sv 2',
- 'contact_person' => 'contact person name 2',
+ 'contact_person' => 'subgroup contact person name 2',
'phone' => '0506543210',
'email' => 'subgroup2@example.com',
],
diff --git a/tests/src/Kernel/UnitMigrationTest.php b/tests/src/Kernel/UnitMigrationTest.php
index 2500573..16a6416 100644
--- a/tests/src/Kernel/UnitMigrationTest.php
+++ b/tests/src/Kernel/UnitMigrationTest.php
@@ -105,7 +105,7 @@ public function testUnitMigration() : void {
$this->assertInstanceOf(Topical::class, $topical);
$this->assertEquals("https://localhost/$langcode", $links->get('www'));
- $subgroup = $translation->get('subgroup')->get(1)->data;
+ $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'));
From dd829c519533237659517f4f179d3991410338f8 Mon Sep 17 00:00:00 2001
From: mmyllynen <4696381+dire@users.noreply.github.com>
Date: Fri, 17 Nov 2023 12:22:30 +0200
Subject: [PATCH 6/8] UHF-9159: Added translations for the subgroup field.
---
helfi_tpr.install | 2 +-
src/Entity/Unit.php | 2 +-
translations/fi.po | 4 ++++
translations/sv.po | 4 ++++
4 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/helfi_tpr.install b/helfi_tpr.install
index 26c68de..7f35cd5 100644
--- a/helfi_tpr.install
+++ b/helfi_tpr.install
@@ -306,7 +306,7 @@ function helfi_tpr_update_8045() : void {
*/
function helfi_tpr_update_8046() : void {
$subgroup_field = BaseFieldDefinition::create('tpr_connection')
- ->setLabel(new TranslatableMarkup('Subgroup', [], ['context' => 'TPR Unit field label']))
+ ->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)
diff --git a/src/Entity/Unit.php b/src/Entity/Unit.php
index d7f5255..0652bdb 100644
--- a/src/Entity/Unit.php
+++ b/src/Entity/Unit.php
@@ -358,7 +358,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) :
],
'subgroup' => [
'description' => 'SUBGROUP',
- 'label' => new TranslatableMarkup('Subgroup', [], ['context' => 'TPR Unit field label']),
+ 'label' => new TranslatableMarkup('Contact details of daycare centre groups', [], ['context' => 'TPR Unit field label']),
],
];
diff --git a/translations/fi.po b/translations/fi.po
index 0364897..007c198 100644
--- a/translations/fi.po
+++ b/translations/fi.po
@@ -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"
diff --git a/translations/sv.po b/translations/sv.po
index fb9162f..7b1e270 100644
--- a/translations/sv.po
+++ b/translations/sv.po
@@ -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"
From 6def3ff9988e3ee9835cda34de94ca87ff708290 Mon Sep 17 00:00:00 2001
From: mmyllynen <4696381+dire@users.noreply.github.com>
Date: Tue, 21 Nov 2023 11:52:57 +0200
Subject: [PATCH 7/8] UHF-9159: Use link template for the links.
---
src/Field/Connection/Subgroup.php | 41 ++++++++++++++++++-------------
1 file changed, 24 insertions(+), 17 deletions(-)
diff --git a/src/Field/Connection/Subgroup.php b/src/Field/Connection/Subgroup.php
index 97c9013..be66258 100644
--- a/src/Field/Connection/Subgroup.php
+++ b/src/Field/Connection/Subgroup.php
@@ -4,7 +4,9 @@
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.
@@ -45,26 +47,31 @@ public function build(): array {
$data = Html::escape($this->get($field));
- if ($field === 'name') {
- $fields_data[] = '' . $data . '';
- }
- elseif ($field === 'email') {
- $fields_data[] = '' . $data . '';
- }
- elseif ($field === 'phone') {
- $fields_data[] = '' . $data . '';
- }
- else {
- $fields_data[] = $data;
- }
+ $fields_data[] = match ($field) {
+ 'name' => [
+ '#markup' => '' . $data . '',
+ ],
+ 'contact_person' => [
+ '#markup' => '' . $data . '',
+ '#prefix' => '
',
+ ],
+ 'email' => [
+ '#url' => Url::fromUri('mailto:' . $data),
+ '#title' => new FormattableMarkup($data, []),
+ '#type' => 'link',
+ '#prefix' => '
',
+ ],
+ 'phone' => [
+ '#url' => Url::fromUri('tel:' . $data),
+ '#title' => new FormattableMarkup($data, []),
+ '#type' => 'link',
+ '#prefix' => '
',
+ ],
+ };
}
- $markup = '' . implode('
', $fields_data) . '
';
-
$build = [
- 'contact' => [
- '#markup' => $markup,
- ],
+ 'subgroup' => $fields_data,
];
return $build;
From 84d0c426d859c7c5e26bb4c6fb2e2d5ac7a5d006 Mon Sep 17 00:00:00 2001
From: mmyllynen <4696381+dire@users.noreply.github.com>
Date: Tue, 21 Nov 2023 12:09:48 +0200
Subject: [PATCH 8/8] UHF-9159: Removed unused variable.
---
src/Field/Connection/Subgroup.php | 1 -
1 file changed, 1 deletion(-)
diff --git a/src/Field/Connection/Subgroup.php b/src/Field/Connection/Subgroup.php
index be66258..1768244 100644
--- a/src/Field/Connection/Subgroup.php
+++ b/src/Field/Connection/Subgroup.php
@@ -38,7 +38,6 @@ public function getFields(): array {
public function build(): array {
$fields = $this->getFields();
$fields_data = [];
- $name = '';
foreach ($fields as $field) {
if (!$this->get($field)) {