-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OEL-1213: Address format created, tests provided.
- Loading branch information
Showing
6 changed files
with
319 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
modules/oe_whitelabel_helper/config/schema/oe_whitelabel_helper.schema.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
field.formatter.settings.oe_whitelabel_helper_address_inline: | ||
type: mapping | ||
label: 'Address inline formatter settings' | ||
mapping: | ||
delimiter: | ||
type: string | ||
label: 'Delimiter for address items.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
modules/oe_whitelabel_helper/src/Plugin/Field/FieldFormatter/AddressInlineFormatter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\oe_whitelabel_helper\Plugin\Field\FieldFormatter; | ||
|
||
use CommerceGuys\Addressing\Locale; | ||
use Drupal\address\AddressInterface; | ||
use Drupal\address\Plugin\Field\FieldFormatter\AddressDefaultFormatter; | ||
use Drupal\Core\Field\FieldItemListInterface; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\Core\Language\LanguageInterface; | ||
|
||
/** | ||
* Format an address inline with locale format and a configurable separator. | ||
* | ||
* @FieldFormatter( | ||
* id = "oe_whitelabel_helper_address_inline", | ||
* label = @Translation("Inline address"), | ||
* field_types = { | ||
* "address", | ||
* }, | ||
* ) | ||
*/ | ||
class AddressInlineFormatter extends AddressDefaultFormatter { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function defaultSettings() { | ||
return [ | ||
'delimiter' => ', ', | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function settingsForm(array $form, FormStateInterface $form_state) { | ||
|
||
$form['delimiter'] = [ | ||
'#type' => 'textfield', | ||
'#title' => $this->t('Delimiter'), | ||
'#default_value' => $this->getSetting('delimiter'), | ||
'#description' => $this->t('Specify delimiter between address items.'), | ||
'#required' => TRUE, | ||
]; | ||
|
||
return $form; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function settingsSummary() { | ||
return [ | ||
$this->t('Delimiter: @delimiter', [ | ||
'@delimiter' => $this->getSetting('delimiter'), | ||
]), | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function viewElements(FieldItemListInterface $items, $langcode) { | ||
$elements = []; | ||
foreach ($items as $delta => $item) { | ||
$elements[$delta] = $this->viewElement($item, $langcode); | ||
} | ||
|
||
return $elements; | ||
} | ||
|
||
/** | ||
* Builds a renderable array for a single address item. | ||
* | ||
* @param \Drupal\address\AddressInterface $address | ||
* The address. | ||
* @param string $langcode | ||
* The language that should be used to render the field. | ||
* | ||
* @return array | ||
* A renderable array. | ||
*/ | ||
protected function viewElement(AddressInterface $address, $langcode) { | ||
$country_code = $address->getCountryCode(); | ||
$countries = $this->countryRepository->getList($langcode); | ||
$address_format = $this->addressFormatRepository->get($country_code); | ||
$values = $this->getValues($address, $address_format); | ||
|
||
$address_elements['%country'] = $countries[$country_code]; | ||
foreach ($address_format->getUsedFields() as $field) { | ||
$address_elements['%' . $field] = $values[$field]; | ||
} | ||
|
||
if (Locale::matchCandidates($address_format->getLocale(), $address->getLocale())) { | ||
$format_string = '%country' . "\n" . $address_format->getLocalFormat(); | ||
} | ||
else { | ||
$format_string = $address_format->getFormat() . "\n" . '%country'; | ||
} | ||
/* | ||
* Remove extra characters from address format since address fields are | ||
* optional. | ||
* | ||
* @see \CommerceGuys\Addressing\AddressFormat\AddressFormatRepository::getDefinitions() | ||
*/ | ||
$format_string = str_replace([',', ' - ', '/'], "\n", $format_string); | ||
|
||
$items = $this->extractAddressItems($format_string, $address_elements); | ||
|
||
return [ | ||
'#theme' => 'oe_whitelabel_helper_address_inline', | ||
'#address' => $address, | ||
'#address_items' => $items, | ||
'#address_delimiter' => $this->getSetting('delimiter'), | ||
'#cache' => [ | ||
'contexts' => [ | ||
'languages:' . LanguageInterface::TYPE_INTERFACE, | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Extract address items from a format string and replace placeholders. | ||
* | ||
* @param string $string | ||
* The address format string, containing placeholders. | ||
* @param array $replacements | ||
* An array of address items. | ||
* | ||
* @return array | ||
* The exploded lines. | ||
*/ | ||
protected function extractAddressItems(string $string, array $replacements): array { | ||
// 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)); | ||
$line = preg_replace('/\s\s+/', ' ', $line); | ||
$lines[$index] = $line; | ||
} | ||
// Remove empty lines. | ||
$lines = array_filter($lines); | ||
|
||
return $lines; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
modules/oe_whitelabel_helper/templates/oe-whitelabel-helper-address-inline.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{# | ||
/** | ||
* @file | ||
* Default template for the 'oe_whitelabel_helper_address_inline' address formatter. | ||
* | ||
* Available variables: | ||
* - address: Address object. | ||
* - address_items: Address items. | ||
* - address_delimiter: Delimiter between address items. | ||
* | ||
* @ingroup themeable | ||
*/ | ||
#} | ||
<span translate="no"> | ||
{{ address_items|join(address_delimiter) }} | ||
</span> |
123 changes: 123 additions & 0 deletions
123
...elabel_helper/tests/src/Kernel/Plugin/Field/FieldFormatter/AddressInlineFormatterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\Tests\oe_whitelabel_helper\Kernel\Plugin\Field\FieldFormatter; | ||
|
||
use Drupal\entity_test\Entity\EntityTest; | ||
use Drupal\Tests\address\Kernel\Formatter\FormatterTestBase; | ||
|
||
/** | ||
* Test AddressInlineFormatter plugin. | ||
* | ||
* @group batch2 | ||
*/ | ||
class AddressInlineFormatterTest extends FormatterTestBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static $modules = [ | ||
'oe_whitelabel_helper', | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->createField('address', 'oe_whitelabel_helper_address_inline'); | ||
} | ||
|
||
/** | ||
* Tests formatting of address. | ||
*/ | ||
public function testInlineFormatterAddress(): void { | ||
$entity = EntityTest::create([]); | ||
foreach ($this->addressFieldTestData() as $data) { | ||
$cloned_entity = clone $entity; | ||
$cloned_entity->{$this->fieldName} = $data['address']; | ||
$this->renderEntityFields($cloned_entity, $this->display); | ||
$this->assertRaw($data['expected']); | ||
unset($cloned_entity); | ||
} | ||
} | ||
|
||
/** | ||
* Test data for testInlineFormatterAddress. | ||
* | ||
* @return array[] | ||
* An array of test data arrays with expected result. | ||
*/ | ||
public function addressFieldTestData(): array { | ||
return [ | ||
'Brussels Belgium' => [ | ||
'address' => [ | ||
'country_code' => 'BE', | ||
'locality' => 'Brussels <Bruxelles>', | ||
'postal_code' => '1000', | ||
'address_line1' => 'Rue de la Loi, 56 <123>', | ||
'address_line2' => 'or \'Wetstraat\' (Dutch), meaning "Law Street"', | ||
], | ||
'expected' => 'Rue de la Loi, 56 <123>, or 'Wetstraat' (Dutch), meaning "Law Street", 1000 Brussels <Bruxelles>, Belgium', | ||
], | ||
'Mexico' => [ | ||
'address' => [ | ||
'country_code' => 'MX', | ||
], | ||
'expected' => 'Mexico', | ||
], | ||
'Mexico Ciudad de Mexico' => [ | ||
'address' => [ | ||
'country_code' => 'MX', | ||
'administrative_area' => 'CDMX', | ||
], | ||
'expected' => 'CDMX, Mexico', | ||
], | ||
'Mexico Baja California Tijuana' => [ | ||
'address' => [ | ||
'country_code' => 'MX', | ||
'administrative_area' => 'B.C.', | ||
'locality' => 'Tijuana', | ||
], | ||
'expected' => 'Tijuana, B.C., Mexico', | ||
], | ||
'Mexico Baja California Tijuana 22000' => [ | ||
'address' => [ | ||
'country_code' => 'MX', | ||
'administrative_area' => 'B.C.', | ||
'locality' => 'Tijuana', | ||
'postal_code' => '22000', | ||
], | ||
'expected' => '22000 Tijuana, B.C., Mexico', | ||
], | ||
'Mexico Baja California Tijuana 22000 Street' => [ | ||
'address' => [ | ||
'country_code' => 'MX', | ||
'administrative_area' => 'B.C.', | ||
'locality' => 'Tijuana', | ||
'postal_code' => '22000', | ||
'address_line1' => 'Street', | ||
], | ||
'expected' => 'Street, 22000 Tijuana, B.C., Mexico', | ||
], | ||
'Bangladesh Dhaka' => [ | ||
'address' => [ | ||
'country_code' => 'BD', | ||
'locality' => 'Dhaka', | ||
], | ||
'expected' => 'Dhaka, Bangladesh', | ||
], | ||
'Bangladesh Dhaka 1100' => [ | ||
'address' => [ | ||
'country_code' => 'BD', | ||
'locality' => 'Dhaka', | ||
'postal_code' => '1100', | ||
], | ||
'expected' => 'Dhaka, 1100, Bangladesh', | ||
], | ||
]; | ||
} | ||
|
||
} |