Skip to content

Commit 2ba01d1

Browse files
cbildsteinbojanz
authored andcommitted
Issue #2893967 by cbildstein, acromel, bojanz, AaronChristian, rszrama: Improve the UX of importing / creating currencies
1 parent 755924e commit 2ba01d1

7 files changed

Lines changed: 43 additions & 22 deletions

File tree

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
entity.commerce_currency.add_form:
2-
route_name: entity.commerce_currency.add_form
1+
entity.commerce_currency.import:
2+
route_name: entity.commerce_currency.import
33
title: 'Add currency'
44
appears_on:
55
- entity.commerce_currency.collection
66

7-
entity.commerce_currency.import:
8-
route_name: entity.commerce_currency.import
9-
title: 'Import currencies'
7+
entity.commerce_currency.add_form:
8+
route_name: entity.commerce_currency.add_form
9+
title: 'Add custom currency'
1010
appears_on:
1111
- entity.commerce_currency.collection
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
# The import URL and title use the "Add" verb to better match user expectations.
12
entity.commerce_currency.import:
2-
path: '/admin/commerce/config/currencies/import'
3+
path: '/admin/commerce/config/currencies/add'
34
defaults:
45
_form: '\Drupal\commerce_price\Form\CurrencyImportForm'
5-
_title: 'Import currencies'
6+
_title: 'Add currency'
67
requirements:
78
_permission: 'administer commerce_currency'
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Drupal\commerce_price;
4+
5+
use Drupal\Core\Entity\EntityTypeInterface;
6+
use Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider;
7+
8+
/**
9+
* Provides routes for the Currency entity.
10+
*/
11+
class CurrencyRouteProvider extends DefaultHtmlRouteProvider {
12+
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
protected function getAddFormRoute(EntityTypeInterface $entity_type) {
17+
$route = parent::getAddFormRoute($entity_type);
18+
// Replace the "Add currency" title with "Add custom currency".
19+
// The t() function is used to ensure the string is picked up for
20+
// translation, even though _title is supposed to be untranslated.
21+
$route->setDefault('_title_callback', '');
22+
$route->setDefault('_title', t('Add custom currency')->getUntranslatedString());
23+
24+
return $route;
25+
}
26+
27+
}

modules/price/src/Entity/Currency.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* "delete" = "Drupal\Core\Entity\EntityDeleteForm"
2525
* },
2626
* "route_provider" = {
27-
* "default" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
27+
* "default" = "Drupal\commerce_price\CurrencyRouteProvider",
2828
* },
2929
* "list_builder" = "Drupal\commerce_price\CurrencyListBuilder",
3030
* },
@@ -43,7 +43,7 @@
4343
* "fractionDigits"
4444
* },
4545
* links = {
46-
* "add-form" = "/admin/commerce/config/currencies/add",
46+
* "add-form" = "/admin/commerce/config/currencies/add-custom",
4747
* "edit-form" = "/admin/commerce/config/currencies/{commerce_currency}",
4848
* "delete-form" = "/admin/commerce/config/currencies/{commerce_currency}/delete",
4949
* "collection" = "/admin/commerce/config/currencies"

modules/price/src/Form/CurrencyForm.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ public function form(array $form, FormStateInterface $form_state) {
4545
/** @var \Drupal\commerce_price\Entity\CurrencyInterface $currency */
4646
$currency = $this->entity;
4747

48-
$import_url = Url::fromRoute('entity.commerce_currency.import')->toString();
49-
$form['message'] = [
50-
'#type' => 'markup',
51-
'#markup' => $this->t('This form is only intended for creating custom currencies. Real-world currencies should be <a href=":url">imported</a>.', [':url' => $import_url]),
52-
];
53-
5448
$form['name'] = [
5549
'#type' => 'textfield',
5650
'#title' => $this->t('Name'),

modules/price/src/Form/CurrencyImportForm.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
5656
else {
5757
$form['currency_codes'] = [
5858
'#type' => 'select',
59-
'#title' => $this->t('Currencies'),
60-
'#required' => TRUE,
59+
'#title' => $this->t('Available currencies'),
6160
'#options' => $currencies,
6261
'#multiple' => TRUE,
6362
'#size' => 10,
@@ -67,7 +66,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
6766
'#type' => 'submit',
6867
'#button_type' => 'primary',
6968
'#name' => 'import',
70-
'#value' => $this->t('Import'),
69+
'#value' => $this->t('Add'),
7170
'#submit' => ['::submitForm'],
7271
];
7372
}

modules/price/tests/src/Functional/CurrencyTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ class CurrencyTest extends CommerceBrowserTestBase {
1717
* Tests importing a currency.
1818
*/
1919
public function testCurrencyImport() {
20-
$this->drupalGet('admin/commerce/config/currencies/import');
20+
$this->drupalGet('admin/commerce/config/currencies/add');
2121
$edit = [
2222
'currency_codes[]' => ['CHF'],
2323
];
24-
$this->submitForm($edit, 'Import');
24+
$this->submitForm($edit, 'Add');
2525

2626
$url = Url::fromRoute('entity.commerce_currency.collection');
2727
$this->assertEquals($this->getUrl(), $this->getAbsoluteUrl($url->toString()));
@@ -35,11 +35,11 @@ public function testCurrencyImport() {
3535
}
3636

3737
/**
38-
* Tests creating a currency.
38+
* Tests adding a currency.
3939
*/
4040
public function testCurrencyCreation() {
4141
$this->drupalGet('admin/commerce/config/currencies');
42-
$this->getSession()->getPage()->clickLink('Add currency');
42+
$this->getSession()->getPage()->clickLink('Add custom currency');
4343
$edit = [
4444
'name' => 'Test currency',
4545
'currencyCode' => 'XXX',

0 commit comments

Comments
 (0)