From c061cb956f99c003596a9690cec632aa169bd115 Mon Sep 17 00:00:00 2001
From: Sergio Elvira Perez <sergioelviraperez@gmail.com>
Date: Thu, 24 Feb 2022 15:52:26 +0100
Subject: [PATCH 1/2] OEL-1213: Address format created, tests provided.

---
 ...entity_view_display.node.oe_event.full.yml |   5 +-
 .../schema/oe_whitelabel_helper.schema.yml    |   7 +
 .../oe_whitelabel_helper.module               |  15 ++
 .../FieldFormatter/AddressInlineFormatter.php | 155 ++++++++++++++++++
 ...whitelabel-helper-address-inline.html.twig |  16 ++
 .../AddressInlineFormatterTest.php            | 123 ++++++++++++++
 6 files changed, 319 insertions(+), 2 deletions(-)
 create mode 100644 modules/oe_whitelabel_helper/config/schema/oe_whitelabel_helper.schema.yml
 create mode 100644 modules/oe_whitelabel_helper/src/Plugin/Field/FieldFormatter/AddressInlineFormatter.php
 create mode 100644 modules/oe_whitelabel_helper/templates/oe-whitelabel-helper-address-inline.html.twig
 create mode 100644 modules/oe_whitelabel_helper/tests/src/Kernel/Plugin/Field/FieldFormatter/AddressInlineFormatterTest.php

diff --git a/modules/oe_whitelabel_event/config/install/core.entity_view_display.node.oe_event.full.yml b/modules/oe_whitelabel_event/config/install/core.entity_view_display.node.oe_event.full.yml
index dee4b7f0..c09a3832 100755
--- a/modules/oe_whitelabel_event/config/install/core.entity_view_display.node.oe_event.full.yml
+++ b/modules/oe_whitelabel_event/config/install/core.entity_view_display.node.oe_event.full.yml
@@ -61,9 +61,10 @@ content:
       view_mode: default
     third_party_settings: {  }
   oe_location:
-    type: address_default
+    type: oe_whitelabel_helper_address_inline
     label: inline
-    settings: {  }
+    settings:
+      delimiter: ', '
     third_party_settings: {  }
     weight: 5
     region: content
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
new file mode 100644
index 00000000..a965f717
--- /dev/null
+++ b/modules/oe_whitelabel_helper/config/schema/oe_whitelabel_helper.schema.yml
@@ -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.'
diff --git a/modules/oe_whitelabel_helper/oe_whitelabel_helper.module b/modules/oe_whitelabel_helper/oe_whitelabel_helper.module
index 6874592c..57670247 100644
--- a/modules/oe_whitelabel_helper/oe_whitelabel_helper.module
+++ b/modules/oe_whitelabel_helper/oe_whitelabel_helper.module
@@ -13,3 +13,18 @@ declare(strict_types = 1);
 function oe_whitelabel_helper_locale_translation_projects_alter(&$projects) {
   $projects['oe_whitelabel_helper']['info']['interface translation server pattern'] = drupal_get_path('module', 'oe_whitelabel_helper') . '/translations/%project-%language.po';
 }
+
+/**
+ * Implements hook_theme().
+ */
+function oe_whitelabel_helper_theme($existing, $type, $theme, $path) {
+  return [
+    'oe_whitelabel_helper_address_inline' => [
+      'variables' => [
+        'address' => NULL,
+        'address_items' => [],
+        'address_delimiter' => NULL,
+      ],
+    ],
+  ];
+}
diff --git a/modules/oe_whitelabel_helper/src/Plugin/Field/FieldFormatter/AddressInlineFormatter.php b/modules/oe_whitelabel_helper/src/Plugin/Field/FieldFormatter/AddressInlineFormatter.php
new file mode 100644
index 00000000..8ce4f9da
--- /dev/null
+++ b/modules/oe_whitelabel_helper/src/Plugin/Field/FieldFormatter/AddressInlineFormatter.php
@@ -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;
+  }
+
+}
diff --git a/modules/oe_whitelabel_helper/templates/oe-whitelabel-helper-address-inline.html.twig b/modules/oe_whitelabel_helper/templates/oe-whitelabel-helper-address-inline.html.twig
new file mode 100644
index 00000000..5359af84
--- /dev/null
+++ b/modules/oe_whitelabel_helper/templates/oe-whitelabel-helper-address-inline.html.twig
@@ -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>
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
new file mode 100644
index 00000000..1ead5d5a
--- /dev/null
+++ b/modules/oe_whitelabel_helper/tests/src/Kernel/Plugin/Field/FieldFormatter/AddressInlineFormatterTest.php
@@ -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 &lt;123&gt;, or &#039;Wetstraat&#039; (Dutch), meaning &quot;Law Street&quot;, 1000 Brussels &lt;Bruxelles&gt;, 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',
+      ],
+    ];
+  }
+
+}
-- 
GitLab


From 69215e6a163704d1074e4f876f501a2a826b0e98 Mon Sep 17 00:00:00 2001
From: Sergio Elvira Perez <sergioelviraperez@gmail.com>
Date: Wed, 2 Mar 2022 09:13:07 +0100
Subject: [PATCH 2/2] OEL-1213: See added and removed group for testing
 uneeded.

---
 .../src/Plugin/Field/FieldFormatter/AddressInlineFormatter.php  | 2 ++
 .../Plugin/Field/FieldFormatter/AddressInlineFormatterTest.php  | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)

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 8ce4f9da..a080a0ea 100644
--- a/modules/oe_whitelabel_helper/src/Plugin/Field/FieldFormatter/AddressInlineFormatter.php
+++ b/modules/oe_whitelabel_helper/src/Plugin/Field/FieldFormatter/AddressInlineFormatter.php
@@ -21,6 +21,8 @@ use Drupal\Core\Language\LanguageInterface;
  *     "address",
  *   },
  * )
+ *
+ * @see https://github.com/openeuropa/oe_theme/blob/3.x/modules/oe_theme_helper/src/Plugin/Field/FieldFormatter/AddressInlineFormatter.php
  */
 class AddressInlineFormatter extends AddressDefaultFormatter {
 
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 1ead5d5a..ff3038e0 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
@@ -9,8 +9,6 @@ use Drupal\Tests\address\Kernel\Formatter\FormatterTestBase;
 
 /**
  * Test AddressInlineFormatter plugin.
- *
- * @group batch2
  */
 class AddressInlineFormatterTest extends FormatterTestBase {
 
-- 
GitLab