Commit 2631a154 authored by bojanz's avatar bojanz
Browse files

Create a LabelHelper and move the existing labels there, allowing them to be reused.

parent 010ab010
......@@ -8,11 +8,8 @@
namespace Drupal\address\Form;
use CommerceGuys\Addressing\Enum\AddressField;
use CommerceGuys\Addressing\Enum\AdministrativeAreaType;
use CommerceGuys\Addressing\Enum\DependentLocalityType;
use CommerceGuys\Addressing\Enum\LocalityType;
use CommerceGuys\Addressing\Enum\PostalCodeType;
use CommerceGuys\Intl\Country\CountryRepositoryInterface;
use Drupal\address\LabelHelper;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
......@@ -63,18 +60,6 @@ class AddressFormatForm extends EntityForm {
public function form(array $form, FormStateInterface $formState) {
$form = parent::form($form, $formState);
$addressFormat = $this->entity;
$fields = [
AddressField::RECIPIENT => $this->t('Recipient'),
AddressField::ORGANIZATION => $this->t('Organization'),
AddressField::ADDRESS_LINE1 => $this->t('Address line 1'),
AddressField::ADDRESS_LINE2 => $this->t('Address line 2'),
AddressField::SORTING_CODE => $this->t('Sorting code'),
AddressField::POSTAL_CODE => $this->t('Postal code'),
AddressField::DEPENDENT_LOCALITY => $this->t('Dependent locality'),
AddressField::LOCALITY => $this->t('Locality'),
AddressField::ADMINISTRATIVE_AREA => $this->t('Administrative area'),
];
$countryCode = $addressFormat->getCountryCode();
if ($countryCode == 'ZZ') {
$form['countryCode'] = [
......@@ -104,14 +89,14 @@ class AddressFormatForm extends EntityForm {
$form['requiredFields'] = [
'#type' => 'checkboxes',
'#title' => t('Required fields'),
'#options' => $fields,
'#options' => LabelHelper::getGenericFieldLabels(),
'#default_value' => $addressFormat->getRequiredFields(),
];
$form['uppercaseFields'] = [
'#type' => 'checkboxes',
'#title' => t('Uppercase fields'),
'#description' => t('Uppercased on envelopes to faciliate automatic post handling.'),
'#options' => $fields,
'#options' => LabelHelper::getGenericFieldLabels(),
'#default_value' => $addressFormat->getUppercaseFields(),
];
$form['postalCodePattern'] = [
......@@ -132,54 +117,28 @@ class AddressFormatForm extends EntityForm {
'#type' => 'select',
'#title' => $this->t('Postal code type'),
'#default_value' => $addressFormat->getPostalCodeType(),
'#options' => [
PostalCodeType::POSTAL => $this->t('Postal'),
PostalCodeType::ZIP => $this->t('Zip'),
PostalCodeType::PIN => $this->t('Pin'),
],
'#options' => LabelHelper::getPostalCodeLabels(),
'#empty_value' => '',
];
$form['dependentLocalityType'] = [
'#type' => 'select',
'#title' => $this->t('Dependent locality type'),
'#default_value' => $addressFormat->getDependentLocalityType(),
'#options' => [
DependentLocalityType::DISTRICT => $this->t('District'),
DependentLocalityType::NEIGHBORHOOD => $this->t('Neighborhood'),
DependentLocalityType::VILLAGE_TOWNSHIP => $this->t('Village township'),
DependentLocalityType::SUBURB => $this->t('Suburb'),
],
'#options' => LabelHelper::getDependentLocalityLabels(),
'#empty_value' => '',
];
$form['localityType'] = [
'#type' => 'select',
'#title' => $this->t('Locality type'),
'#default_value' => $addressFormat->getLocalityType(),
'#options' => [
LocalityType::CITY => t('City'),
LocalityType::DISTRICT => t('District'),
LocalityType::POST_TOWN => t('Post town'),
],
'#options' => LabelHelper::getLocalityLabels(),
'#empty_value' => '',
];
$form['administrativeAreaType'] = [
'#type' => 'select',
'#title' => $this->t('Administrative area type'),
'#default_value' => $addressFormat->getAdministrativeAreaType(),
'#options' => [
AdministrativeAreaType::AREA => $this->t('Area'),
AdministrativeAreaType::COUNTY => $this->t('County'),
AdministrativeAreaType::DEPARTMENT => $this->t('Department'),
AdministrativeAreaType::DISTRICT => $this->t('District'),
AdministrativeAreaType::DO_SI => $this->t('Do si'),
AdministrativeAreaType::EMIRATE => $this->t('Emirate'),
AdministrativeAreaType::ISLAND => $this->t('Island'),
AdministrativeAreaType::OBLAST => $this->t('Oblast'),
AdministrativeAreaType::PARISH => $this->t('Parish'),
AdministrativeAreaType::PREFECTURE => $this->t('Prefecture'),
AdministrativeAreaType::PROVINCE => $this->t('Province'),
AdministrativeAreaType::STATE => $this->t('State'),
],
'#options' => LabelHelper::getAdministrativeAreaLabels(),
'#empty_value' => '',
];
......
<?php
/**
* @file
* Contains \Drupal\address\LabelHelper.
*/
namespace Drupal\address;
use CommerceGuys\Addressing\Enum\AddressField;
use CommerceGuys\Addressing\Enum\AdministrativeAreaType;
use CommerceGuys\Addressing\Enum\DependentLocalityType;
use CommerceGuys\Addressing\Enum\LocalityType;
use CommerceGuys\Addressing\Enum\PostalCodeType;
use CommerceGuys\Addressing\Model\AddressFormatInterface;
/**
* Provides translated labels for the library enums.
*/
class LabelHelper {
/**
* Gets the field labels suitable for the given address format.
*
* Intended to be shown to the end user, they sometimes use a more familiar
* term than the field name (Company instead of Organization, Contact name
* instead of Recipient, etc).
*
* @param \CommerceGuys\Addressing\Model\AddressFormatInterface $addressFormat
* The address format.
*
* @return string[]
* An array of labels, keyed by field.
*/
public static function getFieldLabels(AddressFormatInterface $addressFormat) {
$administrativeAreaType = $addressFormat->getAdministrativeAreaType();
$localityType = $addressFormat->getLocalityType();
$dependentLocalityType = $addressFormat->getDependentLocalityType();
$postalCodeType = $addressFormat->getPostalCodeType();
return [
AddressField::ADMINISTRATIVE_AREA => self::getAdministrativeAreaLabel($administrativeAreaType),
AddressField::LOCALITY => self::getLocalityLabel($localityType),
AddressField::DEPENDENT_LOCALITY => self::getDependentLocalityLabel($dependentLocalityType),
AddressField::POSTAL_CODE => self::getPostalCodeLabel($postalCodeType),
// Google's library always labels the sorting code field as "Cedex".
AddressField::SORTING_CODE => t('Cedex'),
AddressField::ADDRESS_LINE1 => t('Street address'),
// The address line 2 label is usually shown only to screen-reader users.
AddressField::ADDRESS_LINE2 => t('Street address line 2'),
AddressField::ORGANIZATION => t('Company'),
AddressField::RECIPIENT => t('Contact name'),
];
}
/**
* Gets the generic field labels.
*
* Intended primarily for backend settings screens.
*
* @return string[]
* The field labels, keyed by field.
*/
public static function getGenericFieldLabels() {
return [
AddressField::ADMINISTRATIVE_AREA => t('Administrative area'),
AddressField::LOCALITY => t('Locality'),
AddressField::DEPENDENT_LOCALITY => t('Dependent locality'),
AddressField::POSTAL_CODE => t('Postal code'),
AddressField::SORTING_CODE => t('Sorting code'),
AddressField::ADDRESS_LINE1 => t('Address line 1'),
AddressField::ADDRESS_LINE2 => t('Address line 2'),
AddressField::ORGANIZATION => t('Organization'),
AddressField::RECIPIENT => t('Recipient'),
];
}
/**
* Gets the administrative area label for the given type.
*
* @param string $administrativeAreaType
* The administrative area type.
*
* @return string
* The administrative area label.
*/
public static function getAdministrativeAreaLabel($administrativeAreaType) {
if (!$administrativeAreaType) {
return NULL;
}
AdministrativeAreaType::assertExists($administrativeAreaType);
$labels = self::getAdministrativeAreaLabels();
return $labels[$administrativeAreaType];
}
/**
* Gets all administrative area labels.
*
* @return string[]
* The administrative area labels, keyed by type.
*/
public static function getAdministrativeAreaLabels() {
return [
AdministrativeAreaType::AREA => t('Area'),
AdministrativeAreaType::COUNTY => t('County'),
AdministrativeAreaType::DEPARTMENT => t('Department'),
AdministrativeAreaType::DISTRICT => t('District'),
AdministrativeAreaType::DO_SI => t('Do si'),
AdministrativeAreaType::EMIRATE => t('Emirate'),
AdministrativeAreaType::ISLAND => t('Island'),
AdministrativeAreaType::OBLAST => t('Oblast'),
AdministrativeAreaType::PARISH => t('Parish'),
AdministrativeAreaType::PREFECTURE => t('Prefecture'),
AdministrativeAreaType::PROVINCE => t('Province'),
AdministrativeAreaType::STATE => t('State'),
];
}
/**
* Gets the locality label for the given type.
*
* @param string $localityType
* The locality type.
*
* @return string
* The locality label.
*/
public static function getLocalityLabel($localityType) {
if (!$localityType) {
return NULL;
}
LocalityType::assertExists($localityType);
$labels = self::getLocalityLabels();
return $labels[$localityType];
}
/**
* Gets all locality labels.
*
* @return string[]
* The locality labels, keyed by type.
*/
public static function getLocalityLabels() {
return [
LocalityType::CITY => t('City'),
LocalityType::DISTRICT => t('District'),
LocalityType::POST_TOWN => t('Post town'),
];
}
/**
* Gets the dependent locality label for the given type.
*
* @param string $dependentLocalityType
* The dependent locality type.
*
* @return string
* The dependent locality label.
*/
public static function getDependentLocalityLabel($dependentLocalityType) {
if (!$dependentLocalityType) {
return NULL;
}
DependentLocalityType::assertExists($dependentLocalityType);
$labels = self::getDependentLocalityLabels();
return $labels[$dependentLocalityType];
}
/**
* Gets all dependent locality labels.
*
* @return string[]
* The dependent locality labels, keyed by type.
*/
public static function getDependentLocalityLabels() {
return [
DependentLocalityType::DISTRICT => t('District'),
DependentLocalityType::NEIGHBORHOOD => t('Neighborhood'),
DependentLocalityType::VILLAGE_TOWNSHIP => t('Village township'),
DependentLocalityType::SUBURB => t('Suburb'),
];
}
/**
* Gets the postal code label for the given type.
*
* @param string $postalCodeType
* The postal code type.
*
* @return string
* The postal code label.
*/
public static function getPostalCodeLabel($postalCodeType) {
if (!$postalCodeType) {
return NULL;
}
PostalCodeType::assertExists($postalCodeType);
$labels = self::getPostalCodeLabels();
return $labels[$postalCodeType];
}
/**
* Gets all postal code labels.
*
* @return string[]
* The postal code labels, keyed by type.
*/
public static function getPostalCodeLabels() {
return [
PostalCodeType::POSTAL => t('Postal code'),
PostalCodeType::ZIP => t('Zip code'),
PostalCodeType::PIN => t('Pin code'),
];
}
}
Supports Markdown
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