Commit d7783513 authored by bojanz's avatar bojanz

Clean up commerce_price, use currencyCode instead of currency_code.

parent 65eb63af
......@@ -5,7 +5,6 @@
* Defines the Currency entity and the Price field.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Core\Language\LanguageInterface;
......@@ -31,9 +30,8 @@ function commerce_price_configurable_language_insert(ConfigurableLanguage $langu
// Ignore 'und' and 'zxx'.
if (!($language->getId() === LanguageInterface::LANGCODE_NOT_SPECIFIED || $language->getId() === LanguageInterface::LANGCODE_NOT_APPLICABLE)) {
// Import currencies.
$currency_importer = \Drupal::service('commerce_price.currency_importer');
$importer = \Drupal::service('commerce_price.currency_importer');
$currencies = \Drupal\commerce_price\Entity\Currency::loadMultiple();
$currency_importer->importCurrencyTranslations($currencies, [$language]);
$importer->importCurrencyTranslations($currencies, [$language]);
}
}
......@@ -2,7 +2,7 @@ commerce_price.commerce_currency.*:
type: config_entity
label: 'Currency'
mapping:
currency_code:
currencyCode:
type: string
label: 'Currency code'
name:
......
......@@ -39,7 +39,7 @@ class CurrencyImporter implements CurrencyImporterInterface {
protected $languageManager;
/**
* Constructs a new CurrencyImporter.
* Creates a new CurrencyImporter object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The entity manager.
......@@ -73,16 +73,16 @@ class CurrencyImporter implements CurrencyImporterInterface {
/**
* {@inheritdoc}
*/
public function importCurrency($currency_code) {
if ($this->currencyStorage->load($currency_code)) {
public function importCurrency($currencyCode) {
if ($this->currencyStorage->load($currencyCode)) {
return FALSE;
}
$language = $this->languageManager->getDefaultLanguage();
$currency = $this->getCurrency($currency_code, $language, CurrencyImporterInterface::FALLBACK_LANGUAGE);
$currency = $this->getCurrency($currencyCode, $language, CurrencyImporterInterface::FALLBACK_LANGUAGE);
if ($currency) {
$values = [
'currency_code' => $currency->getCurrencyCode(),
'currencyCode' => $currency->getCurrencyCode(),
'name' => $currency->getName(),
'numericCode' => $currency->getNumericCode(),
'symbol' => $currency->getSymbol(),
......@@ -131,7 +131,7 @@ class CurrencyImporter implements CurrencyImporterInterface {
/**
* Get a single currency.
*
* @param string $currency_code
* @param string $currencyCode
* The currency code.
* @param \Drupal\Core\Language\LanguageInterface $language
* The language.
......@@ -142,9 +142,9 @@ class CurrencyImporter implements CurrencyImporterInterface {
* Returns \CommerceGuys\Intl\Currency\Currency or
* false when a exception has occurred.
*/
protected function getCurrency($currency_code, LanguageInterface $language, $fallback = NULL) {
protected function getCurrency($currencyCode, LanguageInterface $language, $fallback = NULL) {
try {
$currency = $this->currencyRepository->get($currency_code, $language->getId(), $fallback);
$currency = $this->currencyRepository->get($currencyCode, $language->getId(), $fallback);
}
catch (UnknownLocaleException $e) {
return FALSE;
......
......@@ -31,13 +31,13 @@ interface CurrencyImporterInterface {
/**
* Creates a new currency object for the given currency code.
*
* @param string $currency_code
* @param string $currencyCode
* The currency code.
*
* @return \Drupal\commerce_price\Entity\Currency | bool
* The new currency or false if the currency is already imported.
*/
public function importCurrency($currency_code);
public function importCurrency($currencyCode);
/**
* Imports translations for the currency entity.
......
......@@ -19,9 +19,12 @@ class CurrencyListBuilder extends ConfigEntityListBuilder {
* {@inheritdoc}
*/
public function buildHeader() {
$header['name'] = $this->t('Name');
$header['currency_code'] = $this->t('Currency code');
$header['status'] = $this->t('Status');
$header = [
'name' => $this->t('Name'),
'currencyCode' => $this->t('Currency code'),
'status' => $this->t('Status'),
];
return $header + parent::buildHeader();
}
......@@ -29,9 +32,12 @@ class CurrencyListBuilder extends ConfigEntityListBuilder {
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['name'] = $this->getLabel($entity);
$row['currency_code'] = $entity->id();
$row['status'] = $entity->status() ? t('Enabled') : t('Disabled');
$row = [
'name' => $this->getLabel($entity),
'currencyCode' => $entity->id(),
'status' => $entity->status() ? $this->t('Enabled') : $this->t('Disabled'),
];
return $row + parent::buildRow($entity);
}
......
......@@ -27,13 +27,13 @@ use Drupal\Core\Config\Entity\ConfigEntityBase;
* admin_permission = "administer stores",
* config_prefix = "commerce_currency",
* entity_keys = {
* "id" = "currency_code",
* "id" = "currencyCode",
* "label" = "name",
* "uuid" = "uuid",
* "status" = "status"
* },
* config_export = {
* "currency_code",
* "currencyCode",
* "name",
* "numericCode",
* "symbol",
......@@ -54,7 +54,7 @@ class Currency extends ConfigEntityBase implements CurrencyInterface {
*
* @var string
*/
protected $currency_code;
protected $currencyCode;
/**
* The currency name.
......@@ -88,22 +88,21 @@ class Currency extends ConfigEntityBase implements CurrencyInterface {
* Overrides \Drupal\Core\Entity\Entity::id().
*/
public function id() {
return $this->getCurrencyCode();
return $this->currencyCode;
}
/**
* {@inheritdoc}
*/
public function getCurrencyCode() {
return $this->currency_code;
return $this->currencyCode;
}
/**
* {@inheritdoc}
*/
public function setCurrencyCode($currency_code) {
$this->currency_code = $currency_code;
public function setCurrencyCode($currencyCode) {
$this->currencyCode = $currencyCode;
return $this;
}
......@@ -119,7 +118,6 @@ class Currency extends ConfigEntityBase implements CurrencyInterface {
*/
public function setName($name) {
$this->name = $name;
return $this;
}
......@@ -135,7 +133,6 @@ class Currency extends ConfigEntityBase implements CurrencyInterface {
*/
public function setNumericCode($numericCode) {
$this->numericCode = $numericCode;
return $this;
}
......@@ -151,7 +148,6 @@ class Currency extends ConfigEntityBase implements CurrencyInterface {
*/
public function setSymbol($symbol) {
$this->symbol = $symbol;
return $this;
}
......@@ -167,7 +163,6 @@ class Currency extends ConfigEntityBase implements CurrencyInterface {
*/
public function setFractionDigits($fractionDigits) {
$this->fractionDigits = $fractionDigits;
return $this;
}
......
......@@ -22,7 +22,7 @@ class CurrencyForm extends EntityForm {
protected $currencyStorage;
/**
* Creates a CurrencyForm instance.
* Creates a new CurrencyForm object.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $currencyStorage
* The currency storage.
......@@ -55,7 +55,7 @@ class CurrencyForm extends EntityForm {
'#maxlength' => 255,
'#required' => TRUE,
];
$form['currency_code'] = [
$form['currencyCode'] = [
'#type' => 'textfield',
'#title' => $this->t('Currency code'),
'#default_value' => $currency->getCurrencyCode(),
......@@ -107,12 +107,12 @@ class CurrencyForm extends EntityForm {
*/
public function validateCurrencyCode(array $element, FormStateInterface &$form_state, array $form) {
$currency = $this->getEntity();
$currency_code = $element['#value'];
if (!preg_match('/^[A-Z]{3}$/', $currency_code)) {
$currencyCode = $element['#value'];
if (!preg_match('/^[A-Z]{3}$/', $currencyCode)) {
$form_state->setError($element, $this->t('The currency code must consist of three uppercase letters.'));
}
elseif ($currency->isNew()) {
$loadedCurrency = $this->currencyStorage->load($currency_code);
$loadedCurrency = $this->currencyStorage->load($currencyCode);
if ($loadedCurrency) {
$form_state->setError($element, $this->t('The currency code is already in use.'));
}
......
......@@ -42,7 +42,7 @@ class CurrencyImporterForm extends FormBase {
];
}
else {
$form['currency_code'] = [
$form['currencyCode'] = [
'#type' => 'select',
'#title' => $this->t('Currency code'),
'#description' => $this->t('Please select the currency you would like to import.'),
......@@ -80,8 +80,8 @@ class CurrencyImporterForm extends FormBase {
*/
public function getCurrencyOptions(array $currencies) {
$options = [];
foreach ($currencies as $currency_code => $currency) {
$options[$currency_code] = $currency->getName();
foreach ($currencies as $currencyCode => $currency) {
$options[$currencyCode] = $currency->getName();
}
asort($options);
......@@ -94,7 +94,7 @@ class CurrencyImporterForm extends FormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
$currency = $this->currencyImporter->importCurrency(
$values['currency_code']
$values['currencyCode']
);
try {
......
Markdown is supported
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