You are here

public function FixedRatesOverview::overview in Currency 8.3

Views the configured fixed rates.

Return value

array A renderable array.

1 string reference to 'FixedRatesOverview::overview'
currency.routing.yml in ./currency.routing.yml
currency.routing.yml

File

src/Controller/FixedRatesOverview.php, line 84

Class

FixedRatesOverview
Provides the overview of fixed exchange rates.

Namespace

Drupal\currency\Controller

Code

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;
}