diff --git a/src/Entity/Currency.php b/src/Entity/Currency.php index cab74db..a6645d0 100644 --- a/src/Entity/Currency.php +++ b/src/Entity/Currency.php @@ -11,7 +11,6 @@ use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\currency\Plugin\Currency\AmountFormatter\AmountFormatterManagerInterface; -use Drupal\currency\Usage; /** * Defines a currency entity class. @@ -124,6 +123,13 @@ class Currency extends ConfigEntityBase implements CurrencyInterface { protected $subunits; /** + * An array of exchange rates for different currency conversions. + * + * @var array + */ + protected $exchangeRates; + + /** * This currency's usages. * * @var \Drupal\currency\UsageInterface[] @@ -141,13 +147,6 @@ class Currency extends ConfigEntityBase implements CurrencyInterface { * {@inheritdoc} */ public function __construct(array $values, $entity_type) { - if (isset($values['usages'])) { - $usages_data = $values['usages']; - $values['usages'] = []; - foreach ($usages_data as $usage_data) { - $values['usages'][] = $usage_data; - } - } parent::__construct($values, $entity_type); } @@ -202,6 +201,21 @@ class Currency extends ConfigEntityBase implements CurrencyInterface { /** * {@inheritdoc} */ + public function getExchangeRates() { + return $this->exchangeRates; + } + /** + * {@inheritdoc} + */ + public function setExchangeRates(array $exchange_rates) { + $this->exchangeRates = $exchange_rates; + + return $this; + } + + /** + * {@inheritdoc} + */ public function setLabel($label) { $this->label = $label; diff --git a/src/Entity/Currency/CurrencyForm.php b/src/Entity/Currency/CurrencyForm.php index acff283..8a14270 100644 --- a/src/Entity/Currency/CurrencyForm.php +++ b/src/Entity/Currency/CurrencyForm.php @@ -80,6 +80,7 @@ class CurrencyForm extends EntityForm { $values = $form_state->getValues(); $currency->setCurrencyCode($values['currency_code']); $currency->setCurrencyNumber($values['currency_number']); + $currency->setExchangeRates(array()); $currency->setLabel($values['label']); $currency->setSign($values['sign']); $currency->setSubunits($values['subunits']); @@ -118,6 +119,8 @@ class CurrencyForm extends EntityForm { '#type' => 'textfield', ); + // @todo add missing UI for exchange rates. + $form['status'] = array( '#default_value' => $currency->status(), '#title' => $this->t('Enabled'), diff --git a/src/Entity/CurrencyInterface.php b/src/Entity/CurrencyInterface.php index 4c47e0b..638022e 100644 --- a/src/Entity/CurrencyInterface.php +++ b/src/Entity/CurrencyInterface.php @@ -151,6 +151,20 @@ interface CurrencyInterface extends ConfigEntityInterface { */ public function getDecimals(); + + /** + * Gets the exchange rates for this currency. + */ + public function getExchangeRates(); + + /** + * Returns the exchange rates for this currency. + * + * @param array $exchange_rates + * An array with exchange rates from one currency to another. + */ + public function setExchangeRates(array $exchange_rates); + /** * Format an amount using this currency and the environment's default currency locale. * pattern. diff --git a/src/Tests/Entity/Currency/CurrencyDeleteFormWebTest.php b/src/Tests/Entity/Currency/CurrencyDeleteFormWebTest.php index 8f19425..c60ec1e 100644 --- a/src/Tests/Entity/Currency/CurrencyDeleteFormWebTest.php +++ b/src/Tests/Entity/Currency/CurrencyDeleteFormWebTest.php @@ -7,6 +7,8 @@ namespace Drupal\currency\Tests\Entity\Currency; +use Drupal\Core\Entity\Entity; +use Drupal\currency\Entity\Currency; use Drupal\simpletest\WebTestBase; /** @@ -22,11 +24,12 @@ class CurrencyDeleteFormWebTest extends WebTestBase { * Tests the form. */ function testForm() { - $user = $this->drupalCreateUser(array('currency.currency.delete')); + $user = $this->drupalCreateUser(['currency.currency.delete', 'currency.currency.view']); $this->drupalLogin($user); - $currency = entity_create('currency', array( + $currency = Currency::create([ 'currencyCode' => 'ABC', - )); + 'exchangeRates' => [], + ]); $currency->save(); $this->drupalPostForm('admin/config/regional/currency/' . $currency->id() . '/delete', array(), t('Delete')); $this->assertFalse((bool) entity_load_unchanged('currency', $currency->id()));