diff --git a/modules/oe_whitelabel_helper/config/schema/oe_whitelabel_helper.schema.yml b/modules/oe_whitelabel_helper/config/schema/oe_whitelabel_helper.schema.yml index d2692f0a4..837fd610a 100644 --- a/modules/oe_whitelabel_helper/config/schema/oe_whitelabel_helper.schema.yml +++ b/modules/oe_whitelabel_helper/config/schema/oe_whitelabel_helper.schema.yml @@ -5,6 +5,11 @@ field.formatter.settings.oe_whitelabel_helper_address_inline: delimiter: type: string label: 'Delimiter for address items.' + properties: + type: sequence + sequence: + type: boolean + label: 'Properties' condition.plugin.oe_whitelabel_helper_current_component_library: type: condition.plugin mapping: diff --git a/modules/oe_whitelabel_helper/src/Plugin/Field/FieldFormatter/AddressInlineFormatter.php b/modules/oe_whitelabel_helper/src/Plugin/Field/FieldFormatter/AddressInlineFormatter.php index a080a0eac..e004fcd6a 100644 --- a/modules/oe_whitelabel_helper/src/Plugin/Field/FieldFormatter/AddressInlineFormatter.php +++ b/modules/oe_whitelabel_helper/src/Plugin/Field/FieldFormatter/AddressInlineFormatter.php @@ -6,6 +6,7 @@ use CommerceGuys\Addressing\Locale; use Drupal\address\AddressInterface; +use Drupal\address\LabelHelper; use Drupal\address\Plugin\Field\FieldFormatter\AddressDefaultFormatter; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; @@ -32,6 +33,7 @@ class AddressInlineFormatter extends AddressDefaultFormatter { public static function defaultSettings() { return [ 'delimiter' => ', ', + 'properties' => [], ]; } @@ -39,7 +41,6 @@ public static function defaultSettings() { * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { - $form['delimiter'] = [ '#type' => 'textfield', '#title' => $this->t('Delimiter'), @@ -48,6 +49,14 @@ public function settingsForm(array $form, FormStateInterface $form_state) { '#required' => TRUE, ]; + $form['properties'] = [ + '#type' => 'checkboxes', + '#title' => $this->t('Properties'), + '#default_value' => $this->getActiveProperties(), + '#description' => $this->t('Which properties should be displayed. Leave empty for all.'), + '#options' => $this->getPropertiesDisplayOptions(), + ]; + return $form; } @@ -59,6 +68,9 @@ public function settingsSummary() { $this->t('Delimiter: @delimiter', [ '@delimiter' => $this->getSetting('delimiter'), ]), + $this->t('Properties: @properties', [ + '@properties' => implode(', ', $this->getActiveProperties()), + ]), ]; } @@ -102,6 +114,7 @@ protected function viewElement(AddressInterface $address, $langcode) { else { $format_string = $address_format->getFormat() . "\n" . '%country'; } + /* * Remove extra characters from address format since address fields are * optional. @@ -137,11 +150,24 @@ protected function viewElement(AddressInterface $address, $langcode) { * The exploded lines. */ protected function extractAddressItems(string $string, array $replacements): array { + $properties = array_map(function (string $property): string { + return '%' . $property; + }, $this->getActiveProperties()); + + if (!empty($properties)) { + $properties_keys = array_flip($properties); + + foreach (array_diff_key($replacements, $properties_keys) as $key => $value) { + $replacements[$key] = ''; + } + } + // Make sure the replacements don't have any unneeded newlines. $replacements = array_map('trim', $replacements); $string = strtr($string, $replacements); // Remove noise caused by empty placeholders. $lines = explode("\n", $string); + foreach ($lines as $index => $line) { // Remove leading punctuation, excess whitespace. $line = trim(preg_replace('/^[-,]+/', '', $line, 1)); @@ -154,4 +180,26 @@ protected function extractAddressItems(string $string, array $replacements): arr return $lines; } + /** + * Provides the options for the properties display setting. + * + * @return array + * The properties display options. + */ + protected function getPropertiesDisplayOptions(): array { + return [ + 'country' => $this->t('Country'), + ] + LabelHelper::getGenericFieldLabels(); + } + + /** + * Gets the active properties. + * + * @return array + * The properties. + */ + protected function getActiveProperties(): array { + return array_keys(array_filter($this->getSetting('properties'))); + } + } diff --git a/modules/oe_whitelabel_helper/tests/src/Kernel/Plugin/Field/FieldFormatter/AddressInlineFormatterTest.php b/modules/oe_whitelabel_helper/tests/src/Kernel/Plugin/Field/FieldFormatter/AddressInlineFormatterTest.php index ff3038e09..277ff223f 100644 --- a/modules/oe_whitelabel_helper/tests/src/Kernel/Plugin/Field/FieldFormatter/AddressInlineFormatterTest.php +++ b/modules/oe_whitelabel_helper/tests/src/Kernel/Plugin/Field/FieldFormatter/AddressInlineFormatterTest.php @@ -34,6 +34,10 @@ protected function setUp(): void { public function testInlineFormatterAddress(): void { $entity = EntityTest::create([]); foreach ($this->addressFieldTestData() as $data) { + if (!empty($data['settings'])) { + $this->alterDisplaySettings($data['settings']); + } + $cloned_entity = clone $entity; $cloned_entity->{$this->fieldName} = $data['address']; $this->renderEntityFields($cloned_entity, $this->display); @@ -48,7 +52,7 @@ public function testInlineFormatterAddress(): void { * @return array[] * An array of test data arrays with expected result. */ - public function addressFieldTestData(): array { + protected function addressFieldTestData(): array { return [ 'Brussels Belgium' => [ 'address' => [ @@ -115,7 +119,51 @@ public function addressFieldTestData(): array { ], 'expected' => 'Dhaka, 1100, Bangladesh', ], + 'Brussels Belgium with $ delimiter' => [ + 'address' => [ + 'country_code' => 'BE', + 'locality' => 'Brussels', + 'postal_code' => '1000', + 'address_line1' => 'Rue de la Loi', + 'address_line2' => '56', + ], + 'expected' => 'Rue de la Loi$56$1000 Brussels$Belgium', + 'settings' => [ + 'delimiter' => '$', + ], + ], + 'Brussels Belgium only country and locality' => [ + 'address' => [ + 'country_code' => 'BE', + 'locality' => 'Brussels', + 'postal_code' => '1000', + 'address_line1' => 'Rue de la Loi', + 'address_line2' => '56', + ], + 'expected' => 'Brussels, Belgium', + 'settings' => [ + 'delimiter' => ', ', + 'properties' => [ + 'country' => 'country', + 'locality' => 'locality', + ], + ], + ], ]; } + /** + * Alters the display's settings. + * + * @param array $settings + * The settings. + */ + protected function alterDisplaySettings(array $settings): void { + $this->display->setComponent($this->fieldName, [ + 'type' => 'oe_whitelabel_helper_address_inline', + 'settings' => $settings, + ]); + $this->display->save(); + } + } diff --git a/modules/oe_whitelabel_starter_event/config/install/core.entity_view_display.node.oe_sc_event.teaser.yml b/modules/oe_whitelabel_starter_event/config/install/core.entity_view_display.node.oe_sc_event.teaser.yml index ed5738e06..ec08e6c7b 100755 --- a/modules/oe_whitelabel_starter_event/config/install/core.entity_view_display.node.oe_sc_event.teaser.yml +++ b/modules/oe_whitelabel_starter_event/config/install/core.entity_view_display.node.oe_sc_event.teaser.yml @@ -12,8 +12,8 @@ dependencies: - field.field.node.oe_sc_event.oe_summary - node.type.oe_sc_event module: - - address - daterange_compact + - oe_whitelabel_helper - text - user id: node.oe_sc_event.teaser @@ -30,9 +30,23 @@ content: weight: 4 region: content oe_sc_event_location: - type: address_default + type: oe_whitelabel_helper_address_inline label: hidden - settings: { } + settings: + delimiter: ', ' + properties: + country: true + locality: true + givenName: false + additionalName: false + familyName: false + organization: false + addressLine1: false + addressLine2: false + postalCode: false + sortingCode: false + dependentLocality: false + administrativeArea: false third_party_settings: { } weight: 5 region: content diff --git a/modules/oe_whitelabel_starter_event/config/post_updates/00004_teaser_location/core.entity_view_display.node.oe_sc_event.teaser.yml b/modules/oe_whitelabel_starter_event/config/post_updates/00004_teaser_location/core.entity_view_display.node.oe_sc_event.teaser.yml new file mode 100755 index 000000000..ec08e6c7b --- /dev/null +++ b/modules/oe_whitelabel_starter_event/config/post_updates/00004_teaser_location/core.entity_view_display.node.oe_sc_event.teaser.yml @@ -0,0 +1,71 @@ +langcode: en +status: true +dependencies: + config: + - core.entity_view_mode.node.teaser + - field.field.node.oe_sc_event.body + - field.field.node.oe_sc_event.oe_documents + - field.field.node.oe_sc_event.oe_featured_media + - field.field.node.oe_sc_event.oe_sc_event_dates + - field.field.node.oe_sc_event.oe_sc_event_location + - field.field.node.oe_sc_event.oe_sc_event_registration_url + - field.field.node.oe_sc_event.oe_summary + - node.type.oe_sc_event + module: + - daterange_compact + - oe_whitelabel_helper + - text + - user +id: node.oe_sc_event.teaser +targetEntityType: node +bundle: oe_sc_event +mode: teaser +content: + oe_sc_event_dates: + type: daterange_compact + label: hidden + settings: + daterange_compact_format: oe_whitelabel_date_only_short_month + third_party_settings: { } + weight: 4 + region: content + oe_sc_event_location: + type: oe_whitelabel_helper_address_inline + label: hidden + settings: + delimiter: ', ' + properties: + country: true + locality: true + givenName: false + additionalName: false + familyName: false + organization: false + addressLine1: false + addressLine2: false + postalCode: false + sortingCode: false + dependentLocality: false + administrativeArea: false + third_party_settings: { } + weight: 5 + region: content + oe_summary: + type: text_default + label: hidden + settings: { } + third_party_settings: { } + weight: 2 + region: content +hidden: + body: true + langcode: true + links: true + oe_content_content_owner: true + oe_content_legacy_link: true + oe_content_navigation_title: true + oe_content_short_title: true + oe_documents: true + oe_featured_media: true + oe_sc_event_registration_url: true + search_api_excerpt: true diff --git a/modules/oe_whitelabel_starter_event/oe_whitelabel_starter_event.post_update.php b/modules/oe_whitelabel_starter_event/oe_whitelabel_starter_event.post_update.php index 9e4162cb7..c7f13088e 100644 --- a/modules/oe_whitelabel_starter_event/oe_whitelabel_starter_event.post_update.php +++ b/modules/oe_whitelabel_starter_event/oe_whitelabel_starter_event.post_update.php @@ -49,3 +49,10 @@ function oe_whitelabel_starter_event_post_update_00003(): void { TRUE ); } + +/** + * Add location to Event teaser. + */ +function oe_whitelabel_starter_event_post_update_00004(): void { + ConfigImporter::importSingle('module', 'oe_whitelabel_starter_event', '/config/post_updates/00004_teaser_location', 'core.entity_view_display.node.oe_sc_event.teaser'); +} diff --git a/templates/content/node--oe-sc-event--teaser.html.twig b/templates/content/node--oe-sc-event--teaser.html.twig index cb24a700a..df62e02cb 100755 --- a/templates/content/node--oe-sc-event--teaser.html.twig +++ b/templates/content/node--oe-sc-event--teaser.html.twig @@ -15,7 +15,8 @@ image: image, meta: [ content.oe_sc_event_dates|field_value, - ], + content.oe_sc_event_location|field_value + ]|filter(element => element is not empty), attributes: attributes, }) }} {% endblock %} diff --git a/tests/src/Functional/ContentEventRenderTest.php b/tests/src/Functional/ContentEventRenderTest.php index 4ed21ffba..7691c7de9 100644 --- a/tests/src/Functional/ContentEventRenderTest.php +++ b/tests/src/Functional/ContentEventRenderTest.php @@ -115,9 +115,13 @@ public function testEventPage(): void { $this->drupalGet($node->toUrl()); $crawler = $client->getCrawler(); - $date = $crawler->filter('dl > dd'); + $date = $crawler->filter('dl > dd:nth-of-type(1)'); $this->assertEquals('Monday 7 February 2022, 09.00 (CET) - Tuesday 22 February 2022, 19.00 (CET)', trim($date->text())); + // Assert address. + $address = $crawler->filter('dl > dd:nth-of-type(2)'); + $this->assertEquals('Charlemagne building, Wetstraat 170, 1040 Brussel, Belgium', trim($address->text())); + // Assert in-page navigation title. $this->assertEquals( 'Page content', @@ -178,8 +182,10 @@ public function testEventRenderingTeaser(): void { trim($image->attr('src')) ); - $time = $crawler->filter('div > span.text-muted'); + $time = $crawler->filter('div > span.text-muted:nth-of-type(1)'); $this->assertEquals('9 Feb 2022', trim($time->text())); + $address = $crawler->filter('div > span.text-muted:nth-of-type(2)'); + $this->assertEquals('Brussel, Belgium', trim($address->text())); // Assert event dates starting and ending at different days. $node->set('oe_sc_event_dates', [ @@ -194,8 +200,10 @@ public function testEventRenderingTeaser(): void { $crawler = new Crawler((string) $render); $this->drupalGet($node->toUrl()); - $time = $crawler->filter('div > span.text-muted'); + $time = $crawler->filter('div > span.text-muted:nth-of-type(1)'); $this->assertEquals('7 Feb 2022 - 22 Feb 2022', trim($time->text())); + $address = $crawler->filter('div > span.text-muted:nth-of-type(2)'); + $this->assertEquals('Brussel, Belgium', trim($address->text())); } /** @@ -252,6 +260,12 @@ protected function createExampleEvent(): NodeInterface { 'value' => '2022-02-09T20:00:00', 'end_value' => '2022-02-09T22:00:00', ], + 'oe_sc_event_location' => [ + 'country_code' => 'BE', + 'address_line1' => 'Charlemagne building, Wetstraat 170', + 'postal_code' => '1040', + 'locality' => 'Brussel', + ], 'uid' => 1, 'status' => 1, ]);