diff --git a/helfi_tpr.install b/helfi_tpr.install index 88b026c..6e01253 100644 --- a/helfi_tpr.install +++ b/helfi_tpr.install @@ -227,3 +227,57 @@ function helfi_tpr_update_8043() : void { ->installFieldStorageDefinition($name, 'tpr_service', 'helfi_tpr', $field); } } + +/** + * Add multiple missing fields to Unit entity. + */ +function helfi_tpr_update_8044() : void { + $fields['other_info'] = BaseFieldDefinition::create('tpr_connection') + ->setLabel(new TranslatableMarkup('Further information')) + ->setDescription(new TranslatableMarkup('The "OTHER_INFO" connection type')) + ->setTranslatable(TRUE) + ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED) + ->setDisplayOptions('form', [ + 'type' => 'readonly_field_widget', + ]) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE); + + $fields['price_info'] = BaseFieldDefinition::create('tpr_connection') + ->setLabel(new TranslatableMarkup('Charges')) + ->setDescription(new TranslatableMarkup('The "PRICE" connection type')) + ->setTranslatable(TRUE) + ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED) + ->setDisplayOptions('form', [ + 'type' => 'readonly_field_widget', + ]) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE); + + $fields['links'] = BaseFieldDefinition::create('tpr_connection') + ->setLabel(new TranslatableMarkup('Web sites')) + ->setDescription(new TranslatableMarkup('The "LINK" connection type')) + ->setTranslatable(TRUE) + ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED) + ->setDisplayOptions('form', [ + 'type' => 'readonly_field_widget', + ]) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE); + + $fields['contacts'] = BaseFieldDefinition::create('tpr_connection') + ->setLabel(new TranslatableMarkup('Other contact information')) + ->setDescription(new TranslatableMarkup('The "PHONE_OR_EMAIL" connection type')) + ->setTranslatable(TRUE) + ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED) + ->setDisplayOptions('form', [ + 'type' => 'readonly_field_widget', + ]) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE); + + foreach ($fields as $name => $field) { + \Drupal::entityDefinitionUpdateManager() + ->installFieldStorageDefinition($name, 'tpr_unit', 'helfi_tpr', $field); + } +} diff --git a/migrations/tpr_unit.yml b/migrations/tpr_unit.yml index 753d1fb..d996aba 100644 --- a/migrations/tpr_unit.yml +++ b/migrations/tpr_unit.yml @@ -56,6 +56,54 @@ process: value: value data: data type: type + _other_info_connections: + plugin: array_element_equals + source: connections + value: OTHER_INFO + key: type + other_info: + plugin: sub_process + source: '@_other_info_connections' + process: + value: value + data: data + type: type + _price_info_connections: + plugin: array_element_equals + source: connections + value: PRICE + key: type + price_info: + plugin: sub_process + source: '@_price_info_connections' + process: + value: value + data: data + type: type + _links_connections: + plugin: array_element_equals + source: connections + value: LINK + key: type + links: + plugin: sub_process + source: '@_links_connections' + process: + value: value + data: data + type: type + _contacts_connections: + plugin: array_element_equals + source: connections + value: PHONE_OR_EMAIL + key: type + contacts: + plugin: sub_process + source: '@_contacts_connections' + process: + value: value + data: data + type: type accessibility_sentences: plugin: sub_process source: accessibility_sentences diff --git a/src/Entity/Listing/ListBuilder.php b/src/Entity/Listing/ListBuilder.php index 53c4e84..541f7ec 100644 --- a/src/Entity/Listing/ListBuilder.php +++ b/src/Entity/Listing/ListBuilder.php @@ -4,9 +4,9 @@ namespace Drupal\helfi_tpr\Entity\Listing; +use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityListBuilder; -use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Routing\RedirectDestinationInterface; diff --git a/src/Entity/Unit.php b/src/Entity/Unit.php index f93f9b2..0f2332d 100644 --- a/src/Entity/Unit.php +++ b/src/Entity/Unit.php @@ -301,6 +301,10 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) : ->setDescription(new TranslatableMarkup('The "OPENING_HOURS" connection type')) ->setTranslatable(TRUE) ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) + ->setDisplayOptions('form', [ + 'type' => 'readonly_field_widget', + ]) + ->setDisplayConfigurable('form', TRUE) ->setDisplayConfigurable('view', TRUE); $fields['provided_languages'] = BaseFieldDefinition::create('string') ->setLabel(new TranslatableMarkup('Provided languages')) @@ -323,6 +327,10 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) : ->setLabel(new TranslatableMarkup('Highlights')) ->setTranslatable(TRUE) ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) + ->setDisplayOptions('form', [ + 'type' => 'readonly_field_widget', + ]) + ->setDisplayConfigurable('form', TRUE) ->setDisplayConfigurable('view', TRUE); $fields['ontologyword_ids'] = BaseFieldDefinition::create('integer') ->setLabel(new TranslatableMarkup('Ontologyword IDs')) @@ -336,6 +344,46 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) : ->setDisplayOptions('form', [ 'region' => 'hidden', ]); + $fields['other_info'] = BaseFieldDefinition::create('tpr_connection') + ->setLabel(new TranslatableMarkup('Further information')) + ->setDescription(new TranslatableMarkup('The "OTHER_INFO" connection type')) + ->setTranslatable(TRUE) + ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE) + ->setDisplayOptions('form', [ + 'type' => 'readonly_field_widget', + ]); + $fields['price_info'] = BaseFieldDefinition::create('tpr_connection') + ->setLabel(new TranslatableMarkup('Charges')) + ->setDescription(new TranslatableMarkup('The "PRICE" connection type')) + ->setTranslatable(TRUE) + ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE) + ->setDisplayOptions('form', [ + 'type' => 'readonly_field_widget', + ]); + $fields['links'] = BaseFieldDefinition::create('tpr_connection') + ->setLabel(new TranslatableMarkup('Web sites')) + ->setDescription(new TranslatableMarkup('The "LINK" connection type')) + ->setTranslatable(TRUE) + ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED) + ->setDisplayOptions('form', [ + 'type' => 'readonly_field_widget', + ]) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE); + $fields['contacts'] = BaseFieldDefinition::create('tpr_connection') + ->setLabel(new TranslatableMarkup('Other contact information')) + ->setDescription(new TranslatableMarkup('The "PHONE_OR_EMAIL" connection type')) + ->setTranslatable(TRUE) + ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED) + ->setDisplayOptions('form', [ + 'type' => 'readonly_field_widget', + ]) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE); return $fields; } diff --git a/src/Field/Connection/Link.php b/src/Field/Connection/Link.php new file mode 100644 index 0000000..a0384e0 --- /dev/null +++ b/src/Field/Connection/Link.php @@ -0,0 +1,19 @@ +get('name')); + + if (function_exists('_filter_autop')) { + $markup = _filter_autop($markup); + } + + return [ + 'name' => [ + '#markup' => $markup, + ], + ]; + } + +} diff --git a/src/Field/Connection/PhoneOrEmail.php b/src/Field/Connection/PhoneOrEmail.php new file mode 100644 index 0000000..3fd2d1a --- /dev/null +++ b/src/Field/Connection/PhoneOrEmail.php @@ -0,0 +1,59 @@ +getFields(); + $fields_data = []; + + foreach ($fields as $field) { + if (!$this->get($field)) { + continue; + } + + $data = Html::escape($this->get($field)); + + $fields_data[] = $data; + } + + $build = [ + 'contact' => [ + '#markup' => '

' . implode('
', $fields_data) . '

', + ], + ]; + + return $build; + } + +} diff --git a/src/Field/Connection/Price.php b/src/Field/Connection/Price.php new file mode 100644 index 0000000..1652c0a --- /dev/null +++ b/src/Field/Connection/Price.php @@ -0,0 +1,47 @@ +get('name')); + + if (function_exists('_filter_autop')) { + $markup = _filter_autop($markup); + } + + return [ + 'name' => [ + '#markup' => $markup, + ], + ]; + } + +} diff --git a/src/Field/Connection/Repository.php b/src/Field/Connection/Repository.php index beffcca..13a6d45 100644 --- a/src/Field/Connection/Repository.php +++ b/src/Field/Connection/Repository.php @@ -18,6 +18,10 @@ final class Repository { OpeningHour::TYPE_NAME => OpeningHour::class, OpeningHourObject::TYPE_NAME => OpeningHourObject::class, Highlight::TYPE_NAME => Highlight::class, + OtherInfo::TYPE_NAME => OtherInfo::class, + Price::TYPE_NAME => Price::class, + Link::TYPE_NAME => Link::class, + PhoneOrEmail::TYPE_NAME => PhoneOrEmail::class, ]; /** diff --git a/src/Field/Connection/OpeningHourBase.php b/src/Field/Connection/TextWithLink.php similarity index 89% rename from src/Field/Connection/OpeningHourBase.php rename to src/Field/Connection/TextWithLink.php index d0fc59e..5740f80 100644 --- a/src/Field/Connection/OpeningHourBase.php +++ b/src/Field/Connection/TextWithLink.php @@ -8,9 +8,9 @@ use Drupal\Core\Url; /** - * A base class for 'opening hour' connections. + * A base class for connections with text and link. */ -abstract class OpeningHourBase extends Connection { +abstract class TextWithLink extends Connection { /** * {@inheritdoc} diff --git a/src/Plugin/Field/FieldFormatter/ConnectionFormatter.php b/src/Plugin/Field/FieldFormatter/ConnectionFormatter.php index 3ce49bc..0cb845b 100644 --- a/src/Plugin/Field/FieldFormatter/ConnectionFormatter.php +++ b/src/Plugin/Field/FieldFormatter/ConnectionFormatter.php @@ -32,6 +32,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) : array { $element[$delta] = [ '#type' => 'item', 'content' => $item->data->build(), + '#parents' => [$delta], ]; } diff --git a/tests/src/Unit/ConnectionTest.php b/tests/src/Unit/ConnectionTest.php index 3a0750a..1fd6035 100644 --- a/tests/src/Unit/ConnectionTest.php +++ b/tests/src/Unit/ConnectionTest.php @@ -6,8 +6,8 @@ use Drupal\helfi_tpr\Field\Connection\Highlight; use Drupal\helfi_tpr\Field\Connection\OpeningHour; -use Drupal\helfi_tpr\Field\Connection\OpeningHourBase; use Drupal\helfi_tpr\Field\Connection\OpeningHourObject; +use Drupal\helfi_tpr\Field\Connection\TextWithLink; use Drupal\Tests\UnitTestCase; /** @@ -21,14 +21,14 @@ class ConnectionTest extends UnitTestCase { /** * Tests opening hours. * - * @covers \Drupal\helfi_tpr\Field\Connection\OpeningHourBase::build - * @covers \Drupal\helfi_tpr\Field\Connection\OpeningHourBase::getFields + * @covers \Drupal\helfi_tpr\Field\Connection\TextWithLink::build + * @covers \Drupal\helfi_tpr\Field\Connection\TextWithLink::getFields * @covers ::set * @covers ::get * @covers ::isValidField * @dataProvider openingHourData */ - public function testOpeningHours(OpeningHourBase $object) : void { + public function testOpeningHours(TextWithLink $object) : void { $object->set('name', 'mon-wed 10-19'); $this->assertNotEmpty($object->build()); @@ -71,10 +71,10 @@ public function testHighlights() : void { * * @covers ::set * @covers ::isValidField - * @covers \Drupal\helfi_tpr\Field\Connection\OpeningHourBase::getFields + * @covers \Drupal\helfi_tpr\Field\Connection\TextWithLink::getFields * @dataProvider openingHourData */ - public function testInvalidFieldName(OpeningHourBase $object) : void { + public function testInvalidFieldName(TextWithLink $object) : void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Field "invalid_field" is not valid.'); $object->set('invalid_field', 'value'); @@ -97,7 +97,7 @@ public function openingHourData() : array { * Tests invalid data type. * * @dataProvider invalidFieldValueData - * @covers \Drupal\helfi_tpr\Field\Connection\OpeningHourBase::getFields + * @covers \Drupal\helfi_tpr\Field\Connection\TextWithLink::getFields * @covers ::set * @covers ::isValidField */ @@ -125,7 +125,7 @@ public function invalidFieldValueData() : array { * Tests valid values. * * @dataProvider validFieldValueData - * @covers \Drupal\helfi_tpr\Field\Connection\OpeningHourBase::getFields + * @covers \Drupal\helfi_tpr\Field\Connection\TextWithLink::getFields * @covers ::set * @covers ::get * @covers ::isValidField diff --git a/translations/fi.po b/translations/fi.po index 657d4b6..b468aae 100644 --- a/translations/fi.po +++ b/translations/fi.po @@ -55,3 +55,15 @@ msgstr "Ylikirjoita arvo kentälle: @field_name" msgid "Override: Picture" msgstr "Oletuskuvan korvaava kuva" + +msgid "Further information" +msgstr "Lisätietoja" + +msgid "Charges" +msgstr "Hinnat" + +msgid "Web sites" +msgstr "Muualla verkossa" + +msgid "Other contact information" +msgstr "Muut yhteystiedot" diff --git a/translations/sv.po b/translations/sv.po index c8d38db..da8f6d6 100644 --- a/translations/sv.po +++ b/translations/sv.po @@ -40,3 +40,15 @@ msgstr "Överskrida: @field_name" msgid "Override: Picture" msgstr "Bild som ersätter standardbilden" + +msgid "Further information" +msgstr "Ytterligare uppgifter" + +msgid "Charges" +msgstr "Priser" + +msgid "Web sites" +msgstr "På webben" + +msgid "Other contact information" +msgstr "Andra kontaktuppgifter"