diff --git a/helfi_tpr.install b/helfi_tpr.install index 96318ab..7f35cd5 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('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); +} 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..0652bdb 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('Contact details of daycare centre groups', [], ['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..1768244 --- /dev/null +++ b/src/Field/Connection/Subgroup.php @@ -0,0 +1,79 @@ +getFields(); + $fields_data = []; + + foreach ($fields as $field) { + if (!$this->get($field)) { + continue; + } + + $data = Html::escape($this->get($field)); + + $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' => '
', + ], + }; + } + + $build = [ + 'subgroup' => $fields_data, + ]; + + return $build; + } + +} diff --git a/src/Fixture/Unit.php b/src/Fixture/Unit.php index 8af98f1..83616f2 100644 --- a/src/Fixture/Unit.php +++ b/src/Fixture/Unit.php @@ -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' => 'subgroup@example.com', + ], + [ + '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' => 'subgroup2@example.com', + ], ], 'provided_languages' => [ 'fi', diff --git a/tests/src/Kernel/UnitMigrationTest.php b/tests/src/Kernel/UnitMigrationTest.php index 115a91c..16a6416 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(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("subgroup@example.com", $subgroup->get('email')); } // Re-run migrate and make sure migrate map hash doesn't change. 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. * 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"