Skip to content

Commit

Permalink
Merge pull request #13361 from craftcms/feature/address-subdivisions-…
Browse files Browse the repository at this point in the history
…event

Feature/address subdivisions event
  • Loading branch information
brandonkelly authored Jun 28, 2023
2 parents 58a9940 + 2b4629c commit 434342c
Show file tree
Hide file tree
Showing 5 changed files with 992 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Custom element sources can now be configured to only appear for certain sites. ([#13344](https://github.com/craftcms/cms/discussions/13344))
- The “My Account” page no longer shows a “Require a password reset on next login” checkbox.
- The Asset Indexes utility no longer shows the “Cache remote images” option on ephemeral environments. ([#13202](https://github.com/craftcms/cms/issues/13202))
- It’s now possible to configure UK addresses to show a “County” field. ([#13361](https://github.com/craftcms/cms/pull/13361))

### Development
- Added a new `_globals` global Twig variable for front-end templates, which can be used to store custom values in a global scope. ([#13050](https://github.com/craftcms/cms/pull/13050), [#12951](https://github.com/craftcms/cms/discussions/12951))
Expand All @@ -36,6 +37,7 @@
### Extensibility
- Filesystem types can now register custom file uploaders. ([#13313](https://github.com/craftcms/cms/pull/13313))
- When applying a draft, the canonical elements’ `getDirtyAttributes()` and `getDirtyFields()` methods now return the attribute names and field handles that were modified on the draft for save events. ([#12967](https://github.com/craftcms/cms/issues/12967))
- Added `craft\addresses\SubdivisionRepository`. ([#13361](https://github.com/craftcms/cms/pull/13361))
- Added `craft\base\ElementInterface::getThumbSvg()`. ([#13262](https://github.com/craftcms/cms/pull/13262))
- Added `craft\base\ElementInterface::setDirtyFields()`.
- Added `craft\base\ElementInterface::setFieldValueFromRequest()`. ([#12935](https://github.com/craftcms/cms/issues/12935))
Expand All @@ -46,12 +48,15 @@
- Added `craft\base\FsTrait::$showHasUrlSetting`. ([#13224](https://github.com/craftcms/cms/pull/13224))
- Added `craft\base\FsTrait::$showUrlSetting`. ([#13224](https://github.com/craftcms/cms/pull/13224))
- Added `craft\controllers\AssetsControllerTrait`.
- Added `craft\events\DefineAddressSubdivisionsEvent`. ([#13361](https://github.com/craftcms/cms/pull/13361))
- Added `craft\gql\GqlEntityRegistry::getOrCreate()`. ([#13354](https://github.com/craftcms/cms/pull/13354))
- Added `craft\helpers\Assets::iconSvg()`.
- Added `craft\helpers\StringHelper::escapeShortcodes()`. ([#12935](https://github.com/craftcms/cms/issues/12935))
- Added `craft\helpers\StringHelper::unescapeShortcodes()`. ([#12935](https://github.com/craftcms/cms/issues/12935))
- Added `craft\models\FieldLayout::$provider`. ([#13250](https://github.com/craftcms/cms/pull/13250))
- Added `craft\services\Addresses::$formatter`, which can be used to override the default address formatter. ([#13242](https://github.com/craftcms/cms/pull/13242), [#12615](https://github.com/craftcms/cms/discussions/12615))
- Added `craft\services\Addresses::EVENT_DEFINE_ADDRESS_SUBDIVISIONS`. ([#13361](https://github.com/craftcms/cms/pull/13361))
- Added `craft\services\Addresses::defineAddressSubdivisions()`. ([#13361](https://github.com/craftcms/cms/pull/13361))
- Added `craft\web\CpScreenResponseBehavior::$pageSidebar`, `pageSidebar()`, and `pageSidebarTemplate()`. ([#13019](https://github.com/craftcms/cms/pull/13019), [#12795](https://github.com/craftcms/cms/issues/12795))
- Added `craft\web\CpScreenResponseBehavior::$slideoutBodyClass`.
- `craft\helpers\Cp::selectizeFieldHtml()`, `selectizeHtml()`, and `_includes/forms/selectize.twig` now support a `multi` param. ([#13176](https://github.com/craftcms/cms/pull/13176))
Expand Down
68 changes: 68 additions & 0 deletions src/addresses/SubdivisionRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\addresses;

use CommerceGuys\Addressing\Locale;
use CommerceGuys\Addressing\Subdivision\SubdivisionRepository as BaseSubdivisionRepository;
use Craft;

/**
* Craft's extension of the commerceguys/addressing SubdivisionRepository.
* Its main purpose is to allow addition of data that's not returned by the commerceguys/addressing library,
* like the GB counties data. It also triggers an event which allows developers to modify the subdivisions further.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 4.5.0
*/
class SubdivisionRepository extends BaseSubdivisionRepository
{
/**
* @inheritdoc
*/
public function getList(array $parents, $locale = null): array
{
// get the list of subdivisions from commerceguys/addressing
$options = parent::getList($parents, Craft::$app->language);

// if the list is empty (like in case of GB), get the extra options from our files
if (empty($options)) {
$options = $this->_getExtraOptions($parents, Craft::$app->language);
}

// trigger the event to give devs a chance to modify further, and return the list
return Craft::$app->getAddresses()->defineAddressSubdivisions($parents, $options);
}

/**
* Get a list of extra subdivision options
*
* @param array $parents
* @param string|null $lang
* @return array
*/
private function _getExtraOptions(array $parents, string $lang = null): array
{
$list = [];
$fileName = implode('-', $parents);
$filePath = __DIR__ . '/data/' . $fileName . '.json';

if (@file_exists($filePath) && $data = @file_get_contents($filePath)) {
$data = json_decode($data, true);

if ($data['subdivisions']) {
$useLocalName = Locale::matchCandidates($lang, $data['extraLocale'] ?? null);
foreach ($data['subdivisions'] as $key => $value) {
$list[$key] = $useLocalName ? $value['local_name'] : $value['name'];
}
}
}
ksort($list);

return $list;
}
}
Loading

0 comments on commit 434342c

Please sign in to comment.