Commit 9f864b12 authored by bojanz's avatar bojanz

Issue #2853976 by bojanz: Update for commerceguys/addressing v1.0

parent 57bee306
......@@ -42,4 +42,3 @@ function address_post_update_convert_names_subdivisions(&$sandbox = NULL) {
/**
* @} End of "addtogroup updates-8.x-1.0-rc1".
*/
......@@ -5,8 +5,7 @@
"homepage": "http://drupal.org/project/address",
"license": "GPL-2.0+",
"require": {
"commerceguys/intl": "~0.7",
"commerceguys/addressing": "~1.0"
"commerceguys/addressing": "dev-master"
},
"minimum-stability": "dev"
}
{
"require": {
"commerceguys/intl": {
"version": "v0.7.4",
"url": "https://github.com/commerceguys/intl/archive/v0.7.4.zip"
},
"commerceguys/addressing": {
"version": "v1.0.0-beta5",
"url": "https://github.com/commerceguys/addressing/archive/v1.0.0-beta5.zip"
"version": "dev-master",
"url": "https://github.com/commerceguys/addressing/archive/master.zip"
}
}
}
......@@ -5,7 +5,7 @@ namespace Drupal\address\Element;
use CommerceGuys\Addressing\AddressFormat\AddressField;
use CommerceGuys\Addressing\AddressFormat\AddressFormat;
use CommerceGuys\Addressing\AddressFormat\AddressFormatHelper;
use CommerceGuys\Addressing\LocaleHelper;
use CommerceGuys\Addressing\Locale;
use Drupal\address\FieldHelper;
use Drupal\address\LabelHelper;
use Drupal\Component\Utility\Html;
......@@ -202,7 +202,7 @@ class Address extends FormElement {
$required_fields = $address_format->getRequiredFields();
$labels = LabelHelper::getFieldLabels($address_format);
$locale = \Drupal::languageManager()->getConfigOverrideLanguage()->getId();
if (LocaleHelper::match($address_format->getLocale(), $locale)) {
if (Locale::matchCandidates($address_format->getLocale(), $locale)) {
$format_string = $address_format->getLocalFormat();
}
else {
......
......@@ -6,7 +6,7 @@ use CommerceGuys\Addressing\AddressFormat\AddressField;
use CommerceGuys\Addressing\AddressFormat\AddressFormat;
use CommerceGuys\Addressing\AddressFormat\AddressFormatRepositoryInterface;
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
use CommerceGuys\Addressing\LocaleHelper;
use CommerceGuys\Addressing\Locale;
use CommerceGuys\Addressing\Subdivision\SubdivisionRepositoryInterface;
use Drupal\address\AddressInterface;
use Drupal\address\FieldHelper;
......@@ -190,7 +190,7 @@ class AddressDefaultFormatter extends FormatterBase implements ContainerFactoryP
$locale = $element['#locale'];
// Add the country to the bottom or the top of the format string,
// depending on whether the format is minor-to-major or major-to-minor.
if (LocaleHelper::match($address_format->getLocale(), $locale)) {
if (Locale::matchCandidates($address_format->getLocale(), $locale)) {
$format_string = '%country' . "\n" . $address_format->getLocalFormat();
}
else {
......@@ -274,7 +274,7 @@ class AddressDefaultFormatter extends FormatterBase implements ContainerFactoryP
// Remember the original value so that it can be used for $parents.
$original_values[$field] = $values[$field];
// Replace the value with the expected code.
$use_local_name = LocaleHelper::match($address->getLocale(), $subdivision->getLocale());
$use_local_name = Locale::matchCandidates($address->getLocale(), $subdivision->getLocale());
$values[$field] = $use_local_name ? $subdivision->getLocalCode() : $subdivision->getCode();
if (!$subdivision->hasChildren()) {
// The current subdivision has no children, stop.
......
......@@ -6,7 +6,7 @@ use CommerceGuys\Addressing\AddressFormat\AddressField;
use CommerceGuys\Addressing\AddressFormat\AddressFormat;
use CommerceGuys\Addressing\AddressFormat\AddressFormatRepositoryInterface;
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
use CommerceGuys\Addressing\LocaleHelper;
use CommerceGuys\Addressing\Locale;
use CommerceGuys\Addressing\Subdivision\SubdivisionRepositoryInterface;
use Drupal\address\AddressInterface;
use Drupal\Core\Field\FormatterBase;
......@@ -199,7 +199,7 @@ class AddressPlainFormatter extends FormatterBase implements ContainerFactoryPlu
// Remember the original value so that it can be used for $parents.
$original_values[$field] = $value;
// Replace the value with the expected code.
if (LocaleHelper::match($address->getLocale(), $subdivision->getLocale())) {
if (Locale::matchCandidates($address->getLocale(), $subdivision->getLocale())) {
$values[$field] = [
'code' => $subdivision->getLocalCode(),
'name' => $subdivision->getLocalName(),
......
......@@ -2,7 +2,7 @@
namespace Drupal\address\Plugin\views\field;
use CommerceGuys\Addressing\LocaleHelper;
use CommerceGuys\Addressing\Locale;
use CommerceGuys\Addressing\Subdivision\SubdivisionRepositoryInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
......@@ -95,7 +95,7 @@ class Subdivision extends FieldPluginBase {
$subdivision = $this->subdivisionRepository->get($code, $parents);
// @todo Allow a choice between subdivision code and name.
if ($subdivision) {
$use_local_name = LocaleHelper::match($address->getLocale(), $subdivision->getLocale());
$use_local_name = Locale::matchCandidates($address->getLocale(), $subdivision->getLocale());
$value = $use_local_name ? $subdivision->getLocalName() : $subdivision->getName();
}
......
......@@ -2,8 +2,7 @@
namespace Drupal\address\Repository;
use CommerceGuys\Intl\Country\CountryRepository as ExternalCountryRepository;
use CommerceGuys\Addressing\Country\CountryRepositoryInterface as ExternalCountryRepositoryInterface;
use CommerceGuys\Addressing\Country\CountryRepository as ExternalCountryRepository;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Language\LanguageManagerInterface;
......@@ -12,7 +11,7 @@ use Drupal\Core\Language\LanguageManagerInterface;
*
* Countries are stored on disk in JSON and cached inside Drupal.
*/
class CountryRepository extends ExternalCountryRepository implements ExternalCountryRepositoryInterface {
class CountryRepository extends ExternalCountryRepository {
/**
* The cache backend.
......@@ -53,38 +52,10 @@ class CountryRepository extends ExternalCountryRepository implements ExternalCou
else {
$filename = $this->definitionPath . $locale . '.json';
$this->definitions[$locale] = json_decode(file_get_contents($filename), TRUE);
// Merge-in base definitions.
$base_definitions = $this->loadBaseDefinitions();
foreach ($this->definitions[$locale] as $countryCode => $definition) {
$this->definitions[$locale][$countryCode] += $base_definitions[$countryCode];
}
$this->cache->set($cache_key, $this->definitions[$locale], CacheBackendInterface::CACHE_PERMANENT, ['countries']);
}
return $this->definitions[$locale];
}
/**
* Loads the base country definitions.
*
* @return array
* The base country definitions.
*/
protected function loadBaseDefinitions() {
if (!empty($this->baseDefinitions)) {
return $this->baseDefinitions;
}
$cache_key = 'address.countries.base';
if ($cached = $this->cache->get($cache_key)) {
$this->baseDefinitions = $cached->data;
}
else {
$this->baseDefinitions = json_decode(file_get_contents($this->definitionPath . 'base.json'), TRUE);
$this->cache->set($cache_key, $this->baseDefinitions, CacheBackendInterface::CACHE_PERMANENT, ['countries']);
}
return $this->baseDefinitions;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment