Commit b679d0f6 authored by bojanz's avatar bojanz
Browse files

Initial commit.

Contains code by cheatlex, luksak, googletorp, dwkitchen, vasike.
parents
name: Address
type: module
description: 'Provides functionality for handling postal addresses.'
package: Field types
core: 8.x
dependencies:
- field
<?php
/**
* Implements hook_requirements().
*/
function address_requirements($phase) {
$requirements = array();
if ($phase == 'install') {
if (!class_exists('\CommerceGuys\Addressing\Repository\AddressFormatRepository')) {
$requirements['addressing_library'] = array(
'description' => t('Address requires the commerceguys/addressing library.'),
'severity' => REQUIREMENT_ERROR,
);
}
}
return $requirements;
}
entity.address_format.add_form:
route_name: entity.address_format.add_form
title: 'Add a new address format'
appears_on:
- entity.address_format.collection
entity.subdivision.add_form:
route_name: entity.subdivision.add_form
title: 'Add a new subdivision'
appears_on:
- entity.subdivision.collection
entity.address_format.collection:
title: 'Address formats'
parent: system.admin_config_regional
description: 'Administer the address formats for each country.'
route_name: entity.address_format.collection
entity.address_format.collection:
route_name: 'entity.address_format.collection'
base_route: 'entity.address_format.collection'
title: 'List'
entity.address_format.import:
route_name: 'entity.address_format.import'
base_route: 'entity.address_format.collection'
title: 'Import'
entity.subdivision.collection:
route_name: 'entity.subdivision.collection'
base_route: 'entity.subdivision.collection'
title: 'List'
<?php
/**
* @file
* Provides functionality for handling postal addresses.
*/
'administer address formats':
title: 'Administer address formats'
description: 'Allows users to create, update, and delete address format entities.'
'restrict access': TRUE
entity.address_format.collection:
path: '/admin/config/regional/address-formats'
defaults:
_entity_list: 'address_format'
_title: 'Address formats'
requirements:
_permission: 'administer address formats'
entity.address_format.import:
path: '/admin/config/regional/address-formats/import'
defaults:
_form: '\Drupal\address\Form\AddressFormatImporterForm'
_title: 'Import an address format'
requirements:
_permission: 'administer address formats'
entity.address_format.add_form:
path: '/admin/config/regional/address-formats/add'
defaults:
_entity_form: 'address_format.add'
_title: 'Add a new address format'
requirements:
_entity_create_access: 'address_format'
entity.address_format.edit_form:
path: '/admin/config/regional/address-formats/manage/{address_format}'
defaults:
_entity_form: 'address_format.edit'
_title: 'Edit an address format'
requirements:
_entity_access: 'address_format.update'
entity.address_format.delete_form:
path: '/admin/config/regional/address-formats/manage/{address_format}/delete'
defaults:
_entity_form: 'address_format.delete'
_title: 'Delete an address format'
requirements:
_entity_access: 'address_format.delete'
entity.subdivision.collection:
path: '/admin/config/regional/subdivisions/{address_format}/{parent}'
defaults:
parent: null
_controller: '\Drupal\address\Controller\SubdivisionController::buildList'
_title: 'Subdivisions'
options:
parameters:
address_format:
type: entity:address_format
parent:
type: entity:subdivision
requirements:
_permission: 'administer subdivisions'
entity.subdivision.add_form:
path: '/admin/config/regional/subdivisions/add/{address_format}/{parent}'
defaults:
parent: null
_controller: '\Drupal\address\Controller\SubdivisionController::addForm'
_title: 'Add a new subdivision'
options:
parameters:
address_format:
type: entity:address_format
parent:
type: entity:subdivision
requirements:
_entity_create_access: 'subdivision'
entity.subdivision.edit_form:
path: '/admin/config/regional/subdivisions/manage/{subdivision}'
defaults:
_entity_form: 'subdivision.edit'
_title: 'Edit a subdivision'
requirements:
_entity_access: 'subdivision.update'
entity.subdivision.delete_form:
path: '/admin/config/regional/subdivisions/manage/{subdivision}/delete'
defaults:
_entity_form: 'subdivision.delete'
_title: 'Delete a subdivision'
requirements:
_entity_access: 'subdivision.delete'
services:
address.subdivision_record_storage:
class: Drupal\address\SubdivisionRecordStorage
arguments: ['@config.factory']
address.subdivision_query:
class: Drupal\address\Entity\Query\SubdivisionQueryFactory
arguments: ['@address.subdivision_record_storage']
address.address_format_importer:
class: Drupal\address\AddressFormatImporter
arguments: ['@entity.manager', '@language_manager']
address.data_provider:
class: Drupal\address\DataProvider
arguments: ['@country_manager', '@entity.manager']
address.postal_formatter:
class: CommerceGuys\Addressing\Formatter\PostalFormatter
arguments: ['@address.data_provider']
{
"name": "drupal/address",
"type": "drupal-module",
"description": "Defines flexible address field entity.",
"homepage": "http://drupal.org/project/address",
"license": "GPL-2.0+",
"require": {
"commerceguys/addressing": "*"
},
"minimum-stability": "dev"
}
address.address_format.*:
type: config_entity
label: 'Address format'
mapping:
countryCode:
type: string
label: 'Country code'
format:
type: string
label: 'Format'
requiredFields:
type: sequence
label: 'Required fields'
sequence:
- type: string
uppercaseFields:
type: sequence
label: 'Uppercase Fields'
sequence:
- type: string
administrativeAreaType:
type: string
label: 'Administrative area type'
localityType:
type: string
label: 'Locality type'
dependentLocalityType:
type: string
label: 'Dependent locality type'
postalCodeType:
type: string
label: 'Postal code type'
postalCodePattern:
type: string
label: 'Postal code pattern'
postalCodePrefix:
type: string
label: 'Postal code prefix'
address.subdivisions.*:
type: mapping
label: 'Subdivisions'
mapping:
subdivisions:
type: sequence
label: 'Subdivisions'
sequence:
- type: address.subdivision.[%key]
address.subdivision.*:
type: config_entity
label: 'Subdivision'
mapping:
countryCode:
type: string
label: 'Country code'
parentId:
type: string
label: 'Parent ID'
id:
type: string
label: 'ID'
code:
type: string
label: 'Code'
name:
type: label
label: 'Name'
translatable: true
<?php
/**
* @file
* Contains \Drupal\address\AddressFormatImporter.
*/
namespace Drupal\address;
use CommerceGuys\Addressing\Repository\AddressFormatRepository;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\address\AddressFormatImporterInterface;
use Drupal\language\ConfigurableLanguageManagerInterface;
class AddressFormatImporter implements AddressFormatImporterInterface {
/**
* The address format manager.
*
* @var \CommerceGuys\Addressing\Repository\AddressFormatRepositoryInterface
*/
protected $addressFormatRepository;
/**
* The address format storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $addressFormatStorage;
/**
* The configurable language manager.
*
* @var \Drupal\language\ConfigurableLanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs a new CurrencyImporter.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The entity manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* The language manager.
*/
public function __construct(EntityManagerInterface $entityManager, LanguageManagerInterface $languageManager) {
$this->addressFormatStorage = $entityManager->getStorage('address_format');
$this->languageManager = $languageManager;
$this->addressFormatRepository = new AddressFormatRepository();
}
/**
* {@inheritdoc}
*/
public function getImportableAddressFormats() {
$language = $this->languageManager->getCurrentLanguage();
$importableAddressFormats = $this->addressFormatRepository->getAll($language->getId());
$importedAddressFormats = $this->addressFormatStorage->loadMultiple();
// Remove any already imported currencies.
foreach ($importedAddressFormats as $addressFormat) {
if (isset($importableAddressFormats[$addressFormat->id()])) {
unset($importableAddressFormats[$addressFormat->id()]);
}
}
return $importableAddressFormats;
}
/**
* {@inheritdoc}
*/
public function importAddressFormat($countryCode) {
if ($this->addressFormatStorage->load($countryCode)) {
return FALSE;
}
$language = $this->languageManager->getDefaultLanguage();
$addressFormat = $this->getAddressFormat($countryCode, $language);
$values = array(
'countryCode' => $addressFormat->getCountryCode(),
'format' => $addressFormat->getFormat(),
'requiredFields' => $addressFormat->getRequiredFields(),
'uppercaseFields' => $addressFormat->getUppercaseFields(),
'administrativeAreaType' => $addressFormat->getAdministrativeAreaType(),
'localityType' => $addressFormat->getLocalityType(),
'dependentLocalityType' => $addressFormat->getDependentLocalityType(),
'postalCodeType' => $addressFormat->getPostalCodeType(),
'postalCodePattern' => $addressFormat->getPostalCodePattern(),
'postalCodePrefix' => $addressFormat->getPostalCodePrefix(),
);
$entity = $this->addressFormatStorage->create($values);
return $entity;
}
/**
* Get a single currency.
*
* @param string $countryCode
* The country code.
* @param \Drupal\Core\Language\LanguageInterface $language
* The language.
*
* @return CommerceGuys\Addressing\Model\AddressFormat
* Returns \CommerceGuys\Addressing\Model\AddressFormat
*/
protected function getAddressFormat($countryCode, LanguageInterface $language) {
return $this->addressFormatRepository->get($countryCode, $language->getId());
}
}
<?php
/**
* @file
* Contains \Drupal\address\AddressFormatImporterInterface.
*/
namespace Drupal\address;
/**
* Defines an address format importer.
*/
interface AddressFormatImporterInterface {
/**
* Returns all importable address formats.
*
* @return \CommerceGuys\Addressing\Model\AddressFormatInterface[]
* Array of importable address formats.
*/
public function getImportableAddressFormats();
/**
* Creates a new address format object for the given country code.
*
* @param string $countryCode
* The country code.
*
* @return \Drupal\address\Entity\AddressFormat | bool
* The new address format or false if the address format is already imported.
*/
public function importAddressFormat($countryCode);
}
<?php
/**
* @file
* Contains \Drupal\address\AddressFormatListBuilder.
*/
namespace Drupal\address;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Locale\CountryManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a listing of address formats.
*/
class AddressFormatListBuilder extends ConfigEntityListBuilder {
/**
* The country manager.
*
* @var \Drupal\Core\Locale\CountryManagerInterface
*/
protected $countryManager;
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entityType) {
return new static(
$entityType,
$container->get('entity.manager')->getStorage($entityType->id()),
$container->get('country_manager')
);
}
/**
* Constructs a new AddressFormatListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entityType
* The entity type definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
* @param \Drupal\Core\Locale\CountryManagerInterface $countryManager
* The country manager.
*/
public function __construct(EntityTypeInterface $entityType, EntityStorageInterface $storage, CountryManagerInterface $countryManager) {
$this->entityTypeId = $entityType->id();
$this->storage = $storage;
$this->entityType = $entityType;
$this->countryManager = $countryManager;
}
/**
* {@inheritdoc}
*/
public function getDefaultOperations(EntityInterface $entity) {
/** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
$operations = parent::getDefaultOperations($entity);
// Show the 'List subdivisions' operation if the parent format
// uses at least the administrative area subdivision field.
$format = $entity->getFormat();
if (strpos($format, '%administrative_area') !== FALSE) {
$operations['subdivisions'] = array(
'title' => $this->t('List subdivisions'),
'url' => Url::fromRoute('entity.subdivision.collection', array(
'address_format' => $entity->id(),
)),
);
}
return $operations;
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['country'] = $this->t('Country');
$header['status'] = $this->t('Status');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['country'] = $this->getCountryName($entity->id());
$row['status'] = $entity->status() ? $this->t('Enabled') : $this->t('Disabled');
return $row + parent::buildRow($entity);
}
/**
* Returns the name of the country with the provided code.
*
* @param string $countryCode
* The country code.
*
* @return string
* The country name.
*/
protected function getCountryName($countryCode) {
if ($countryCode == 'ZZ') {
return $this->t('Generic');
}
else {
$countries = $this->countryManager->getList();
return $countries[$countryCode];
}
}
}
<?php
/**
* @file
* Contains \Drupal\address\Controller\SubdivisionController.
*/
namespace Drupal\address\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Provides route responses for subdivisions.
*/
class SubdivisionController extends ControllerBase {
/**
* Provides the subdivision add form.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* The route match.
*
* @return array
* The subdivision add form.
*/
public function addForm(RouteMatchInterface $routeMatch) {
$addressFormat = $routeMatch->getParameter('address_format');
$parent = $routeMatch->getParameter('parent');
$values = array(
'countryCode' => $addressFormat->getCountryCode(),
'parentId' => $parent ? $parent->id() : NULL,
);
$subdivision = $this->entityManager()->getStorage('subdivision')->create($values);
return $this->entityFormBuilder()->getForm($subdivision, 'add');
}
/**
* Provides the subdivision list.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* The route match.
*
* @return array
* The subdivision list.
*/
public function buildList(RouteMatchInterface $routeMatch) {
$listBuilder = $this->entityManager()->getListBuilder('subdivision');
$listBuilder->setAddressFormat($routeMatch->getParameter('address_format'));
$listBuilder->setParent($routeMatch->getParameter('parent'));
$build = array();
$build['subdivision_table'] = $listBuilder->render();
return $build;
}
}
<?php
namespace Drupal\address;
use CommerceGuys\Addressing\Provider\DataProviderInterface;
use Drupal\Core\Locale\CountryManagerInterface;
use Drupal\Core\Entity\EntityManagerInterface;
/**
* Exposes Drupal data to the addressing library.
*
* Note that the methods ignore the $locale parameter because the loaded
* data is already translated into the current language.
*/
class DataProvider implements DataProviderInterface
{
/**
* The country manager.
*