-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13361 from craftcms/feature/address-subdivisions-…
…event Feature/address subdivisions event
- Loading branch information
Showing
5 changed files
with
992 additions
and
1 deletion.
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
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,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; | ||
} | ||
} |
Oops, something went wrong.