Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OEL-1676: Add address to Event teaser. #194

Merged
merged 19 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,14 +33,14 @@ class AddressInlineFormatter extends AddressDefaultFormatter {
public static function defaultSettings() {
return [
'delimiter' => ', ',
'properties' => [],
];
}

/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {

$form['delimiter'] = [
'#type' => 'textfield',
'#title' => $this->t('Delimiter'),
Expand All @@ -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;
}

Expand All @@ -59,6 +68,9 @@ public function settingsSummary() {
$this->t('Delimiter: @delimiter', [
'@delimiter' => $this->getSetting('delimiter'),
]),
$this->t('Properties: @properties', [
'@properties' => implode(', ', $this->getActiveProperties()),
]),
];
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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));
Expand All @@ -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')));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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' => [
Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
3 changes: 2 additions & 1 deletion templates/content/node--oe-sc-event--teaser.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
image: image,
meta: [
content.oe_sc_event_dates|field_value,
],
content.oe_sc_event_location|field_value
brummbar marked this conversation as resolved.
Show resolved Hide resolved
]|filter(element => element is not empty),
attributes: attributes,
}) }}
{% endblock %}
20 changes: 17 additions & 3 deletions tests/src/Functional/ContentEventRenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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', [
Expand All @@ -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()));
}

/**
Expand Down Expand Up @@ -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,
]);
Expand Down