Commit 3f2425f9 authored by bojanz's avatar bojanz
Browse files

Issue #2610958: Start following the Drupal coding standard again

parent 02c3768e
......@@ -41,14 +41,14 @@ class AddressFormatImporter implements AddressFormatImporterInterface {
/**
* Constructs a AddressFormatImporter object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(EntityManagerInterface $entityManager, LanguageManagerInterface $languageManager) {
$this->storage = $entityManager->getStorage('address_format');
$this->languageManager = $languageManager;
public function __construct(EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) {
$this->storage = $entity_manager->getStorage('address_format');
$this->languageManager = $language_manager;
$this->externalRepository = new AddressFormatRepository();
}
......@@ -56,12 +56,12 @@ class AddressFormatImporter implements AddressFormatImporterInterface {
* {@inheritdoc}
*/
public function importAll() {
$addressFormats = $this->externalRepository->getAll();
$countryCodes = array_keys($addressFormats);
$address_formats = $this->externalRepository->getAll();
$country_codes = array_keys($address_formats);
// It's nicer API-wise to just pass the country codes.
// The external repository maintains a static cache, so the repeated ->get()
// calls have minimal performance impact.
$this->importEntities($countryCodes);
$this->importEntities($country_codes);
if ($this->languageManager->isMultilingual()) {
$languages = $this->languageManager->getLanguages(LanguageInterface::STATE_CONFIGURABLE);
......@@ -72,21 +72,21 @@ class AddressFormatImporter implements AddressFormatImporterInterface {
/**
* {@inheritdoc}
*/
public function importEntities(array $countryCodes) {
foreach ($countryCodes as $countryCode) {
$addressFormat = $this->externalRepository->get($countryCode);
public function importEntities(array $country_codes) {
foreach ($country_codes as $country_code) {
$address_format = $this->externalRepository->get($country_code);
$values = [
'langcode' => 'en',
'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(),
'countryCode' => $address_format->getCountryCode(),
'format' => $address_format->getFormat(),
'requiredFields' => $address_format->getRequiredFields(),
'uppercaseFields' => $address_format->getUppercaseFields(),
'administrativeAreaType' => $address_format->getAdministrativeAreaType(),
'localityType' => $address_format->getLocalityType(),
'dependentLocalityType' => $address_format->getDependentLocalityType(),
'postalCodeType' => $address_format->getPostalCodeType(),
'postalCodePattern' => $address_format->getPostalCodePattern(),
'postalCodePrefix' => $address_format->getPostalCodePrefix(),
];
$entity = $this->storage->create($values);
$entity->trustData()->save();
......@@ -97,16 +97,16 @@ class AddressFormatImporter implements AddressFormatImporterInterface {
* {@inheritdoc}
*/
public function importTranslations(array $langcodes) {
$availableTranslations = $this->getAvailableTranslations();
$availableTranslations = array_intersect_key($availableTranslations, array_flip($langcodes));
foreach ($availableTranslations as $langcode => $countryCodes) {
$addressFormats = $this->storage->loadMultiple($countryCodes);
foreach ($addressFormats as $countryCode => $addressFormat) {
$externalTranslation = $this->externalRepository->get($countryCode, $langcode);
$configName = $addressFormat->getConfigDependencyName();
$configTranslation = $this->languageManager->getLanguageConfigOverride($langcode, $configName);
$configTranslation->set('format', $externalTranslation->getFormat());
$configTranslation->save();
$available_translations = $this->getAvailableTranslations();
$available_translations = array_intersect_key($available_translations, array_flip($langcodes));
foreach ($available_translations as $langcode => $country_codes) {
$address_formats = $this->storage->loadMultiple($country_codes);
foreach ($address_formats as $country_code => $address_format) {
$external_translation = $this->externalRepository->get($country_code, $langcode);
$config_name = $address_format->getConfigDependencyName();
$config_translation = $this->languageManager->getLanguageConfigOverride($langcode, $config_name);
$config_translation->set('format', $external_translation->getFormat());
$config_translation->save();
}
}
}
......
......@@ -20,10 +20,10 @@ interface AddressFormatImporterInterface {
/**
* Imports address formats for the given country codes.
*
* @param array $countryCodes
* @param array $country_codes
* The country codes for which address formats should be imported.
*/
public function importEntities(array $countryCodes);
public function importEntities(array $country_codes);
/**
* Imports translations for the given language codes.
......
......@@ -30,7 +30,6 @@ class AddressFormatListBuilder extends ConfigEntityListBuilder {
*/
public function buildHeader() {
$header['country'] = $this->t('Country');
return $header + parent::buildHeader();
}
......@@ -39,7 +38,6 @@ class AddressFormatListBuilder extends ConfigEntityListBuilder {
*/
public function buildRow(EntityInterface $entity) {
$row['country'] = $entity->label();
return $row + parent::buildRow($entity);
}
......
......@@ -163,8 +163,8 @@ class AddressFormat extends ConfigEntityBase implements AddressFormatInterface {
/**
* {@inheritdoc}
*/
public function setCountryCode($countryCode) {
$this->countryCode = $countryCode;
public function setCountryCode($country_code) {
$this->countryCode = $country_code;
return $this;
}
......@@ -178,9 +178,9 @@ class AddressFormat extends ConfigEntityBase implements AddressFormatInterface {
/**
* {@inheritdoc}
*/
public function setRequiredFields(array $requiredFields) {
AddressField::assertAllExist($requiredFields);
$this->requiredFields = $requiredFields;
public function setRequiredFields(array $required_fields) {
AddressField::assertAllExist($required_fields);
$this->requiredFields = $required_fields;
return $this;
}
......@@ -194,9 +194,9 @@ class AddressFormat extends ConfigEntityBase implements AddressFormatInterface {
/**
* {@inheritdoc}
*/
public function setUppercaseFields(array $uppercaseFields) {
AddressField::assertAllExist($uppercaseFields);
$this->uppercaseFields = $uppercaseFields;
public function setUppercaseFields(array $uppercase_fields) {
AddressField::assertAllExist($uppercase_fields);
$this->uppercaseFields = $uppercase_fields;
return $this;
}
......@@ -210,9 +210,9 @@ class AddressFormat extends ConfigEntityBase implements AddressFormatInterface {
/**
* {@inheritdoc}
*/
public function setAdministrativeAreaType($administrativeAreaType) {
AdministrativeAreaType::assertExists($administrativeAreaType);
$this->administrativeAreaType = $administrativeAreaType;
public function setAdministrativeAreaType($administrative_area_type) {
AdministrativeAreaType::assertExists($administrative_area_type);
$this->administrativeAreaType = $administrative_area_type;
return $this;
}
......@@ -226,9 +226,9 @@ class AddressFormat extends ConfigEntityBase implements AddressFormatInterface {
/**
* {@inheritdoc}
*/
public function setLocalityType($localityType) {
LocalityType::assertExists($localityType);
$this->localityType = $localityType;
public function setLocalityType($locality_type) {
LocalityType::assertExists($locality_type);
$this->localityType = $locality_type;
return $this;
}
......@@ -242,9 +242,9 @@ class AddressFormat extends ConfigEntityBase implements AddressFormatInterface {
/**
* {@inheritdoc}
*/
public function setDependentLocalityType($dependentLocalityType) {
DependentLocalityType::assertExists($dependentLocalityType);
$this->dependentLocalityType = $dependentLocalityType;
public function setDependentLocalityType($dependent_locality_type) {
DependentLocalityType::assertExists($dependent_locality_type);
$this->dependentLocalityType = $dependent_locality_type;
return $this;
}
......@@ -258,9 +258,9 @@ class AddressFormat extends ConfigEntityBase implements AddressFormatInterface {
/**
* {@inheritdoc}
*/
public function setPostalCodeType($postalCodeType) {
PostalCodeType::assertExists($postalCodeType);
$this->postalCodeType = $postalCodeType;
public function setPostalCodeType($postal_code_type) {
PostalCodeType::assertExists($postal_code_type);
$this->postalCodeType = $postal_code_type;
return $this;
}
......@@ -274,8 +274,8 @@ class AddressFormat extends ConfigEntityBase implements AddressFormatInterface {
/**
* {@inheritdoc}
*/
public function setPostalCodePattern($postalCodePattern) {
$this->postalCodePattern = $postalCodePattern;
public function setPostalCodePattern($postal_code_pattern) {
$this->postalCodePattern = $postal_code_pattern;
return $this;
}
......@@ -289,8 +289,8 @@ class AddressFormat extends ConfigEntityBase implements AddressFormatInterface {
/**
* {@inheritdoc}
*/
public function setPostalCodePrefix($postalCodePrefix) {
$this->postalCodePrefix = $postalCodePrefix;
public function setPostalCodePrefix($postal_code_prefix) {
$this->postalCodePrefix = $postal_code_prefix;
return $this;
}
......
......@@ -7,11 +7,117 @@
namespace Drupal\address\Entity;
use CommerceGuys\Addressing\Model\AddressFormatEntityInterface as ExternalAddressFormatInterface;
use CommerceGuys\Addressing\Model\AddressFormatInterface as ExternalAddressFormatInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* Defines the interface for address formats.
*
* The external address format interface contains getters, while this interface
* adds matching setters.
*
* @see \CommerceGuys\Addressing\Model\AddressFormatInterface
*/
interface AddressFormatInterface extends ExternalAddressFormatInterface, ConfigEntityInterface {
/**
* Sets the two-letter country code.
*
* @param string $country_code
* The two-letter country code.
*
* @return $this
*/
public function setCountryCode($country_code);
/**
* Sets the format string.
*
* @param string $format
* The format string.
*
* @return $this
*/
public function setFormat($format);
/**
* Sets the list of required fields.
*
* @param array $required_fields
* An array of address fields.
*
* @return $this
*/
public function setRequiredFields(array $required_fields);
/**
* Sets the list of fields that need to be uppercased.
*
* @param array $uppercase_fields
* An array of address fields.
*
* @return $this
*/
public function setUppercaseFields(array $uppercase_fields);
/**
* Sets the administrative area type.
*
* @param string $administrative_area_type
* The administrative area type.
*
* @return $this
*/
public function setAdministrativeAreaType($administrative_area_type);
/**
* Sets the locality type.
*
* @param string $locality_type
* The locality type.
*
* @return $this
*/
public function setLocalityType($locality_type);
/**
* Sets the dependent locality type.
*
* @param string $dependent_locality_type
* The dependent locality type.
*
* @return $this
*/
public function setDependentLocalityType($dependent_locality_type);
/**
* Sets the postal code type.
*
* @param string $postal_code_type
* The postal code type.
*
* @return $this
*/
public function setPostalCodeType($postal_code_type);
/**
* Sets the postal code pattern.
*
* @param string $postal_code_pattern
* The postal code pattern.
*
* @return $this
*/
public function setPostalCodePattern($postal_code_pattern);
/**
* Sets the postal code prefix.
*
* @param string $postal_code_prefix
* The postal code prefix.
*
* @return $this
*/
public function setPostalCodePrefix($postal_code_prefix);
}
......@@ -170,8 +170,8 @@ class Zone extends ConfigEntityBase implements ZoneInterface {
*/
public function getMembers() {
if (!$this->membersCollection) {
$pluginManager = $this->getZoneMemberPluginManager();
$this->membersCollection = new ZoneMemberPluginCollection($pluginManager, $this->members, $this);
$plugin_manager = $this->getZoneMemberPluginManager();
$this->membersCollection = new ZoneMemberPluginCollection($plugin_manager, $this->members, $this);
$this->membersCollection->sort();
}
return $this->membersCollection;
......
......@@ -37,14 +37,14 @@ class AvailableCountriesEvent extends Event {
/**
* Constructs a new AvailableCountriesEvent object.
*
* @param array $availableCountries
* @param array $available_countries
* The available countries.
* @param \Drupal\Core\Field\FieldDefinitionInterface $fieldDefinition
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition.
*/
public function __construct(array $availableCountries, FieldDefinitionInterface $fieldDefinition) {
$this->availableCountries = $availableCountries;
$this->fieldDefinition = $fieldDefinition;
public function __construct(array $available_countries, FieldDefinitionInterface $field_definition) {
$this->availableCountries = $available_countries;
$this->fieldDefinition = $field_definition;
}
/**
......@@ -60,13 +60,13 @@ class AvailableCountriesEvent extends Event {
/**
* Sets the available countries.
*
* @param array $availableCountries
* @param array $available_countries
* The available countries to set.
*
* @return $this
*/
public function setAvailableCountries(array $availableCountries) {
$this->availableCountries = $availableCountries;
public function setAvailableCountries(array $available_countries) {
$this->availableCountries = $available_countries;
return $this;
}
......
......@@ -35,14 +35,14 @@ class InitialValuesEvent extends Event {
/**
* Constructs a new InitialValuesEvent object.
*
* @param array $initialValues
* @param array $initial_values
* The initial values.
* @param \Drupal\Core\Field\FieldDefinitionInterface $fieldDefinition
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition.
*/
public function __construct(array $initialValues, FieldDefinitionInterface $fieldDefinition) {
$this->initialValues = $initialValues;
$this->fieldDefinition = $fieldDefinition;
public function __construct(array $initial_values, FieldDefinitionInterface $field_definition) {
$this->initialValues = $initial_values;
$this->fieldDefinition = $field_definition;
}
/**
......@@ -58,13 +58,13 @@ class InitialValuesEvent extends Event {
/**
* Sets the initial values.
*
* @param array $initialValues
* @param array $initial_values
* The inital values to set.
*
* @return $this
*/
public function setInitialValues(array $initialValues) {
$this->initialValues = $initialValues;
public function setInitialValues(array $initial_values) {
$this->initialValues = $initial_values;
return $this;
}
......
......@@ -24,7 +24,7 @@ class FieldHelper {
* The property name.
*/
public static function getPropertyName($field) {
$propertyMapping = [
$property_mapping = [
AddressField::ADMINISTRATIVE_AREA => 'administrative_area',
AddressField::LOCALITY => 'locality',
AddressField::DEPENDENT_LOCALITY => 'dependent_locality',
......@@ -36,7 +36,7 @@ class FieldHelper {
AddressField::RECIPIENT => 'recipient',
];
return isset($propertyMapping[$field]) ? $propertyMapping[$field] : NULL;
return isset($property_mapping[$field]) ? $property_mapping[$field] : NULL;
}
/**
......@@ -51,7 +51,7 @@ class FieldHelper {
* The autocomplete attribute.
*/
public static function getAutocompleteAttribute($field) {
$autocompleteMapping = [
$autocomplete_mapping = [
AddressField::ADMINISTRATIVE_AREA => 'address-level1',
AddressField::LOCALITY => 'address-level2',
AddressField::DEPENDENT_LOCALITY => 'address-level3',
......@@ -63,7 +63,7 @@ class FieldHelper {
AddressField::RECIPIENT => 'name',
];
return isset($autocompleteMapping[$field]) ? $autocompleteMapping[$field] : NULL;
return isset($autocomplete_mapping[$field]) ? $autocomplete_mapping[$field] : NULL;
}
}
......@@ -36,32 +36,32 @@ class AddressFormatForm extends EntityForm {
*
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The address format storage.
* @param \CommerceGuys\Intl\Country\CountryRepositoryInterface $countryRepository
* @param \CommerceGuys\Intl\Country\CountryRepositoryInterface $country_repository
* The country repository.
*/
public function __construct(EntityStorageInterface $storage, CountryRepositoryInterface $countryRepository) {
public function __construct(EntityStorageInterface $storage, CountryRepositoryInterface $country_repository) {
$this->storage = $storage;
$this->countryRepository = $countryRepository;
$this->countryRepository = $country_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
/** @var \Drupal\Core\Entity\EntityManagerInterface $entityManager */
$entityManager = $container->get('entity.manager');
/** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
$entity_manager = $container->get('entity.manager');
return new static($entityManager->getStorage('address_format'), $container->get('address.country_repository'));
return new static($entity_manager->getStorage('address_format'), $container->get('address.country_repository'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $formState) {
$form = parent::form($form, $formState);
$addressFormat = $this->entity;
$countryCode = $addressFormat->getCountryCode();
if ($countryCode == 'ZZ') {
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$address_format = $this->entity;
$country_code = $address_format->getCountryCode();
if ($country_code == 'ZZ') {
$form['countryCode'] = [
'#type' => 'item',
'#title' => $this->t('Country'),
......@@ -72,10 +72,10 @@ class AddressFormatForm extends EntityForm {
$form['countryCode'] = [
'#type' => 'select',
'#title' => $this->t('Country'),
'#default_value' => $addressFormat->getCountryCode(),
'#default_value' => $country_code,
'#required' => TRUE,
'#options' => $this->countryRepository->getList(),
'#disabled' => !$addressFormat->isNew(),
'#disabled' => !$address_format->isNew(),
];
}
......@@ -83,61 +83,61 @@ class AddressFormatForm extends EntityForm {
'#type' => 'textarea',
'#title' => $this->t('Format'),
'#description' => $this->t('Available tokens: @tokens', ['@tokens' => implode(', ', AddressField::getTokens())]),
'#default_value' => $addressFormat->getFormat(),
'#default_value' => $address_format->getFormat(),
'#required' => TRUE,
];
$form['requiredFields'] = [
'#type' => 'checkboxes',
'#title' => t('Required fields'),
'#options' => LabelHelper::getGenericFieldLabels(),
'#default_value' => $addressFormat->getRequiredFields(),
'#default_value' => $address_format->getRequiredFields(),
];
$form['uppercaseFields'] = [
'#type' => 'checkboxes',
'#title' => t('Uppercase fields'),
'#description' => t('Uppercased on envelopes to facilitate automatic post handling.'),
'#options' => LabelHelper::getGenericFieldLabels(),
'#default_value' => $addressFormat->getUppercaseFields(),
'#default_value' => $address_format->getUppercaseFields(),
];
$form['postalCodePattern'] = [
'#type' => 'textfield',
'#title' => $this->t('Postal code pattern'),
'#description' => $this->t('Regular expression used to validate postal codes.'),
'#default_value' => $addressFormat->getPostalCodePattern(),
'#default_value' => $address_format->getPostalCodePattern(),
];
$form['postalCodePrefix'] = [
'#type' => 'textfield',
'#title' => $this->t('Postal code prefix'),
'#description' => $this->t('Added to postal codes when formatting an address for international mailing.'),
'#default_value' => $addressFormat->getPostalCodePrefix(),
'#default_value' => $address_format->getPostalCodePrefix(),
'#size' => 5,
];
$form['postalCodeType'] = [
'#type' => 'select',
'#title' => $this->t('Postal code type'),
'#default_value' => $addressFormat->getPostalCodeType(),
'#default_value' => $address_format->getPostalCodeType(),
'#options' => LabelHelper::getPostalCodeLabels(),
'#empty_value' => '',
];
$form['dependentLocalityType'] = [
'#type' => 'select',
'#title' => $this->t('Dependent locality type'),
'#default_value' => $addressFormat->getDependentLocalityType(),
'#default_value' => $address_format->getDependentLocalityType(),
'#options' => LabelHelper::getDependentLocalityLabels(),
'#empty_value' => '',
];
$form['localityType'] = [
'#type' => 'select',
'#title' => $this->t('Locality type'),
'#default_value' => $addressFormat->getLocalityType(),
'#default_value' => $address_format->getLocalityType(),
'#options' => LabelHelper::getLocalityLabels(),
'#empty_value' => '',
];
$form['administrativeAreaType'] = [
'#type' => 'select',
'#title' => $this->t('Administrative area type'),
'#default_value' => $addressFormat->getAdministrativeAreaType(),
'#default_value' => $address_format->getAdministrativeAreaType(),
'#options' => LabelHelper::getAdministrativeAreaLabels(),
'#empty_value' => '',
];
......@@ -148,29 +148,29 @@ class AddressFormatForm extends EntityForm {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $formState) {
parent::validate($form, $formState);
public function validate(array $form, FormStateInterface $form_state) {
parent::validate($form, $form_state);
// Disallow adding an address format for a country that already has one.
if ($this->entity->isNew())