diff --git a/config/schema/redirect.schema.yml b/config/schema/redirect.schema.yml
index 077f0fa..5dcc9ea 100644
--- a/config/schema/redirect.schema.yml
+++ b/config/schema/redirect.schema.yml
@@ -56,9 +56,9 @@ redirect.domain:
label: 'Redirect domains'
mapping:
domain_redirects:
- type: sequence
+ type: config_object
label: 'Domain redirects'
- sequence:
+ mapping:
from:
type: string
label: 'From domain'
diff --git a/src/Form/RedirectDomainForm.php b/src/Form/RedirectDomainForm.php
index 49698da..ea70b1c 100644
--- a/src/Form/RedirectDomainForm.php
+++ b/src/Form/RedirectDomainForm.php
@@ -10,17 +10,10 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class RedirectDomainForm extends ConfigFormBase {
/**
- * Max additional domain redirects.
- */
- protected $max;
-
-
- /**
* {@inheritdoc}
*/
public function __construct(ConfigFactoryInterface $config_factory) {
parent::__construct($config_factory);
- $this->max = 1;
}
/**
@@ -53,57 +46,52 @@ class RedirectDomainForm extends ConfigFormBase {
*/
public function buildForm(array $form, FormStateInterface $form_state) {
- if ($form_state->getValue('max') !== NULL){
- $this->max = $form_state->getValue('max');
- }
-
- $domain_redirects = \Drupal::config('redirect.domain')->get('domain_redirects');
$form['redirects'] = [
'#type' => 'table',
'#tree' => TRUE,
'#header' => ['From domain', 'To domain'],
+ '#prefix' => '
',
+ '#suffix' => '
',
];
- $rows = [];
- // Shows already added redirects.
- foreach ($domain_redirects as $domain_redirect) {
- $rows[] = [
- 'from' =>[
- 'data' => [
+ $rows = [];
+ // Obtain domain redirects from configuration.
+ if ($domain_redirects = \Drupal::config('redirect.domain')->get('domain_redirects')) {
+ foreach ($domain_redirects as $domain_redirect) {
+ $rows[] = [
+ 'from' => [
'#type' => 'textfield',
'#value' => $domain_redirect['from'],
],
- ],
- 'to' => [
- 'data' => [
+ 'to' => [
'#type' => 'textfield',
'#value' => $domain_redirect['to'],
],
- ],
- ];
+ ];
+ }
}
- // Add text fields for the new domains.
- for ($i = 0; $i < $this->max; $i++) {
+
+ // Add fields for the new redirect domains.
+ for ($i = 0; $i < ($form_state->get('maximum_domains') ?: 1); $i++) {
$rows[] = [
'from' => [
- 'data' => [
- '#type' => 'textfield',
- ],
+ '#type' => 'textfield',
],
'to' => [
- 'data' => [
- '#type' => 'textfield',
- ],
+ '#type' => 'textfield',
],
];
}
- $form['redirects']['#rows'] = $rows;
+ $form['redirects'] += $rows;
$form['add'] = [
'#type' => 'submit',
'#value' => $this->t('Add another'),
'#submit' => ['::addAnotherSubmit'],
- '#ajax' => ['callback' => [$this, 'ajaxAddAnother']],
+ '#ajax' => [
+ 'callback' => '::ajaxAddAnother',
+ 'wrapper' => 'redirect-domain-wrapper',
+ ],
];
$form['submit'] = [
'#type' => 'submit',
@@ -128,12 +116,11 @@ class RedirectDomainForm extends ConfigFormBase {
}
/**
- * Submit callback for adding a new value.
+ * Submit callback for adding a new domain field.
*/
public function addAnotherSubmit(array $form, FormStateInterface $form_state) {
- $this->max = $this->max + 1;
- $form_state->setValue('max', $this->max);
- $form_state->setRebuild();
+ $form_state->set('maximum_domains', ($form_state->get('maximum_domains') ?:1) + 1);
+ $form_state->setRebuild(TRUE);
}
/**
@@ -150,16 +137,19 @@ class RedirectDomainForm extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
$domain_redirects = [];
$domain_config = \Drupal::configFactory()->getEditable('redirect.domain');
- // @todo Go through redirects in the form table and save in configuration.
- /*
- foreach ($form['redirects']['#rows'] as $redirect) {
- $domain_redirects[] = [
- 'from' => $redirect['from']['data']['#value'],
- 'to' => $redirect['to']['data']['#value'],
- ];
+
+ // Go through redirects in the form table and save in configuration.
+ if ($redirects = $form_state->getValues()['redirects']) {
+ foreach ($form_state->getValues()['redirects'] as $redirect) {
+ if (!empty($redirect['from']) && !empty($redirect['to'])) {
+ $domain_redirects[] = [
+ 'from' => $redirect['from'],
+ 'to' => $redirect['to'],
+ ];
+ }
+ }
}
$domain_config->set('domain_redirects', $domain_redirects);
$domain_config->save();
- */
}
}