From e8adf1bea2811e52ee504fad4e6bf95cdd26292b Mon Sep 17 00:00:00 2001 From: Evgenii Nikitin Date: Fri, 14 Oct 2022 16:12:51 +0200 Subject: [PATCH 01/12] EWPP-2701: Add new optional fields to corporate forms. --- config/schema/oe_contact_forms.schema.yml | 6 ++ js/related_checkboxes.js | 29 ++++++ oe_contact_forms.install | 1 + oe_contact_forms.libraries.yml | 6 ++ oe_contact_forms.module | 89 ++++++++++++++++++- oe_contact_forms.post_update.php | 64 +++++++++++++ src/Form/ContactMessageForm.php | 16 ++-- src/Plugin/ConceptSubset/ContactLanguages.php | 58 ++++++++++++ .../CorporateContactFormTest.php | 29 +++++- .../FunctionalJavascript/MessageFormTest.php | 23 +++++ tests/src/Kernel/BaseFieldTest.php | 6 ++ 11 files changed, 315 insertions(+), 12 deletions(-) create mode 100644 js/related_checkboxes.js create mode 100644 oe_contact_forms.libraries.yml create mode 100644 src/Plugin/ConceptSubset/ContactLanguages.php diff --git a/config/schema/oe_contact_forms.schema.yml b/config/schema/oe_contact_forms.schema.yml index e190576c..c3d2dc4a 100644 --- a/config/schema/oe_contact_forms.schema.yml +++ b/config/schema/oe_contact_forms.schema.yml @@ -35,6 +35,12 @@ contact.form.*.third_party.oe_contact_forms: oe_country_residence: type: string label: 'Country of residence' + oe_preferred_language: + type: string + label: 'Preferred contact language' + oe_alternative_language: + type: string + label: 'Alternative contact language' oe_telephone: type: string label: 'Phone' diff --git a/js/related_checkboxes.js b/js/related_checkboxes.js new file mode 100644 index 00000000..5db5fc72 --- /dev/null +++ b/js/related_checkboxes.js @@ -0,0 +1,29 @@ +/** + * @file + * "Related checkboxes" library file. + */ +(function (Drupal, $) { + /** + * Sets state of "Alternative contact language" checkbox based on "Preferred contact language" state. + */ + Drupal.behaviors.oeContactFormsRelatedCheckboxes = { + attach: function (context) { + let prefered_language_checkbox = $(context).find('input[name="corporate_fields[optional_fields][oe_preferred_language]"]'); + let alternative_language_checkbox = $(context).find('input[name="corporate_fields[optional_fields][oe_alternative_language]"]'); + + // Disable "Alternative contact language" if "Preferred contact language" is disabled after page load. + if (prefered_language_checkbox.prop('checked') !== true) { + alternative_language_checkbox.attr('disabled', true); + } + + // Disable/enable "Alternative contact language" based on "Preferred contact language" state. + prefered_language_checkbox.once().click({checkbox: alternative_language_checkbox}, function(e) { + e.data.checkbox.attr('disabled', !$(this).prop('checked')); + if (!$(this).prop('checked')) { + e.data.checkbox.prop('checked', false); + } + }); + }, + }; + +})(Drupal, jQuery); diff --git a/oe_contact_forms.install b/oe_contact_forms.install index ac04f806..1511c299 100644 --- a/oe_contact_forms.install +++ b/oe_contact_forms.install @@ -15,6 +15,7 @@ function oe_contact_forms_install($is_syncing) { if (!$is_syncing) { $graphs = [ 'country' => 'http://publications.europa.eu/resource/authority/country', + 'language' => 'http://publications.europa.eu/resource/authority/language', ]; \Drupal::service('rdf_skos.skos_graph_configurator')->addGraphs($graphs); } diff --git a/oe_contact_forms.libraries.yml b/oe_contact_forms.libraries.yml new file mode 100644 index 00000000..2c8d5f30 --- /dev/null +++ b/oe_contact_forms.libraries.yml @@ -0,0 +1,6 @@ +related_checkboxes: + js: + js/related_checkboxes.js: {} + dependencies: + - core/drupal + - core/jquery diff --git a/oe_contact_forms.module b/oe_contact_forms.module index 635dbb13..a7145fd1 100644 --- a/oe_contact_forms.module +++ b/oe_contact_forms.module @@ -19,6 +19,7 @@ use Drupal\Core\Entity\Display\EntityFormDisplayInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Cache\Cache; +use Drupal\Component\Render\MarkupInterface; /** * Implements hook_form_FORM_ID_alter() for contact_storage_export(). @@ -215,6 +216,8 @@ function oe_contact_forms_form_contact_form_form_alter(&$form, FormStateInterfac '#title' => t('Optional fields'), '#options' => [ 'oe_country_residence' => t('Country of residence'), + 'oe_preferred_language' => t('Preferred contact language'), + 'oe_alternative_language' => t('Alternative contact language'), 'oe_telephone' => t('Phone'), ], '#description' => t("Please specify which optional fields you'd like to include in the form."), @@ -224,7 +227,9 @@ function oe_contact_forms_form_contact_form_form_alter(&$form, FormStateInterfac // To not flatten the data in $form_state. $form['#tree'] = TRUE; $form['#validate'][] = '_oe_contact_forms_email_validate'; + $form['#validate'][] = '_oe_contact_forms_alternative_contact_language_validate'; } + $form['#attached']['library'][] = 'oe_contact_forms/related_checkboxes'; $form['#entity_builders'][] = '_oe_contact_forms_contact_form_builder'; } @@ -468,6 +473,23 @@ function _oe_contact_forms_email_validate(array &$form, FormStateInterface $form $form_state->setValue('corporate_fields', $values); } +/** + * Validate alternative contact language field if set. + * + * @param array $form + * The form. + * @param Drupal\Core\Form\FormStateInterface $form_state + * The form state. + */ +function _oe_contact_forms_alternative_contact_language_validate(array &$form, FormStateInterface $form_state): void { + $values = &$form_state->getValue('corporate_fields'); + if (empty($values['optional_fields']['oe_preferred_language'])) { + // Uncheck alternative contact language if preferred language is unchecked. + $values['optional_fields']['oe_alternative_language'] = ''; + $form_state->setValue('corporate_fields', $values); + } +} + /** * Implements hook_form_FORM_ID_alter() for contact_message_form(). */ @@ -481,7 +503,13 @@ function oe_contact_forms_form_contact_message_form_alter(&$form, FormStateInter // We only want corporate fields on corporate forms. if (!$contact_form->getThirdPartySetting('oe_contact_forms', 'is_corporate_form', FALSE)) { - $fields = ['oe_country_residence', 'oe_telephone', 'oe_topic']; + $fields = [ + 'oe_country_residence', + 'oe_telephone', + 'oe_topic', + 'oe_preferred_language', + 'oe_alternative_language', + ]; foreach ($fields as $field) { if (!isset($form[$field])) { @@ -524,6 +552,9 @@ function oe_contact_forms_entity_base_field_info(EntityTypeInterface $entity_typ ], ]); + $fields['oe_preferred_language'] = oe_contact_forms_create_language_base_field(t('Preferred contact language')); + $fields['oe_alternative_language'] = oe_contact_forms_create_language_base_field(t('Alternative contact language')); + $fields['oe_telephone'] = BaseFieldDefinition::create('telephone') ->setLabel(t('Phone')) ->setSettings([ @@ -552,6 +583,42 @@ function oe_contact_forms_entity_base_field_info(EntityTypeInterface $entity_typ } } +/** + * Creates language base fields. + * + * @param \Drupal\Component\Render\MarkupInterface $title + * Field label. + * + * @return \Drupal\Core\Field\BaseFieldDefinition + * Base field definition. + */ +function oe_contact_forms_create_language_base_field(MarkupInterface $title): BaseFieldDefinition { + return BaseFieldDefinition::create('skos_concept_entity_reference') + ->setLabel($title) + ->setSetting('target_type', 'skos_concept') + ->setSetting('handler', 'default:skos_concept') + ->setSetting('handler_settings', [ + 'target_bundles' => NULL, + 'auto_create' => FALSE, + 'concept_schemes' => [ + 'http://publications.europa.eu/resource/authority/language', + ], + 'concept_subset' => 'contact_languages', + ]) + ->setDisplayOptions('form', [ + 'type' => 'skos_concept_entity_reference_options_select', + 'settings' => [ + 'sort' => 'label', + ], + ]) + ->setDisplayOptions('view', [ + 'weight' => 0, + 'settings' => [ + 'link' => FALSE, + ], + ]); +} + /** * Set dynamic allowed values for the topic field. * @@ -642,14 +709,28 @@ function oe_contact_forms_entity_form_display_alter(EntityFormDisplayInterface $ 'sort' => 'label', ], ]) - ->setComponent('oe_telephone', [ + ->setComponent('oe_preferred_language', [ 'weight' => 6, + 'type' => 'skos_concept_entity_reference_options_select', + 'settings' => [ + 'sort' => 'label', + ], ]) - ->setComponent('copy', [ + ->setComponent('oe_alternative_language', [ 'weight' => 7, + 'type' => 'skos_concept_entity_reference_options_select', + 'settings' => [ + 'sort' => 'label', + ], ]) - ->setComponent('privacy_policy', [ + ->setComponent('oe_telephone', [ 'weight' => 8, + ]) + ->setComponent('copy', [ + 'weight' => 9, + ]) + ->setComponent('privacy_policy', [ + 'weight' => 10, ]); } } diff --git a/oe_contact_forms.post_update.php b/oe_contact_forms.post_update.php index 90446127..5226b11a 100644 --- a/oe_contact_forms.post_update.php +++ b/oe_contact_forms.post_update.php @@ -14,3 +14,67 @@ function oe_contact_forms_post_update_00001(): void { \Drupal::service('module_installer')->install(['oe_corporate_countries']); \Drupal::service('kernel')->invalidateContainer(); } + +/** + * Add "Preferred contact language", "Alternative contact language" base fields. + */ +function oe_contact_forms_post_update_00002(): void { + $graphs = [ + 'language' => 'http://publications.europa.eu/resource/authority/language', + ]; + \Drupal::service('rdf_skos.skos_graph_configurator')->addGraphs($graphs); + + // Create new base fields. + $definition_update_manager = \Drupal::entityDefinitionUpdateManager(); + $storage_definition = BaseFieldDefinition::create('skos_concept_entity_reference') + ->setLabel(t('Preferred contact language')) + ->setSetting('target_type', 'skos_concept') + ->setSetting('handler', 'default:skos_concept') + ->setSetting('handler_settings', [ + 'target_bundles' => NULL, + 'auto_create' => FALSE, + 'concept_schemes' => [ + 'http://publications.europa.eu/resource/authority/language', + ], + 'concept_subset' => 'contact_languages', + ]) + ->setDisplayOptions('form', [ + 'type' => 'skos_concept_entity_reference_options_select', + 'settings' => [ + 'sort' => 'label', + ], + ]) + ->setDisplayOptions('view', [ + 'weight' => 0, + 'settings' => [ + 'link' => FALSE, + ], + ]); + $definition_update_manager->installFieldStorageDefinition('oe_preferred_language', 'contact_message', 'oe_contact_forms', $storage_definition); + + $storage_definition = BaseFieldDefinition::create('skos_concept_entity_reference') + ->setLabel(t('Alternative contact language')) + ->setSetting('target_type', 'skos_concept') + ->setSetting('handler', 'default:skos_concept') + ->setSetting('handler_settings', [ + 'target_bundles' => NULL, + 'auto_create' => FALSE, + 'concept_schemes' => [ + 'http://publications.europa.eu/resource/authority/language', + ], + 'concept_subset' => 'contact_languages', + ]) + ->setDisplayOptions('form', [ + 'type' => 'skos_concept_entity_reference_options_select', + 'settings' => [ + 'sort' => 'label', + ], + ]) + ->setDisplayOptions('view', [ + 'weight' => 0, + 'settings' => [ + 'link' => FALSE, + ], + ]); + $definition_update_manager->installFieldStorageDefinition('oe_alternative_language', 'contact_message', 'oe_contact_forms', $storage_definition); +} diff --git a/src/Form/ContactMessageForm.php b/src/Form/ContactMessageForm.php index f784f893..e631b975 100644 --- a/src/Form/ContactMessageForm.php +++ b/src/Form/ContactMessageForm.php @@ -48,12 +48,16 @@ public function form(array $form, FormStateInterface $form_state): array { // Hide the optional fields the form manager has not configured to be used. $optional_selected = $contact_form->getThirdPartySetting('oe_contact_forms', 'optional_fields', []); - if (!in_array('oe_country_residence', $optional_selected)) { - $form['oe_country_residence']['#access'] = FALSE; - } - - if (!in_array('oe_telephone', $optional_selected)) { - $form['oe_telephone']['#access'] = FALSE; + $optional_fields = [ + 'oe_country_residence', + 'oe_preferred_language', + 'oe_alternative_language', + 'oe_telephone', + ]; + foreach ($optional_fields as $optional_field) { + if (!in_array($optional_field, $optional_selected)) { + $form[$optional_field]['#access'] = FALSE; + } } // Alter the Topics label with the value configured by the form manager. diff --git a/src/Plugin/ConceptSubset/ContactLanguages.php b/src/Plugin/ConceptSubset/ContactLanguages.php new file mode 100644 index 00000000..3f8baeed --- /dev/null +++ b/src/Plugin/ConceptSubset/ContactLanguages.php @@ -0,0 +1,58 @@ + 'http://publications.europa.eu/resource/authority/language/BUL', + 'Spanish' => 'http://publications.europa.eu/resource/authority/language/SPA', + 'Czech' => 'http://publications.europa.eu/resource/authority/language/CES', + 'Danish' => 'http://publications.europa.eu/resource/authority/language/DAN', + 'German' => 'http://publications.europa.eu/resource/authority/language/DEU', + 'Estonian' => 'http://publications.europa.eu/resource/authority/language/EST', + 'Greek' => 'http://publications.europa.eu/resource/authority/language/ELL', + 'English' => 'http://publications.europa.eu/resource/authority/language/ENG', + 'French' => 'http://publications.europa.eu/resource/authority/language/FRA', + 'Irish' => 'http://publications.europa.eu/resource/authority/language/GLE', + 'Croatian' => 'http://publications.europa.eu/resource/authority/language/HRV', + 'Italian' => 'http://publications.europa.eu/resource/authority/language/ITA', + 'Latvian' => 'http://publications.europa.eu/resource/authority/language/LAV', + 'Lithuanian' => 'http://publications.europa.eu/resource/authority/language/LIT', + 'Hungarian' => 'http://publications.europa.eu/resource/authority/language/HUN', + 'Maltese' => 'http://publications.europa.eu/resource/authority/language/MLT', + 'Dutch' => 'http://publications.europa.eu/resource/authority/language/NLD', + 'Polish' => 'http://publications.europa.eu/resource/authority/language/POL', + 'Portuguese' => 'http://publications.europa.eu/resource/authority/language/POR', + 'Romanian' => 'http://publications.europa.eu/resource/authority/language/RON', + 'Slovak' => 'http://publications.europa.eu/resource/authority/language/SLK', + 'Slovenian' => 'http://publications.europa.eu/resource/authority/language/SLV', + 'Finnish' => 'http://publications.europa.eu/resource/authority/language/FIN', + 'Swedish' => 'http://publications.europa.eu/resource/authority/language/SWE', + ]; + + $query->condition('id', array_values($languages), 'IN'); + } + +} diff --git a/tests/src/FunctionalJavascript/CorporateContactFormTest.php b/tests/src/FunctionalJavascript/CorporateContactFormTest.php index 69db1cf4..b0f90085 100644 --- a/tests/src/FunctionalJavascript/CorporateContactFormTest.php +++ b/tests/src/FunctionalJavascript/CorporateContactFormTest.php @@ -8,12 +8,15 @@ use Drupal\contact\Entity\Message; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; use Drupal\node\Entity\Node; +use Drupal\Tests\sparql_entity_storage\Traits\SparqlConnectionTrait; /** * Tests the corporate contact forms. */ class CorporateContactFormTest extends WebDriverTestBase { + use SparqlConnectionTrait; + /** * Corporate fields to test against. * @@ -29,6 +32,8 @@ class CorporateContactFormTest extends WebDriverTestBase { // For expose_as_block default is true so we test false. 'corporate_fields[expose_as_block]' => FALSE, 'corporate_fields[optional_fields][oe_country_residence]' => TRUE, + 'corporate_fields[optional_fields][oe_preferred_language]' => TRUE, + 'corporate_fields[optional_fields][oe_alternative_language]' => TRUE, 'corporate_fields[optional_fields][oe_telephone]' => TRUE, ]; @@ -116,6 +121,8 @@ public function testCorporateContactFormExport(): void { 'edit-columns-created', 'edit-columns-uid', 'edit-columns-oe-country-residence', + 'edit-columns-oe-preferred-language', + 'edit-columns-oe-alternative-language', 'edit-columns-oe-telephone', 'edit-columns-oe-telephone', ]; @@ -136,8 +143,8 @@ public function testCorporateContactFormExport(): void { $absolute_path = \Drupal::service('file_system')->realpath($file->getFileUri()); $actual = file_get_contents($absolute_path); - $headers = '"Message ID",Language,"Form ID","The sender\'s name","The sender\'s email",Subject,Message,Copy,"Recipient ID",Created,"User ID","Country of residence",Phone,Topic'; - $values = '1,English,,example,admin@example.com,"Test subject","Test message",,,"Fri, 02/17/2017 - 19:52",0,,,'; + $headers = '"Message ID",Language,"Form ID","The sender\'s name","The sender\'s email",Subject,Message,Copy,"Recipient ID",Created,"User ID","Country of residence","Preferred contact language","Alternative contact language",Phone,Topic'; + $values = '1,English,,example,admin@example.com,"Test subject","Test message",,,"Fri, 02/17/2017 - 19:52",0,,,,,'; $expected = $headers . PHP_EOL . $values; $this->assertEquals($expected, $actual); } @@ -172,6 +179,22 @@ public function testCorporateContactForm(): void { // Assert fields are now visible. $this->assertFieldsVisible(TRUE); + // Assert alternative contact language is active only when preferred + // contact language field is active. + $preferred_language_element = $page->findField('corporate_fields[optional_fields][oe_preferred_language]'); + $alternative_language_element = $page->findField('corporate_fields[optional_fields][oe_alternative_language]'); + $this->assertFalse($preferred_language_element->isChecked()); + $this->assertFalse($alternative_language_element->isChecked()); + $this->assertEquals('disabled', $alternative_language_element->getAttribute('disabled')); + $preferred_language_element->click(); + $this->assertEmpty($alternative_language_element->getAttribute('disabled')); + $alternative_language_element->click(); + $this->assertTrue($preferred_language_element->isChecked()); + $this->assertTrue($alternative_language_element->isChecked()); + $preferred_language_element->click(); + $this->assertFalse($alternative_language_element->isChecked()); + $this->assertEquals('disabled', $alternative_language_element->getAttribute('disabled')); + // Assert expose_as_block is checked by default. $element = $page->findField('corporate_fields[expose_as_block]'); $this->assertNotEmpty($element); @@ -560,6 +583,8 @@ protected function checkCorporateFieldsInStorage($max_delta = 1): void { 'expose_as_block' => FALSE, 'optional_fields' => [ 'oe_country_residence' => 'oe_country_residence', + 'oe_preferred_language' => 'oe_preferred_language', + 'oe_alternative_language' => 'oe_alternative_language', 'oe_telephone' => 'oe_telephone', ], 'topics' => [], diff --git a/tests/src/FunctionalJavascript/MessageFormTest.php b/tests/src/FunctionalJavascript/MessageFormTest.php index 9c998dc3..599dc05e 100644 --- a/tests/src/FunctionalJavascript/MessageFormTest.php +++ b/tests/src/FunctionalJavascript/MessageFormTest.php @@ -108,6 +108,8 @@ public function testCorporateForm(): void { 'http://publications.europa.eu/resource/authority/country/DZA' => 'Algeria', ]; $this->assertEquals($expected_countries, $actual_countries); + $assert->fieldNotExists('oe_preferred_language'); + $assert->fieldNotExists('oe_alternative_language'); $assert->fieldNotExists('oe_telephone[0][value]'); $assert->fieldExists('oe_topic'); $assert->fieldExists('privacy_policy'); @@ -149,6 +151,8 @@ public function testCorporateForm(): void { $contact_form->setThirdPartySetting('oe_contact_forms', 'privacy_policy', $privacy_url); $optional_selected = [ 'oe_country_residence' => 'oe_country_residence', + 'oe_preferred_language' => 'oe_preferred_language', + 'oe_alternative_language' => 'oe_alternative_language', 'oe_telephone' => 'oe_telephone', ]; $contact_form->setThirdPartySetting('oe_contact_forms', 'optional_fields', $optional_selected); @@ -173,8 +177,27 @@ public function testCorporateForm(): void { $assert->pageTextContains($header); $assert->elementAttributeContains('xpath', "//div[contains(@class, 'form-item-privacy-policy')]//a", 'href', $privacy_url); $assert->fieldExists('oe_country_residence'); + $assert->fieldExists('oe_preferred_language'); + $assert->fieldExists('oe_alternative_language'); $assert->fieldExists('oe_telephone[0][value]'); + // Assert contact language fields contains 24 EU languages. + $expected_languages = [ + '_none' => '- None -', + 'http://publications.europa.eu/resource/authority/language/BUL' => 'Bulgarian', + 'http://publications.europa.eu/resource/authority/language/HRV' => 'Croatian', + 'http://publications.europa.eu/resource/authority/language/CES' => 'Czech', + 'http://publications.europa.eu/resource/authority/language/DAN' => 'Danish', + ]; + $options = $this->getOptions('oe_preferred_language'); + $this->assertEquals(25, count($options)); + $actual_preferred_language = array_slice($options, 0, 5); + $this->assertEquals($expected_languages, $actual_preferred_language); + $options = $this->getOptions('oe_alternative_language'); + $this->assertEquals(25, count($options)); + $actual_alternative_language = array_slice($options, 0, 5); + $this->assertEquals($expected_languages, $actual_alternative_language); + foreach ($topics as $topic) { $assert->elementExists('named', ['option', $topic['topic_name']]); } diff --git a/tests/src/Kernel/BaseFieldTest.php b/tests/src/Kernel/BaseFieldTest.php index 9d04da56..d049aaff 100644 --- a/tests/src/Kernel/BaseFieldTest.php +++ b/tests/src/Kernel/BaseFieldTest.php @@ -47,6 +47,8 @@ public function testCorporateFormBaseFields(): void { // Set correct values. $data['oe_country_residence'] = 'http://publications.europa.eu/resource/authority/country/BEL'; + $data['oe_preferred_language'] = 'http://publications.europa.eu/resource/authority/language/SPA'; + $data['oe_alternative_language'] = 'http://publications.europa.eu/resource/authority/language/FRA'; $data['oe_telephone'] = '0123456'; $data['oe_topic'] = 'Topic name'; @@ -63,6 +65,8 @@ public function testCorporateFormBaseFields(): void { $message = $entity_type_manager->load($message->id()); $this->assertEquals($data['oe_country_residence'], $message->get('oe_country_residence')->getValue()['0']['target_id']); + $this->assertEquals($data['oe_preferred_language'], $message->get('oe_preferred_language')->getValue()['0']['target_id']); + $this->assertEquals($data['oe_alternative_language'], $message->get('oe_alternative_language')->getValue()['0']['target_id']); $this->assertEquals($data['oe_telephone'], $message->get('oe_telephone')->getValue()['0']['value']); $this->assertEquals($data['oe_topic'], $message->get('oe_topic')->getValue()['0']['value']); } @@ -93,6 +97,8 @@ public function testDefaultForm(): void { $form = \Drupal::service('entity.form_builder')->getForm($message, 'default'); $this->assertEquals(FALSE, $form['oe_country_residence']['#access'], 'Check that country is not available.'); + $this->assertEquals(FALSE, $form['oe_preferred_language']['#access'], 'Check that preferred contact language is not available.'); + $this->assertEquals(FALSE, $form['oe_alternative_language']['#access'], 'Check that alternative contact language is not available.'); $this->assertEquals(FALSE, $form['oe_telephone']['#access'], 'Check that telephone is not available.'); $this->assertEquals(FALSE, $form['oe_topic']['#access'], 'Check that topic is not available.'); } From 130c7ab1dfad4886d0d29b37b71ef2b7b37ab965 Mon Sep 17 00:00:00 2001 From: Evgenii Nikitin Date: Thu, 20 Oct 2022 16:39:11 +0200 Subject: [PATCH 02/12] EWPP-2701: Explicit requirement for consolidation/annotated-command version due to compatibility with openeuropa/task-runner-drupal-project-symlink. --- composer.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8439cdc5..6c14bb0c 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ }, "require-dev": { "composer/installers": "~1.11", + "consolidation/annotated-command": "4.5.6", "drupal/config_devel": "~1.2", "drupal/core-composer-scaffold": "^9.3", "drupal/core-dev": "^9.3", @@ -30,7 +31,8 @@ }, "_readme": [ "Explicit requirement for drupal/csv_serialization version due to its dependency compatibility league/csv with PHP7.4", - "Explicit requirement for drupal/token version due to its dependency compatibility with Drupal9." + "Explicit requirement for drupal/token version due to its dependency compatibility with Drupal9.", + "Explicit requirement for consolidation/annotated-command version due to compatibility with openeuropa/task-runner-drupal-project-symlink." ], "scripts": { "post-install-cmd": "./vendor/bin/run drupal:site-setup", From fda8016e8eaab5b7bb397892dd3d943214a21e9f Mon Sep 17 00:00:00 2001 From: Evgenii Nikitin Date: Sat, 22 Oct 2022 16:09:52 +0200 Subject: [PATCH 03/12] EWPP-2701: Add ContactLanguagesSubsetTest. --- js/related_checkboxes.js | 7 ++ oe_contact_forms.module | 13 ++- oe_contact_forms.post_update.php | 36 ++----- .../CorporateContactFormTest.php | 13 +++ .../src/Kernel/ContactLanguagesSubsetTest.php | 97 +++++++++++++++++++ 5 files changed, 131 insertions(+), 35 deletions(-) create mode 100644 tests/src/Kernel/ContactLanguagesSubsetTest.php diff --git a/js/related_checkboxes.js b/js/related_checkboxes.js index 5db5fc72..c66b055d 100644 --- a/js/related_checkboxes.js +++ b/js/related_checkboxes.js @@ -3,8 +3,15 @@ * "Related checkboxes" library file. */ (function (Drupal, $) { + 'use strict'; + /** * Sets state of "Alternative contact language" checkbox based on "Preferred contact language" state. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches the behavior for oeContactFormsRelatedCheckboxes. */ Drupal.behaviors.oeContactFormsRelatedCheckboxes = { attach: function (context) { diff --git a/oe_contact_forms.module b/oe_contact_forms.module index a7145fd1..0e9642d2 100644 --- a/oe_contact_forms.module +++ b/oe_contact_forms.module @@ -227,7 +227,6 @@ function oe_contact_forms_form_contact_form_form_alter(&$form, FormStateInterfac // To not flatten the data in $form_state. $form['#tree'] = TRUE; $form['#validate'][] = '_oe_contact_forms_email_validate'; - $form['#validate'][] = '_oe_contact_forms_alternative_contact_language_validate'; } $form['#attached']['library'][] = 'oe_contact_forms/related_checkboxes'; @@ -409,6 +408,11 @@ function _oe_contact_forms_contact_form_builder($entity_type, ContactFormInterfa $contact_form->setThirdPartySetting('oe_contact_forms', 'is_corporate_form', $is_corporate_form); if ($is_corporate_form && !empty($values)) { + if (empty($values['optional_fields']['oe_preferred_language'])) { + // Uncheck alternative language if preferred language is unchecked. + $values['optional_fields']['oe_alternative_language'] = 0; + } + $contact_form->setThirdPartySetting('oe_contact_forms', 'topic_label', $values['topic_label']); $contact_form->setThirdPartySetting('oe_contact_forms', 'email_subject', $values['email_subject']); $contact_form->setThirdPartySetting('oe_contact_forms', 'header', $values['header']); @@ -482,12 +486,7 @@ function _oe_contact_forms_email_validate(array &$form, FormStateInterface $form * The form state. */ function _oe_contact_forms_alternative_contact_language_validate(array &$form, FormStateInterface $form_state): void { - $values = &$form_state->getValue('corporate_fields'); - if (empty($values['optional_fields']['oe_preferred_language'])) { - // Uncheck alternative contact language if preferred language is unchecked. - $values['optional_fields']['oe_alternative_language'] = ''; - $form_state->setValue('corporate_fields', $values); - } + } /** diff --git a/oe_contact_forms.post_update.php b/oe_contact_forms.post_update.php index 5226b11a..0e65f947 100644 --- a/oe_contact_forms.post_update.php +++ b/oe_contact_forms.post_update.php @@ -7,6 +7,8 @@ declare(strict_types = 1); +use Drupal\Core\Field\BaseFieldDefinition; + /** * Enable the corporate countries component. */ @@ -25,8 +27,7 @@ function oe_contact_forms_post_update_00002(): void { \Drupal::service('rdf_skos.skos_graph_configurator')->addGraphs($graphs); // Create new base fields. - $definition_update_manager = \Drupal::entityDefinitionUpdateManager(); - $storage_definition = BaseFieldDefinition::create('skos_concept_entity_reference') + $preferred_language_definition = BaseFieldDefinition::create('skos_concept_entity_reference') ->setLabel(t('Preferred contact language')) ->setSetting('target_type', 'skos_concept') ->setSetting('handler', 'default:skos_concept') @@ -50,31 +51,10 @@ function oe_contact_forms_post_update_00002(): void { 'link' => FALSE, ], ]); - $definition_update_manager->installFieldStorageDefinition('oe_preferred_language', 'contact_message', 'oe_contact_forms', $storage_definition); + $alternative_language_definition = clone $preferred_language_definition; + $alternative_language_definition->setLabel(t('Alternative contact language')); - $storage_definition = BaseFieldDefinition::create('skos_concept_entity_reference') - ->setLabel(t('Alternative contact language')) - ->setSetting('target_type', 'skos_concept') - ->setSetting('handler', 'default:skos_concept') - ->setSetting('handler_settings', [ - 'target_bundles' => NULL, - 'auto_create' => FALSE, - 'concept_schemes' => [ - 'http://publications.europa.eu/resource/authority/language', - ], - 'concept_subset' => 'contact_languages', - ]) - ->setDisplayOptions('form', [ - 'type' => 'skos_concept_entity_reference_options_select', - 'settings' => [ - 'sort' => 'label', - ], - ]) - ->setDisplayOptions('view', [ - 'weight' => 0, - 'settings' => [ - 'link' => FALSE, - ], - ]); - $definition_update_manager->installFieldStorageDefinition('oe_alternative_language', 'contact_message', 'oe_contact_forms', $storage_definition); + $update_manager = \Drupal::entityDefinitionUpdateManager(); + $update_manager->installFieldStorageDefinition('oe_preferred_language', 'contact_message', 'oe_contact_forms', $preferred_language_definition); + $update_manager->installFieldStorageDefinition('oe_alternative_language', 'contact_message', 'oe_contact_forms', $alternative_language_definition); } diff --git a/tests/src/FunctionalJavascript/CorporateContactFormTest.php b/tests/src/FunctionalJavascript/CorporateContactFormTest.php index b0f90085..0e2a9cac 100644 --- a/tests/src/FunctionalJavascript/CorporateContactFormTest.php +++ b/tests/src/FunctionalJavascript/CorporateContactFormTest.php @@ -275,6 +275,12 @@ public function testCorporateContactForm(): void { // Test with node alias. $element->setValue($alias); + + // Uncheck alternative language to assert its state after form loading. + $alternative_language_element = $page->findField('corporate_fields[optional_fields][oe_alternative_language]'); + $alternative_language_element->click(); + $this->assertFalse($alternative_language_element->isChecked()); + $page->pressButton('Save'); $assert->pageTextContains('Contact form Test form has been updated.'); @@ -282,6 +288,13 @@ public function testCorporateContactForm(): void { $element = $page->findField($field_name); $this->assertNotEmpty($element); $this->assertEquals($alias, $element->getValue()); + + // Assert alternative language element is active if preferred is checked. + $preferred_language_element = $page->findField('corporate_fields[optional_fields][oe_preferred_language]'); + $alternative_language_element = $page->findField('corporate_fields[optional_fields][oe_alternative_language]'); + $this->assertTrue($preferred_language_element->isChecked()); + $this->assertFalse($alternative_language_element->isChecked()); + $this->assertEmpty($alternative_language_element->getAttribute('disabled')); } /** diff --git a/tests/src/Kernel/ContactLanguagesSubsetTest.php b/tests/src/Kernel/ContactLanguagesSubsetTest.php new file mode 100644 index 00000000..807c59e2 --- /dev/null +++ b/tests/src/Kernel/ContactLanguagesSubsetTest.php @@ -0,0 +1,97 @@ + 'http://publications.europa.eu/resource/authority/language', + ]; + \Drupal::service('rdf_skos.skos_graph_configurator')->addGraphs($graphs); + } + + /** + * Tests that only EU languages are allowed in "Contact languages" subset. + */ + public function testContactLanguagesSubset(): void { + $configuration = [ + 'target_type' => 'skos_concept', + 'concept_schemes' => ['http://publications.europa.eu/resource/authority/language'], + 'concept_subset' => 'contact_languages', + ]; + $selection = SkosConceptSelection::create($this->container, $configuration, 'default:skos_concept', []); + $ids = array_merge(self::EU_LANGUAGES, self::NON_EU_LANGUAGES); + $result = array_values($selection->validateReferenceableEntities($ids)); + $this->assertEquals(self::EU_LANGUAGES, $result); + } + +} From 91ad867244d10fcc317e540fdab0e04be85edad1 Mon Sep 17 00:00:00 2001 From: Evgenii Nikitin Date: Tue, 25 Oct 2022 17:50:45 +0200 Subject: [PATCH 04/12] EWPP-2701: Remove useless method. --- oe_contact_forms.module | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/oe_contact_forms.module b/oe_contact_forms.module index 0e9642d2..536d629f 100644 --- a/oe_contact_forms.module +++ b/oe_contact_forms.module @@ -477,18 +477,6 @@ function _oe_contact_forms_email_validate(array &$form, FormStateInterface $form $form_state->setValue('corporate_fields', $values); } -/** - * Validate alternative contact language field if set. - * - * @param array $form - * The form. - * @param Drupal\Core\Form\FormStateInterface $form_state - * The form state. - */ -function _oe_contact_forms_alternative_contact_language_validate(array &$form, FormStateInterface $form_state): void { - -} - /** * Implements hook_form_FORM_ID_alter() for contact_message_form(). */ From 512abda267529ca80c39bcdc092e05e2f470cb1a Mon Sep 17 00:00:00 2001 From: Evgenii Nikitin Date: Wed, 26 Oct 2022 11:41:24 +0200 Subject: [PATCH 05/12] EWPP-2701: Remove consolidation/annotated-command. --- composer.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 6c14bb0c..8439cdc5 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,6 @@ }, "require-dev": { "composer/installers": "~1.11", - "consolidation/annotated-command": "4.5.6", "drupal/config_devel": "~1.2", "drupal/core-composer-scaffold": "^9.3", "drupal/core-dev": "^9.3", @@ -31,8 +30,7 @@ }, "_readme": [ "Explicit requirement for drupal/csv_serialization version due to its dependency compatibility league/csv with PHP7.4", - "Explicit requirement for drupal/token version due to its dependency compatibility with Drupal9.", - "Explicit requirement for consolidation/annotated-command version due to compatibility with openeuropa/task-runner-drupal-project-symlink." + "Explicit requirement for drupal/token version due to its dependency compatibility with Drupal9." ], "scripts": { "post-install-cmd": "./vendor/bin/run drupal:site-setup", From c1eface16b3455703f13c8114636382305a932e4 Mon Sep 17 00:00:00 2001 From: Evgenii Nikitin Date: Thu, 20 Oct 2022 14:55:54 +0200 Subject: [PATCH 06/12] EWPP-2702: Allow form managers to select available languages. --- composer.json | 7 +- config/schema/oe_contact_forms.schema.yml | 16 ++ js/related_checkboxes.js | 3 +- oe_contact_forms.info.yml | 1 + oe_contact_forms.module | 170 +++++++++++++++++- oe_contact_forms.post_update.php | 6 +- .../CorporateContactFormTest.php | 137 +++++++++++++- .../FunctionalJavascript/MessageFormTest.php | 27 ++- 8 files changed, 347 insertions(+), 20 deletions(-) diff --git a/composer.json b/composer.json index 8439cdc5..3293b0ff 100644 --- a/composer.json +++ b/composer.json @@ -6,15 +6,16 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { + "php": ">=7.4", "cweagans/composer-patches": "~1.4", - "drupal/core": "^9.3", "drupal/contact_storage": "^1.2", "drupal/contact_storage_export": "^1.14", + "drupal/core": "^9.3", "drupal/csv_serialization": "^2.0", + "drupal/multivalue_form_element": "^1.0@beta", "drupal/token": "^1.8", - "openeuropa/rdf_skos": "^1.0", "openeuropa/oe_corporate_countries": "^2.0", - "php": ">=7.4" + "openeuropa/rdf_skos": "^1.0" }, "require-dev": { "composer/installers": "~1.11", diff --git a/config/schema/oe_contact_forms.schema.yml b/config/schema/oe_contact_forms.schema.yml index c3d2dc4a..f4b8d95f 100644 --- a/config/schema/oe_contact_forms.schema.yml +++ b/config/schema/oe_contact_forms.schema.yml @@ -44,6 +44,22 @@ contact.form.*.third_party.oe_contact_forms: oe_telephone: type: string label: 'Phone' + override_languages: + type: mapping + label: 'Override languages' + mapping: + oe_preferred_language_options: + type: sequence + label: 'Preferred contact language options' + sequence: + type: string + label: 'Skos term' + oe_alternative_language_options: + type: sequence + label: 'Alternative contact language options' + sequence: + type: string + label: 'Skos term' topics: type: sequence sequence: diff --git a/js/related_checkboxes.js b/js/related_checkboxes.js index c66b055d..a79bdfb6 100644 --- a/js/related_checkboxes.js +++ b/js/related_checkboxes.js @@ -25,10 +25,11 @@ // Disable/enable "Alternative contact language" based on "Preferred contact language" state. prefered_language_checkbox.once().click({checkbox: alternative_language_checkbox}, function(e) { - e.data.checkbox.attr('disabled', !$(this).prop('checked')); if (!$(this).prop('checked')) { e.data.checkbox.prop('checked', false); + e.data.checkbox.change(); } + e.data.checkbox.attr('disabled', !$(this).prop('checked')); }); }, }; diff --git a/oe_contact_forms.info.yml b/oe_contact_forms.info.yml index 535513a3..77148b98 100644 --- a/oe_contact_forms.info.yml +++ b/oe_contact_forms.info.yml @@ -13,3 +13,4 @@ dependencies: - contact_storage_export:contact_storage_export - rdf_skos:rdf_skos - oe_corporate_countries:oe_corporate_countries + - multivalue_form_element:multivalue_form_element diff --git a/oe_contact_forms.module b/oe_contact_forms.module index 536d629f..cfccf26a 100644 --- a/oe_contact_forms.module +++ b/oe_contact_forms.module @@ -223,7 +223,58 @@ function oe_contact_forms_form_contact_form_form_alter(&$form, FormStateInterfac '#description' => t("Please specify which optional fields you'd like to include in the form."), '#default_value' => $contact_form->getThirdPartySetting('oe_contact_forms', 'optional_fields', []), ]; - + $form['corporate_fields']['override_languages'] = [ + '#type' => 'details', + '#title' => t('Override languages'), + '#states' => [ + 'invisible' => [ + ':input[name="corporate_fields[optional_fields][oe_preferred_language]"]' => ['checked' => FALSE], + ], + ], + ]; + $skos_concept_storage = \Drupal::entityTypeManager()->getStorage('skos_concept'); + $default_language_values = array_values($skos_concept_storage->loadMultiple(_oe_contact_forms_contact_language_values())); + $override_languages_settings = $contact_form->getThirdPartySetting('oe_contact_forms', 'override_languages', []); + $form['corporate_fields']['override_languages']['oe_preferred_language_options'] = [ + '#type' => 'multivalue', + '#title' => t('Preferred contact language options'), + 'target' => [ + '#type' => 'entity_autocomplete', + '#target_type' => 'skos_concept', + '#selection_handler' => 'default:skos_concept', + '#selection_settings' => [ + 'concept_schemes' => [ + 'http://publications.europa.eu/resource/authority/language', + ], + 'concept_subset' => 'contact_languages', + ], + '#maxlength' => 1024, + '#placeholder' => '', + ], + '#default_value' => empty($override_languages_settings['oe_preferred_language_options']) ? $default_language_values : array_values($skos_concept_storage->loadMultiple($override_languages_settings['oe_preferred_language_options'])), + ]; + $form['corporate_fields']['override_languages']['oe_alternative_language_options'] = [ + '#type' => 'multivalue', + '#title' => t('Alternative contact language options'), + 'target' => [ + '#type' => 'entity_autocomplete', + '#target_type' => 'skos_concept', + '#selection_handler' => 'default:skos_concept', + '#selection_settings' => [ + 'concept_schemes' => [ + 'http://publications.europa.eu/resource/authority/language', + ], + 'concept_subset' => 'contact_languages', + ], + '#maxlength' => 1024, + ], + '#states' => [ + 'enabled' => [ + ':input[name="corporate_fields[optional_fields][oe_alternative_language]"]' => ['checked' => TRUE], + ], + ], + '#default_value' => empty($override_languages_settings['oe_alternative_language_options']) ? $default_language_values : array_values($skos_concept_storage->loadMultiple($override_languages_settings['oe_alternative_language_options'])), + ]; // To not flatten the data in $form_state. $form['#tree'] = TRUE; $form['#validate'][] = '_oe_contact_forms_email_validate'; @@ -405,6 +456,19 @@ function _oe_contact_forms_contact_form_builder($entity_type, ContactFormInterfa $is_corporate_form = FALSE; } + // Multivalue field isn't processed fully in ajax request, so exit if + // 'Remove topic' or 'Add another item' buttons has been clicked to prevent + // saving wrong values. + if (in_array($triggered_element['#name'], [ + 'corporate_fields_override_languages_oe_preferred_language_options_add_more', + 'corporate_fields_override_languages_oe_alternative_language_options_add_more', + ])) { + return; + } + if (!empty($triggered_element['#parents'][3]) && $triggered_element['#parents'][3] === 'remove_topic') { + return; + } + $contact_form->setThirdPartySetting('oe_contact_forms', 'is_corporate_form', $is_corporate_form); if ($is_corporate_form && !empty($values)) { @@ -412,6 +476,8 @@ function _oe_contact_forms_contact_form_builder($entity_type, ContactFormInterfa // Uncheck alternative language if preferred language is unchecked. $values['optional_fields']['oe_alternative_language'] = 0; } + $values['override_languages']['oe_preferred_language_options'] = _oe_contact_forms_prepare_language_option_values($values, 'oe_preferred_language', 'oe_preferred_language_options'); + $values['override_languages']['oe_alternative_language_options'] = _oe_contact_forms_prepare_language_option_values($values, 'oe_alternative_language', 'oe_alternative_language_options'); $contact_form->setThirdPartySetting('oe_contact_forms', 'topic_label', $values['topic_label']); $contact_form->setThirdPartySetting('oe_contact_forms', 'email_subject', $values['email_subject']); @@ -421,6 +487,7 @@ function _oe_contact_forms_contact_form_builder($entity_type, ContactFormInterfa $contact_form->setThirdPartySetting('oe_contact_forms', 'allow_canonical_url', $values['allow_canonical_url']); $contact_form->setThirdPartySetting('oe_contact_forms', 'expose_as_block', $values['expose_as_block']); $contact_form->setThirdPartySetting('oe_contact_forms', 'optional_fields', $values['optional_fields']); + $contact_form->setThirdPartySetting('oe_contact_forms', 'override_languages', $values['override_languages']); $contact_form->setThirdPartySetting('oe_contact_forms', 'topics', $values['topics_fieldset']['group']); } else { @@ -433,6 +500,7 @@ function _oe_contact_forms_contact_form_builder($entity_type, ContactFormInterfa 'allow_canonical_url', 'expose_as_block', 'optional_fields', + 'override_languages', 'topics', ]; @@ -442,6 +510,34 @@ function _oe_contact_forms_contact_form_builder($entity_type, ContactFormInterfa } } +/** + * Prepares contact language options. + * + * @param array $values + * Form values. + * @param string $field + * Name of contact language field. + * @param string $field_options + * Name of contact language options field. + * + * @return array + * List of languages to save. + */ +function _oe_contact_forms_prepare_language_option_values(array $values, string $field, string $field_options): array { + // Save language options if contact language is used and they are overridden. + if (empty($values['optional_fields'][$field])) { + return []; + } + + $language_values = array_column($values['override_languages'][$field_options], 'target'); + $language_values = array_unique($language_values); + + if ($language_values === _oe_contact_forms_contact_language_values()) { + return []; + } + return $language_values; +} + /** * Validate topics email addresses if set. * @@ -505,6 +601,40 @@ function oe_contact_forms_form_contact_message_form_alter(&$form, FormStateInter $form[$field]['#access'] = FALSE; } } + else { + // Set proper order and options for contact language fields. + $optional_selected = $contact_form->getThirdPartySetting('oe_contact_forms', 'optional_fields', []); + $override_languages = $contact_form->getThirdPartySetting('oe_contact_forms', 'override_languages', []); + foreach (['oe_preferred_language', 'oe_alternative_language'] as $field_name) { + if (in_array($field_name, $optional_selected)) { + $language_options = $override_languages[$field_name . '_options'] ?: _oe_contact_forms_contact_language_values(); + _oe_contact_forms_set_language_options($form, $field_name, $language_options); + } + } + } +} + +/** + * Sets options in language select box according to configuration. + * + * @param array $form + * Contact message form. + * @param string $field + * Field name. + * @param array $settings + * Configuration. + */ +function _oe_contact_forms_set_language_options(array &$form, string $field, array $settings): void { + $options = array_splice($form[$field]['widget']['#options'], 0, 1); + foreach ($settings as $id) { + $options[$id] = $form[$field]['widget']['#options'][$id]; + } + if (!empty($form[$field]['widget']['#default_value'][0]) && !isset($options[$form[$field]['widget']['#default_value'][0]])) { + // Need to show the value on the message edit form even if such option is + // removed from configuration. + $options[$form[$field]['widget']['#default_value'][0]] = $form[$field]['widget']['#options'][$form[$field]['widget']['#default_value'][0]]; + } + $form[$field]['widget']['#options'] = $options; } /** @@ -595,7 +725,7 @@ function oe_contact_forms_create_language_base_field(MarkupInterface $title): Ba ->setDisplayOptions('form', [ 'type' => 'skos_concept_entity_reference_options_select', 'settings' => [ - 'sort' => 'label', + 'sort' => 'id', ], ]) ->setDisplayOptions('view', [ @@ -700,14 +830,14 @@ function oe_contact_forms_entity_form_display_alter(EntityFormDisplayInterface $ 'weight' => 6, 'type' => 'skos_concept_entity_reference_options_select', 'settings' => [ - 'sort' => 'label', + 'sort' => 'id', ], ]) ->setComponent('oe_alternative_language', [ 'weight' => 7, 'type' => 'skos_concept_entity_reference_options_select', 'settings' => [ - 'sort' => 'label', + 'sort' => 'id', ], ]) ->setComponent('oe_telephone', [ @@ -780,3 +910,35 @@ function oe_contact_forms_contact_message_create_access(AccountInterface $accoun return AccessResult::forbidden()->cachePerPermissions(); } + +/** + * List of languages in contact language options fields by default. + */ +function _oe_contact_forms_contact_language_values(): array { + return [ + 'http://publications.europa.eu/resource/authority/language/BUL', + 'http://publications.europa.eu/resource/authority/language/SPA', + 'http://publications.europa.eu/resource/authority/language/CES', + 'http://publications.europa.eu/resource/authority/language/DAN', + 'http://publications.europa.eu/resource/authority/language/DEU', + 'http://publications.europa.eu/resource/authority/language/EST', + 'http://publications.europa.eu/resource/authority/language/ELL', + 'http://publications.europa.eu/resource/authority/language/ENG', + 'http://publications.europa.eu/resource/authority/language/FRA', + 'http://publications.europa.eu/resource/authority/language/GLE', + 'http://publications.europa.eu/resource/authority/language/HRV', + 'http://publications.europa.eu/resource/authority/language/ITA', + 'http://publications.europa.eu/resource/authority/language/LAV', + 'http://publications.europa.eu/resource/authority/language/LIT', + 'http://publications.europa.eu/resource/authority/language/HUN', + 'http://publications.europa.eu/resource/authority/language/MLT', + 'http://publications.europa.eu/resource/authority/language/NLD', + 'http://publications.europa.eu/resource/authority/language/POL', + 'http://publications.europa.eu/resource/authority/language/POR', + 'http://publications.europa.eu/resource/authority/language/RON', + 'http://publications.europa.eu/resource/authority/language/SLK', + 'http://publications.europa.eu/resource/authority/language/SLV', + 'http://publications.europa.eu/resource/authority/language/FIN', + 'http://publications.europa.eu/resource/authority/language/SWE', + ]; +} diff --git a/oe_contact_forms.post_update.php b/oe_contact_forms.post_update.php index 0e65f947..392a85e4 100644 --- a/oe_contact_forms.post_update.php +++ b/oe_contact_forms.post_update.php @@ -21,6 +21,10 @@ function oe_contact_forms_post_update_00001(): void { * Add "Preferred contact language", "Alternative contact language" base fields. */ function oe_contact_forms_post_update_00002(): void { + // Enable new dependency. + \Drupal::service('module_installer')->install(['multivalue_form_element']); + + // Add language skos vocabulary. $graphs = [ 'language' => 'http://publications.europa.eu/resource/authority/language', ]; @@ -42,7 +46,7 @@ function oe_contact_forms_post_update_00002(): void { ->setDisplayOptions('form', [ 'type' => 'skos_concept_entity_reference_options_select', 'settings' => [ - 'sort' => 'label', + 'sort' => 'id', ], ]) ->setDisplayOptions('view', [ diff --git a/tests/src/FunctionalJavascript/CorporateContactFormTest.php b/tests/src/FunctionalJavascript/CorporateContactFormTest.php index 0e2a9cac..9f7d5e8e 100644 --- a/tests/src/FunctionalJavascript/CorporateContactFormTest.php +++ b/tests/src/FunctionalJavascript/CorporateContactFormTest.php @@ -63,6 +63,7 @@ protected function setUp(): void { /** @var \Drupal\user\UserInterface $test_user */ $test_user = $this->drupalCreateUser([ 'administer contact forms', + 'view published skos concept entities', ]); $this->drupalLogin($test_user); } @@ -70,7 +71,7 @@ protected function setUp(): void { /** * Test export of a corporate contact form. */ - public function testCorporateContactFormExport(): void { + public function testCorporateContactfFormExport(): void { $this->drupalLogout(); /** @var \Behat\Mink\WebAssert $assert */ @@ -179,21 +180,35 @@ public function testCorporateContactForm(): void { // Assert fields are now visible. $this->assertFieldsVisible(TRUE); - // Assert alternative contact language is active only when preferred - // contact language field is active. + // Assert alternative contact language and options are active only when + // preferred contact language field is active. $preferred_language_element = $page->findField('corporate_fields[optional_fields][oe_preferred_language]'); $alternative_language_element = $page->findField('corporate_fields[optional_fields][oe_alternative_language]'); + $override_languages_element = $page->find('css', '[data-drupal-selector="edit-corporate-fields-override-languages"]'); $this->assertFalse($preferred_language_element->isChecked()); $this->assertFalse($alternative_language_element->isChecked()); $this->assertEquals('disabled', $alternative_language_element->getAttribute('disabled')); + $this->assertFalse($override_languages_element->isVisible()); + $preferred_language_element->click(); $this->assertEmpty($alternative_language_element->getAttribute('disabled')); + $this->assertTrue($override_languages_element->isVisible()); + $override_languages_element->click(); + $this->assertLanguageOptions('oe_preferred_language_options'); + $alternative_language_options_element = $page->find('css', '[data-drupal-selector="edit-corporate-fields-override-languages-oe-alternative-language-options"]'); + $this->assertTrue($alternative_language_options_element->hasClass('form-disabled')); + $alternative_language_element->click(); $this->assertTrue($preferred_language_element->isChecked()); $this->assertTrue($alternative_language_element->isChecked()); + $this->assertFalse($alternative_language_options_element->hasClass('form-disabled')); + $this->assertLanguageOptions('oe_alternative_language_options'); + $preferred_language_element->click(); $this->assertFalse($alternative_language_element->isChecked()); $this->assertEquals('disabled', $alternative_language_element->getAttribute('disabled')); + $this->assertTrue($alternative_language_options_element->hasClass('form-disabled')); + $this->assertFalse($override_languages_element->isVisible()); // Assert expose_as_block is checked by default. $element = $page->findField('corporate_fields[expose_as_block]'); @@ -222,8 +237,8 @@ public function testCorporateContactForm(): void { $this->assertFalse($disabled_message->isVisible()); // Make sure the saved values are the ones expected. - $this->checkCorporateFieldsOnPage(); - $this->checkCorporateFieldsInStorage(); + $this->checkCorporateFieldsOnPage(1, TRUE); + $this->checkCorporateFieldsInStorage(1, TRUE); // Add more topic values, retest ajax. $this->assertTopicAjax(1); @@ -236,8 +251,8 @@ public function testCorporateContactForm(): void { $this->drupalGet('admin/structure/contact/manage/oe_corporate_form'); // Make sure the saved values are the ones expected. - $this->checkCorporateFieldsOnPage(2); - $this->checkCorporateFieldsInStorage(2); + $this->checkCorporateFieldsOnPage(2, TRUE); + $this->checkCorporateFieldsInStorage(2, TRUE); // Assert internal links for privacy policy. $field_name = 'corporate_fields[privacy_policy]'; @@ -517,6 +532,54 @@ protected function assertTopicAjax($delta = 0): void { $this->assertNotEmpty($element); } + /** + * Asserts language options fields. + * + * @param string $field_name + * Field name. + */ + protected function assertLanguageOptions(string $field_name): void { + /** @var \Behat\Mink\WebAssert $assert */ + $assert = $this->assertSession(); + /** @var \Behat\Mink\Element\DocumentElement $page */ + $page = $this->getSession()->getPage(); + $field_name_selector = str_replace('_', '-', $field_name); + + // Assert there are 25 input elements with 24 languages in specific order + // and last empty element. + $input_elements = $page->findAll('css', '[data-drupal-selector="edit-corporate-fields-override-languages-' . $field_name_selector . '"] .form-autocomplete'); + $this->assertEquals(25, count($input_elements)); + $expected_values = [ + 0 => 'Bulgarian (http://publications.europa.eu/resource/authority/language/BUL)', + 1 => 'Spanish (http://publications.europa.eu/resource/authority/language/SPA)', + 2 => 'Czech (http://publications.europa.eu/resource/authority/language/CES)', + 3 => 'Danish (http://publications.europa.eu/resource/authority/language/DAN)', + 21 => 'Slovenian (http://publications.europa.eu/resource/authority/language/SLV)', + 22 => 'Finnish (http://publications.europa.eu/resource/authority/language/FIN)', + 23 => 'Swedish (http://publications.europa.eu/resource/authority/language/SWE)', + ]; + foreach ($expected_values as $key => $value) { + $this->assertEquals($value, $input_elements[$key]->getValue()); + } + for ($i = 4; $i < 20; $i++) { + $this->assertNotEmpty($input_elements[$key]->getValue()); + } + $this->assertEmpty($input_elements[24]->getValue()); + + // Replace last and first elements and add duplicates to ensure order of + // saved elements and removing of duplicates. + $page->fillField("corporate_fields[override_languages][$field_name][0][target]", 'Swedish (http://publications.europa.eu/resource/authority/language/SWE)'); + $page->fillField("corporate_fields[override_languages][$field_name][23][target]", 'Bulgarian (http://publications.europa.eu/resource/authority/language/BUL)'); + $page->fillField("corporate_fields[override_languages][$field_name][24][target]", 'Bulgarian (http://publications.europa.eu/resource/authority/language/BUL)'); + + // Assert "Add more item" button. + $add_another_element = $page->find('css', '[name="corporate_fields_override_languages_' . $field_name . '_add_more"]'); + $add_another_element->click(); + $assert->assertWaitOnAjaxRequest(); + $input_elements = $page->findAll('css', '[data-drupal-selector="edit-corporate-fields-override-languages-' . $field_name_selector . '"] .form-autocomplete'); + $this->assertEquals(26, count($input_elements)); + } + /** * Add test values to corporate fields. */ @@ -544,8 +607,10 @@ protected function fillCorporateFields(): void { * * @param int $max_delta * The max topic group index. + * @param bool $language_options_filled + * Whether language options are filled or not. */ - protected function checkCorporateFieldsOnPage($max_delta = 1): void { + protected function checkCorporateFieldsOnPage($max_delta = 1, $language_options_filled = FALSE): void { /** @var \Behat\Mink\Element\DocumentElement $page */ $page = $this->getSession()->getPage(); @@ -570,6 +635,29 @@ protected function checkCorporateFieldsOnPage($max_delta = 1): void { $this->assertEquals($value, $element->getValue()); } } + + $expected_values = []; + $expected_values[1] = 'Spanish (http://publications.europa.eu/resource/authority/language/SPA)'; + $expected_values[22] = 'Finnish (http://publications.europa.eu/resource/authority/language/FIN)'; + if ($language_options_filled) { + $expected_values[0] = 'Swedish (http://publications.europa.eu/resource/authority/language/SWE)'; + $expected_values[23] = 'Bulgarian (http://publications.europa.eu/resource/authority/language/BUL)'; + } + else { + $expected_values[0] = 'Bulgarian (http://publications.europa.eu/resource/authority/language/BUL)'; + $expected_values[23] = 'Swedish (http://publications.europa.eu/resource/authority/language/SWE)'; + } + $fields = [ + 'oe-preferred-language-options', + 'oe-alternative-language-options', + ]; + foreach ($fields as $field_name) { + $input_elements = $page->findAll('css', '[data-drupal-selector="edit-corporate-fields-override-languages-' . $field_name . '"] .form-autocomplete'); + $this->assertEquals(25, count($input_elements)); + foreach ($expected_values as $key => $value) { + $this->assertEquals($value, $input_elements[$key]->getValue()); + } + } } /** @@ -577,8 +665,10 @@ protected function checkCorporateFieldsOnPage($max_delta = 1): void { * * @param int $max_delta * The max topic group index. + * @param bool $language_options_filled + * Whether language options are filled or not. */ - protected function checkCorporateFieldsInStorage($max_delta = 1): void { + protected function checkCorporateFieldsInStorage($max_delta = 1, bool $language_options_filled = FALSE): void { /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $entity_storage */ $entity_storage = \Drupal::entityTypeManager()->getStorage('contact_form'); /** @var \Drupal\contact\ContactFormInterface $contact_form */ @@ -615,6 +705,34 @@ protected function checkCorporateFieldsInStorage($max_delta = 1): void { $value = $contact_form->getThirdPartySetting('oe_contact_forms', $key); $this->assertEquals($expected, $value); } + + // Check saved language options. + $value = $contact_form->getThirdPartySetting('oe_contact_forms', 'override_languages'); + if ($language_options_filled) { + $expected = [ + 0 => 'http://publications.europa.eu/resource/authority/language/SWE', + 1 => 'http://publications.europa.eu/resource/authority/language/SPA', + 2 => 'http://publications.europa.eu/resource/authority/language/CES', + 22 => 'http://publications.europa.eu/resource/authority/language/FIN', + 23 => 'http://publications.europa.eu/resource/authority/language/BUL', + ]; + $fields = [ + 'oe_preferred_language_options', + 'oe_alternative_language_options', + ]; + foreach ($fields as $field_name) { + foreach ($expected as $key => $expected_value) { + $this->assertEquals($expected_value, $value[$field_name][$key]); + } + $this->assertEquals(24, count($value[$field_name])); + } + } + else { + $this->assertEquals([ + 'oe_preferred_language_options' => [], + 'oe_alternative_language_options' => [], + ], $value); + } } /** @@ -638,6 +756,7 @@ protected function checkCorporateFieldsNotInStorage(): void { 'expose_as_block' => NULL, 'optional_fields' => NULL, 'topics' => NULL, + 'override_languages' => NULL, ]; foreach ($expected_values as $key => $expected) { diff --git a/tests/src/FunctionalJavascript/MessageFormTest.php b/tests/src/FunctionalJavascript/MessageFormTest.php index 599dc05e..16336297 100644 --- a/tests/src/FunctionalJavascript/MessageFormTest.php +++ b/tests/src/FunctionalJavascript/MessageFormTest.php @@ -181,11 +181,11 @@ public function testCorporateForm(): void { $assert->fieldExists('oe_alternative_language'); $assert->fieldExists('oe_telephone[0][value]'); - // Assert contact language fields contains 24 EU languages. + // Assert contact language fields contains 24 EU languages by default. $expected_languages = [ '_none' => '- None -', 'http://publications.europa.eu/resource/authority/language/BUL' => 'Bulgarian', - 'http://publications.europa.eu/resource/authority/language/HRV' => 'Croatian', + 'http://publications.europa.eu/resource/authority/language/SPA' => 'Spanish', 'http://publications.europa.eu/resource/authority/language/CES' => 'Czech', 'http://publications.europa.eu/resource/authority/language/DAN' => 'Danish', ]; @@ -265,9 +265,32 @@ public function testCorporateForm(): void { ]); $node->save(); $contact_form->setThirdPartySetting('oe_contact_forms', 'privacy_policy', 'internal:' . $alias); + $override_languages = [ + 'oe_preferred_language_options' => [ + 'http://publications.europa.eu/resource/authority/language/CES', + 'http://publications.europa.eu/resource/authority/language/DAN', + ], + 'oe_alternative_language_options' => [ + 'http://publications.europa.eu/resource/authority/language/FRA', + ], + ]; + $contact_form->setThirdPartySetting('oe_contact_forms', 'override_languages', $override_languages); $contact_form->save(); $this->drupalGet('contact/' . $contact_form_id); $assert->elementAttributeContains('xpath', "//div[contains(@class, 'form-item-privacy-policy')]//a", 'href', $alias); + $expected_languages = [ + '_none' => '- None -', + 'http://publications.europa.eu/resource/authority/language/CES' => 'Czech', + 'http://publications.europa.eu/resource/authority/language/DAN' => 'Danish', + ]; + $options = $this->getOptions('oe_preferred_language'); + $this->assertEquals($expected_languages, $options); + $expected_languages = [ + '_none' => '- None -', + 'http://publications.europa.eu/resource/authority/language/FRA' => 'French', + ]; + $options = $this->getOptions('oe_alternative_language'); + $this->assertEquals($expected_languages, $options); } /** From 6edb7cb0dcb2b365138c34721fdb903188385873 Mon Sep 17 00:00:00 2001 From: Evgenii Nikitin Date: Mon, 24 Oct 2022 19:20:24 +0200 Subject: [PATCH 07/12] EWPP-2702: Preferred language has to be required if chosen. --- oe_contact_forms.module | 4 ++++ tests/src/FunctionalJavascript/MessageFormTest.php | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/oe_contact_forms.module b/oe_contact_forms.module index cfccf26a..1a072897 100644 --- a/oe_contact_forms.module +++ b/oe_contact_forms.module @@ -611,6 +611,10 @@ function oe_contact_forms_form_contact_message_form_alter(&$form, FormStateInter _oe_contact_forms_set_language_options($form, $field_name, $language_options); } } + if (in_array('oe_preferred_language', $optional_selected)) { + // Preferred language is required if shown. + $form['oe_preferred_language']['widget']['#required'] = TRUE; + } } } diff --git a/tests/src/FunctionalJavascript/MessageFormTest.php b/tests/src/FunctionalJavascript/MessageFormTest.php index 16336297..df77e665 100644 --- a/tests/src/FunctionalJavascript/MessageFormTest.php +++ b/tests/src/FunctionalJavascript/MessageFormTest.php @@ -211,6 +211,11 @@ public function testCorporateForm(): void { $page->findField('privacy_policy')->click(); $page->findButton('Send message')->press(); + // Assert preferred contact language field is required. + $assert->elementTextContains('css', 'div[aria-label="Error message"]', 'Preferred contact language field is required.'); + $page->selectFieldOption('Preferred contact language', 'http://publications.europa.eu/resource/authority/language/CES'); + $page->findButton('Send message')->press(); + // Assert confirmation message. $assert->elementTextContains('css', 'div[aria-label="Status message"]', $topics['0']['topic_name']); @@ -249,6 +254,7 @@ public function testCorporateForm(): void { $page->fillField('message[0][value]', 'Test message'); $page->selectFieldOption('oe_topic', $topics['0']['topic_name']); $page->findField('privacy_policy')->click(); + $page->selectFieldOption('Preferred contact language', 'http://publications.europa.eu/resource/authority/language/CES'); $page->findButton('Send message')->press(); // Assert that the user is being redirected to the path set. From ddf2cfc0a63301954672af3717a7d52f6150bf2f Mon Sep 17 00:00:00 2001 From: Evgenii Nikitin Date: Tue, 25 Oct 2022 18:32:03 +0200 Subject: [PATCH 08/12] EWPP-2702: Hide alternative contact languages if isn't used. --- oe_contact_forms.module | 5 +--- .../CorporateContactFormTest.php | 29 ++++++++++++++++--- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/oe_contact_forms.module b/oe_contact_forms.module index 1a072897..fe352860 100644 --- a/oe_contact_forms.module +++ b/oe_contact_forms.module @@ -248,8 +248,6 @@ function oe_contact_forms_form_contact_form_form_alter(&$form, FormStateInterfac ], 'concept_subset' => 'contact_languages', ], - '#maxlength' => 1024, - '#placeholder' => '', ], '#default_value' => empty($override_languages_settings['oe_preferred_language_options']) ? $default_language_values : array_values($skos_concept_storage->loadMultiple($override_languages_settings['oe_preferred_language_options'])), ]; @@ -266,10 +264,9 @@ function oe_contact_forms_form_contact_form_form_alter(&$form, FormStateInterfac ], 'concept_subset' => 'contact_languages', ], - '#maxlength' => 1024, ], '#states' => [ - 'enabled' => [ + 'visible' => [ ':input[name="corporate_fields[optional_fields][oe_alternative_language]"]' => ['checked' => TRUE], ], ], diff --git a/tests/src/FunctionalJavascript/CorporateContactFormTest.php b/tests/src/FunctionalJavascript/CorporateContactFormTest.php index 9f7d5e8e..2ed7f7ba 100644 --- a/tests/src/FunctionalJavascript/CorporateContactFormTest.php +++ b/tests/src/FunctionalJavascript/CorporateContactFormTest.php @@ -191,23 +191,24 @@ public function testCorporateContactForm(): void { $this->assertFalse($override_languages_element->isVisible()); $preferred_language_element->click(); + $alternative_language_element = $page->findField('corporate_fields[optional_fields][oe_alternative_language]'); + $this->assertNotNull($alternative_language_element); $this->assertEmpty($alternative_language_element->getAttribute('disabled')); $this->assertTrue($override_languages_element->isVisible()); $override_languages_element->click(); $this->assertLanguageOptions('oe_preferred_language_options'); - $alternative_language_options_element = $page->find('css', '[data-drupal-selector="edit-corporate-fields-override-languages-oe-alternative-language-options"]'); - $this->assertTrue($alternative_language_options_element->hasClass('form-disabled')); + $this->assertAlternativeContactOptionsVisible(FALSE); $alternative_language_element->click(); $this->assertTrue($preferred_language_element->isChecked()); $this->assertTrue($alternative_language_element->isChecked()); - $this->assertFalse($alternative_language_options_element->hasClass('form-disabled')); + $this->assertAlternativeContactOptionsVisible(TRUE); $this->assertLanguageOptions('oe_alternative_language_options'); $preferred_language_element->click(); $this->assertFalse($alternative_language_element->isChecked()); $this->assertEquals('disabled', $alternative_language_element->getAttribute('disabled')); - $this->assertTrue($alternative_language_options_element->hasClass('form-disabled')); + $this->assertAlternativeContactOptionsVisible(FALSE); $this->assertFalse($override_languages_element->isVisible()); // Assert expose_as_block is checked by default. @@ -310,6 +311,10 @@ public function testCorporateContactForm(): void { $this->assertTrue($preferred_language_element->isChecked()); $this->assertFalse($alternative_language_element->isChecked()); $this->assertEmpty($alternative_language_element->getAttribute('disabled')); + $override_languages_element = $page->find('css', '[data-drupal-selector="edit-corporate-fields-override-languages"]'); + $this->assertTrue($override_languages_element->isVisible()); + $override_languages_element->click(); + $this->assertAlternativeContactOptionsVisible(FALSE); } /** @@ -765,4 +770,20 @@ protected function checkCorporateFieldsNotInStorage(): void { } } + /** + * Asserts visibility of "Alternative contact language options" field. + * + * @param bool $visible + * Whether field is visible or not. + */ + protected function assertAlternativeContactOptionsVisible(bool $visible): void { + $element = $this->getSession()->getPage()->find('css', '[data-drupal-selector="edit-corporate-fields-override-languages-oe-alternative-language-options"]'); + if ($visible) { + $this->assertStringNotContainsString('display: none', $element->getAttribute('style')); + } + else { + $this->assertStringContainsString('display: none', $element->getAttribute('style')); + } + } + } From eb08388e431798002331c38dd3cec17485900b86 Mon Sep 17 00:00:00 2001 From: Evgenii Nikitin Date: Wed, 26 Oct 2022 15:51:36 +0200 Subject: [PATCH 09/12] EWPP-2702: Removed typo. --- tests/src/FunctionalJavascript/CorporateContactFormTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/FunctionalJavascript/CorporateContactFormTest.php b/tests/src/FunctionalJavascript/CorporateContactFormTest.php index 2ed7f7ba..a2a499ab 100644 --- a/tests/src/FunctionalJavascript/CorporateContactFormTest.php +++ b/tests/src/FunctionalJavascript/CorporateContactFormTest.php @@ -71,7 +71,7 @@ protected function setUp(): void { /** * Test export of a corporate contact form. */ - public function testCorporateContactfFormExport(): void { + public function testCorporateContactFormExport(): void { $this->drupalLogout(); /** @var \Behat\Mink\WebAssert $assert */ From fde2ceabb0c2edbbe2796818019ed1c8e928736b Mon Sep 17 00:00:00 2001 From: Evgenii Nikitin Date: Wed, 26 Oct 2022 12:46:31 +0200 Subject: [PATCH 10/12] EWPP-2703: Refactoring of ContactMessageForm to make it easier to extend. --- src/Form/ContactMessageForm.php | 54 ++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/src/Form/ContactMessageForm.php b/src/Form/ContactMessageForm.php index e631b975..c3d1f94c 100644 --- a/src/Form/ContactMessageForm.php +++ b/src/Form/ContactMessageForm.php @@ -4,7 +4,9 @@ namespace Drupal\oe_contact_forms\Form; +use Drupal\contact\ContactFormInterface; use Drupal\contact\MessageForm; +use Drupal\contact\MessageInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Drupal\Core\Link; @@ -79,6 +81,25 @@ public function save(array $form, FormStateInterface $form_state): void { /** @var \Drupal\contact\ContactFormInterface $contact_form */ $contact_form = $message->getContactForm(); + $this->setReply($message, $contact_form); + $this->setRecepients($contact_form, $form_state); + + // Send the emails. + parent::save($form, $form_state); + + $this->addStatusMessage($message); + $this->setRedirectUrl($contact_form, $form_state); + } + + /** + * Sets an auto-reply message to send to the message author. + * + * @param \Drupal\contact\MessageInterface $message + * Contact message instance. + * @param \Drupal\contact\ContactFormInterface $contact_form + * Contact form instance. + */ + protected function setReply(MessageInterface $message, ContactFormInterface $contact_form): void { // If the form is configured to include all the fields in the auto-reply, // set the values after the auto-reply body, // so they get included in the email as well. @@ -92,13 +113,23 @@ public function save(array $form, FormStateInterface $form_state): void { $reply .= "\n" . \Drupal::service('renderer')->renderPlain($mail_view); $contact_form->setReply($reply); } + } + /** + * Sets list of recipient email addresses. + * + * @param \Drupal\contact\ContactFormInterface $contact_form + * Contact form instance. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Form state. + */ + protected function setRecepients(ContactFormInterface $contact_form, FormStateInterface $form_state): void { // Set the email recipient(s) based on the selected topic. $recipients = $contact_form->getRecipients(); $topics = $contact_form->getThirdPartySetting('oe_contact_forms', 'topics', []); $selected_topic = $form_state->getValue('oe_topic')['0']['value']; - foreach ($topics as $index => $topic) { + foreach ($topics as $topic) { if ($topic['topic_name'] === $selected_topic) { $topic_recipients = explode(',', $topic['topic_email_address']); $recipients = array_merge($recipients, $topic_recipients); @@ -106,15 +137,30 @@ public function save(array $form, FormStateInterface $form_state): void { break; } } + } - // Send the emails. - parent::save($form, $form_state); - + /** + * Adds status message. + * + * @param \Drupal\contact\MessageInterface $message + * Contact message instance. + */ + protected function addStatusMessage(MessageInterface $message): void { // Apart from the confirmation message also include the following. // The values of the submitted fields. $full_view = $this->entityTypeManager->getViewBuilder('contact_message')->view($message, 'full'); $this->messenger()->addMessage($full_view); + } + /** + * Sets redirect URL. + * + * @param \Drupal\contact\ContactFormInterface $contact_form + * Contact form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Form state. + */ + protected function setRedirectUrl(ContactFormInterface $contact_form, FormStateInterface $form_state): void { // Redirect back to same page if redirect value is not set. if (!$contact_form->getRedirectPath()) { $form_state->setRedirectUrl(Url::fromRoute('')); From 520ec01d1068c7aa0fca64401bcfc95d5821ccff Mon Sep 17 00:00:00 2001 From: Evgenii Nikitin Date: Wed, 26 Oct 2022 19:35:36 +0200 Subject: [PATCH 11/12] EWPP-2703: Add translations. --- oe_contact_forms.info.yml | 4 ++++ oe_contact_forms.module | 7 +++++++ translations/oe_contact_forms-bg.po | 8 ++++++++ translations/oe_contact_forms-cs.po | 8 ++++++++ translations/oe_contact_forms-da.po | 8 ++++++++ translations/oe_contact_forms-de.po | 8 ++++++++ translations/oe_contact_forms-el.po | 8 ++++++++ translations/oe_contact_forms-es.po | 8 ++++++++ translations/oe_contact_forms-et.po | 8 ++++++++ translations/oe_contact_forms-fi.po | 8 ++++++++ translations/oe_contact_forms-fr.po | 8 ++++++++ translations/oe_contact_forms-ga.po | 8 ++++++++ translations/oe_contact_forms-hr.po | 8 ++++++++ translations/oe_contact_forms-hu.po | 8 ++++++++ translations/oe_contact_forms-it.po | 8 ++++++++ translations/oe_contact_forms-lt.po | 8 ++++++++ translations/oe_contact_forms-lv.po | 8 ++++++++ translations/oe_contact_forms-mt.po | 8 ++++++++ translations/oe_contact_forms-nl.po | 8 ++++++++ translations/oe_contact_forms-pl.po | 8 ++++++++ translations/oe_contact_forms-pt-pt.po | 8 ++++++++ translations/oe_contact_forms-ro.po | 8 ++++++++ translations/oe_contact_forms-ru.po | 8 ++++++++ translations/oe_contact_forms-sk.po | 8 ++++++++ translations/oe_contact_forms-sl.po | 8 ++++++++ translations/oe_contact_forms-sv.po | 8 ++++++++ translations/oe_contact_forms-uk.po | 8 ++++++++ 27 files changed, 211 insertions(+) create mode 100644 translations/oe_contact_forms-bg.po create mode 100644 translations/oe_contact_forms-cs.po create mode 100644 translations/oe_contact_forms-da.po create mode 100644 translations/oe_contact_forms-de.po create mode 100644 translations/oe_contact_forms-el.po create mode 100644 translations/oe_contact_forms-es.po create mode 100644 translations/oe_contact_forms-et.po create mode 100644 translations/oe_contact_forms-fi.po create mode 100644 translations/oe_contact_forms-fr.po create mode 100644 translations/oe_contact_forms-ga.po create mode 100644 translations/oe_contact_forms-hr.po create mode 100644 translations/oe_contact_forms-hu.po create mode 100644 translations/oe_contact_forms-it.po create mode 100644 translations/oe_contact_forms-lt.po create mode 100644 translations/oe_contact_forms-lv.po create mode 100644 translations/oe_contact_forms-mt.po create mode 100644 translations/oe_contact_forms-nl.po create mode 100644 translations/oe_contact_forms-pl.po create mode 100644 translations/oe_contact_forms-pt-pt.po create mode 100644 translations/oe_contact_forms-ro.po create mode 100644 translations/oe_contact_forms-ru.po create mode 100644 translations/oe_contact_forms-sk.po create mode 100644 translations/oe_contact_forms-sl.po create mode 100644 translations/oe_contact_forms-sv.po create mode 100644 translations/oe_contact_forms-uk.po diff --git a/oe_contact_forms.info.yml b/oe_contact_forms.info.yml index 77148b98..52c6228b 100644 --- a/oe_contact_forms.info.yml +++ b/oe_contact_forms.info.yml @@ -14,3 +14,7 @@ dependencies: - rdf_skos:rdf_skos - oe_corporate_countries:oe_corporate_countries - multivalue_form_element:multivalue_form_element + +'interface translation project': oe_contact_forms +# The path to the actual translations is defined in +# oe_contact_forms_translation_projects_alter(). diff --git a/oe_contact_forms.module b/oe_contact_forms.module index fe352860..ebfe3cce 100644 --- a/oe_contact_forms.module +++ b/oe_contact_forms.module @@ -943,3 +943,10 @@ function _oe_contact_forms_contact_language_values(): array { 'http://publications.europa.eu/resource/authority/language/SWE', ]; } + +/** + * Implements hook_locale_translation_projects_alter(). + */ +function oe_contact_forms_locale_translation_projects_alter(&$projects) { + $projects['oe_contact_forms']['info']['interface translation server pattern'] = \Drupal::service('extension.path.resolver')->getPath('module', 'oe_contact_forms') . '/translations/%project-%language.po'; +} diff --git a/translations/oe_contact_forms-bg.po b/translations/oe_contact_forms-bg.po new file mode 100644 index 00000000..7c14335f --- /dev/null +++ b/translations/oe_contact_forms-bg.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Предпочитан език за връзка" + +msgid "Alternative contact language" +msgstr "Алтернативен език за връзка" diff --git a/translations/oe_contact_forms-cs.po b/translations/oe_contact_forms-cs.po new file mode 100644 index 00000000..488d4e7f --- /dev/null +++ b/translations/oe_contact_forms-cs.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Preferovaný jazyk korespondence" + +msgid "Alternative contact language" +msgstr "Alternativní jazyk korespondence" diff --git a/translations/oe_contact_forms-da.po b/translations/oe_contact_forms-da.po new file mode 100644 index 00000000..14570151 --- /dev/null +++ b/translations/oe_contact_forms-da.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Foretrukket kontaktsprog" + +msgid "Alternative contact language" +msgstr "Alternativt kontaktsprog" diff --git a/translations/oe_contact_forms-de.po b/translations/oe_contact_forms-de.po new file mode 100644 index 00000000..cb68e0ac --- /dev/null +++ b/translations/oe_contact_forms-de.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Bevorzugte Sprache" + +msgid "Alternative contact language" +msgstr "Alternative Sprache" diff --git a/translations/oe_contact_forms-el.po b/translations/oe_contact_forms-el.po new file mode 100644 index 00000000..f1933cb8 --- /dev/null +++ b/translations/oe_contact_forms-el.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Προτιμώμενη γλώσσα επικοινωνίας" + +msgid "Alternative contact language" +msgstr "Δέυτερη προτιμώμενη γλώσσα επικοινωνίας" diff --git a/translations/oe_contact_forms-es.po b/translations/oe_contact_forms-es.po new file mode 100644 index 00000000..20c02b2c --- /dev/null +++ b/translations/oe_contact_forms-es.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Idioma de contacto preferido" + +msgid "Alternative contact language" +msgstr "Idioma de contacto alternativo" diff --git a/translations/oe_contact_forms-et.po b/translations/oe_contact_forms-et.po new file mode 100644 index 00000000..415f3885 --- /dev/null +++ b/translations/oe_contact_forms-et.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Eelistatud keel ühenduse võtmiseks" + +msgid "Alternative contact language" +msgstr "Alternatiivne keel ühenduse võtmiseks" diff --git a/translations/oe_contact_forms-fi.po b/translations/oe_contact_forms-fi.po new file mode 100644 index 00000000..52a41ac0 --- /dev/null +++ b/translations/oe_contact_forms-fi.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Ensisijainen vastauksen kieli" + +msgid "Alternative contact language" +msgstr "Vaihtoehtoinen vastauksen kieli" diff --git a/translations/oe_contact_forms-fr.po b/translations/oe_contact_forms-fr.po new file mode 100644 index 00000000..cbe8a636 --- /dev/null +++ b/translations/oe_contact_forms-fr.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Langue de préférence" + +msgid "Alternative contact language" +msgstr "Autre langue de contact possible" diff --git a/translations/oe_contact_forms-ga.po b/translations/oe_contact_forms-ga.po new file mode 100644 index 00000000..d2841ea8 --- /dev/null +++ b/translations/oe_contact_forms-ga.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Autre langue de contact possible" + +msgid "Alternative contact language" +msgstr "Teanga chumarsáide eile" diff --git a/translations/oe_contact_forms-hr.po b/translations/oe_contact_forms-hr.po new file mode 100644 index 00000000..0022a1ba --- /dev/null +++ b/translations/oe_contact_forms-hr.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Primarni izbor jezika odgovora" + +msgid "Alternative contact language" +msgstr "Sporedni izbor jezika odgovora" diff --git a/translations/oe_contact_forms-hu.po b/translations/oe_contact_forms-hu.po new file mode 100644 index 00000000..f3eda06b --- /dev/null +++ b/translations/oe_contact_forms-hu.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "A kapcsolattartás választott nyelve (első)" + +msgid "Alternative contact language" +msgstr "A kapcsolattartás választott nyelve (második)" diff --git a/translations/oe_contact_forms-it.po b/translations/oe_contact_forms-it.po new file mode 100644 index 00000000..ffc70afd --- /dev/null +++ b/translations/oe_contact_forms-it.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Lingua preferita" + +msgid "Alternative contact language" +msgstr "Lingua alternativa" diff --git a/translations/oe_contact_forms-lt.po b/translations/oe_contact_forms-lt.po new file mode 100644 index 00000000..a332dacd --- /dev/null +++ b/translations/oe_contact_forms-lt.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Pageidaujama korespondencijos kalba" + +msgid "Alternative contact language" +msgstr "Alternatyvi korespondencijos kalba" diff --git a/translations/oe_contact_forms-lv.po b/translations/oe_contact_forms-lv.po new file mode 100644 index 00000000..468650c4 --- /dev/null +++ b/translations/oe_contact_forms-lv.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Vēlamā saziņas valoda" + +msgid "Alternative contact language" +msgstr "Alternatīva saziņas valoda" diff --git a/translations/oe_contact_forms-mt.po b/translations/oe_contact_forms-mt.po new file mode 100644 index 00000000..0b43f564 --- /dev/null +++ b/translations/oe_contact_forms-mt.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Lingwa ta’ kuntatt li tippreferi" + +msgid "Alternative contact language" +msgstr "Lingwa ta’ kuntatt alternattiva" diff --git a/translations/oe_contact_forms-nl.po b/translations/oe_contact_forms-nl.po new file mode 100644 index 00000000..c03cd134 --- /dev/null +++ b/translations/oe_contact_forms-nl.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Correspondentietaal" + +msgid "Alternative contact language" +msgstr "Alternatieve correspondentietaal" diff --git a/translations/oe_contact_forms-pl.po b/translations/oe_contact_forms-pl.po new file mode 100644 index 00000000..92bdc375 --- /dev/null +++ b/translations/oe_contact_forms-pl.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Preferowany język" + +msgid "Alternative contact language" +msgstr "Drugi preferowany język" diff --git a/translations/oe_contact_forms-pt-pt.po b/translations/oe_contact_forms-pt-pt.po new file mode 100644 index 00000000..307fab5f --- /dev/null +++ b/translations/oe_contact_forms-pt-pt.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Língua de contacto preferida" + +msgid "Alternative contact language" +msgstr "Outra língua de contacto" diff --git a/translations/oe_contact_forms-ro.po b/translations/oe_contact_forms-ro.po new file mode 100644 index 00000000..3c8c2c23 --- /dev/null +++ b/translations/oe_contact_forms-ro.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Limba de contact preferată" + +msgid "Alternative contact language" +msgstr "Limbi de contact alternative" diff --git a/translations/oe_contact_forms-ru.po b/translations/oe_contact_forms-ru.po new file mode 100644 index 00000000..b5f1b171 --- /dev/null +++ b/translations/oe_contact_forms-ru.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Предпочтительный язык" + +msgid "Alternative contact language" +msgstr "Альтернативный язык" diff --git a/translations/oe_contact_forms-sk.po b/translations/oe_contact_forms-sk.po new file mode 100644 index 00000000..aec377e3 --- /dev/null +++ b/translations/oe_contact_forms-sk.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Prvý (uprednostňovaný) jazyk" + +msgid "Alternative contact language" +msgstr "Druhý (alternatívny) jazyk" diff --git a/translations/oe_contact_forms-sl.po b/translations/oe_contact_forms-sl.po new file mode 100644 index 00000000..5ba83d0b --- /dev/null +++ b/translations/oe_contact_forms-sl.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Želen kontaktni jezik" + +msgid "Alternative contact language" +msgstr "Drugi kontaktni jezik" diff --git a/translations/oe_contact_forms-sv.po b/translations/oe_contact_forms-sv.po new file mode 100644 index 00000000..35eb7ab9 --- /dev/null +++ b/translations/oe_contact_forms-sv.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Vilket språk vill du helst få svar på?" + +msgid "Alternative contact language" +msgstr "Vilket annat språk skulle gå bra?" diff --git a/translations/oe_contact_forms-uk.po b/translations/oe_contact_forms-uk.po new file mode 100644 index 00000000..79ad4872 --- /dev/null +++ b/translations/oe_contact_forms-uk.po @@ -0,0 +1,8 @@ +msgid "" +msgstr "" + +msgid "Preferred contact language" +msgstr "Бажана мова для спілкування" + +msgid "Alternative contact language" +msgstr "Альтернативна мова для спілкування" From 4e19ab2daebe6abe83dc7be3f7f70b6ee8ac6197 Mon Sep 17 00:00:00 2001 From: 22Alexandra Date: Thu, 3 Nov 2022 11:18:23 +0200 Subject: [PATCH 12/12] EWPP-2700: Add multivalue form element patch. --- composer.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3293b0ff..1cfc35db 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "drupal/contact_storage_export": "^1.14", "drupal/core": "^9.3", "drupal/csv_serialization": "^2.0", - "drupal/multivalue_form_element": "^1.0@beta", + "drupal/multivalue_form_element": "^1.0.0-beta4", "drupal/token": "^1.8", "openeuropa/oe_corporate_countries": "^2.0", "openeuropa/rdf_skos": "^1.0" @@ -70,6 +70,9 @@ "drupal/contact_storage_export": { "https://www.drupal.org/project/contact_storage_export/issues/2996037": "https://www.drupal.org/files/issues/2021-10-22/contact_storage_export-breaks-on-high-load-balancer-2996037-17.patch", "https://www.drupal.org/project/contact_storage_export/issues/3049010": "https://www.drupal.org/files/issues/2020-04-14/php73-3049010-15.patch" + }, + "drupal/multivalue_form_element": { + "latest-master": "https://github.com/openeuropa/multivalue_form_element/compare/1.0.0-beta4...1.0.x.diff" } }, "drupal-scaffold": {