/** * Views the configured fixed rates. * * @return array * A renderable array. */ public function overview() { /** @var \Drupal\currency\Plugin\Currency\ExchangeRateProvider\FixedRates $plugin */ $plugin = $this->currencyExchangeRateProviderManager->createInstance('currency_fixed_rates'); $rates = $plugin->loadALl(); $form['rates'] = array('#empty' => $this->t('There are no exchange rates yet. <a href="@path">Add an exchange rate</a>.', array('@path' => $this->urlGenerator->generateFromRoute('currency.exchange_rate_provider.fixed_rates.add'))), '#header' => array($this->t('From'), $this->t('To'), $this->t('Exchange rate'), $this->t('Operations')), '#type' => 'table'); foreach ($rates as $currency_code_from => $currency_codes_to) { foreach ($currency_codes_to as $currency_code_to => $rate) { $currency_from = $this->currencyStorage->load($currency_code_from); $currency_to = $this->currencyStorage->load($currency_code_to); if ($currency_from && $currency_to) { $row['currency_from'] = array('#markup' => $currency_from->label(), '#type' => 'item'); $row['currency_to'] = array('#markup' => $currency_to->label(), '#type' => 'item'); $row['rate'] = array('#markup' => $this->currencyAmountFormatterManager->getDefaultPlugin()->formatAmount($currency_to, $rate), '#type' => 'item'); $row['operations'] = array('#links' => array(array('title' => $this->t('edit'), 'route_name' => 'currency.exchange_rate_provider.fixed_rates.edit', 'route_parameters' => array('currency_code_from' => $currency_code_from, 'currency_code_to' => $currency_code_to))), '#type' => 'operations'); $form['rates'][] = $row; } } } return $form; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { /** @var \Drupal\currency\Plugin\Currency\ExchangeRateProvider\FixedRates $plugin */ $plugin = $this->currencyExchangeRateProviderManager->createInstance('currency_fixed_rates'); $values = $form_state->getValues(); $currency_code_from = $values['currency_code_from']; $currency_code_to = $values['currency_code_to']; $currency_from = $this->currencyStorage->load($currency_code_from); $currency_to = $this->currencyStorage->load($currency_code_to); $triggering_element = $form_state->getTriggeringElement(); switch ($triggering_element['#name']) { case 'save': $plugin->save($currency_code_from, $currency_code_to, $values['rate']['amount']); drupal_set_message($this->t('The exchange rate for @currency_title_from to @currency_title_to has been saved.', array('@currency_title_from' => $currency_from->label(), '@currency_title_to' => $currency_to->label()))); break; case 'delete': $plugin->delete($currency_code_from, $currency_code_to); drupal_set_message($this->t('The exchange rate for @currency_title_from to @currency_title_to has been deleted.', array('@currency_title_from' => $currency_from->label(), '@currency_title_to' => $currency_to->label()))); break; } $form_state->setRedirect('currency.exchange_rate_provider.fixed_rates.overview'); }