3 namespace Drupal\redirect_domain\Form;
5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\Core\Form\FormStateInterface;
9 * Provides a redirect domain configuration form.
11 class RedirectDomainForm extends ConfigFormBase {
16 public function getFormId() {
17 return 'form_redirect_domain_form';
23 protected function getEditableConfigNames() {
25 'redirect_domain.domains',
32 public function buildForm(array $form, FormStateInterface $form_state) {
33 if (!$form_state->has('maximum_domains')) {
34 $form_state->set('maximum_domains', 1);
37 $form['redirects'] = [
41 $this->t('From domain'),
43 $this->t('Destination')
45 '#prefix' => '<div id="redirect-domain-wrapper">',
46 '#suffix' => '</div>',
50 // Obtain domain redirects from configuration.
51 if ($domain_redirects = $this->config('redirect_domain.domains')->get('domain_redirects')) {
52 foreach ($domain_redirects as $key => $value) {
53 foreach ($value as $item) {
54 $form['redirects'][] = [
56 '#type' => 'textfield',
57 '#value' => str_replace(':','.',$key),
60 '#type' => 'textfield',
61 '#value' => $item['sub_path'],
64 '#type' => 'textfield',
65 '#value' => $item['destination'],
72 // Fields for the new domain redirects.
73 for ($i = 0; $i < $form_state->get('maximum_domains'); $i++) {
74 $form['redirects'][] = [
76 '#type' => 'textfield',
79 '#type' => 'textfield',
83 '#type' => 'textfield',
90 '#value' => $this->t('Add another'),
91 '#submit' => ['::addAnotherSubmit'],
93 'callback' => '::ajaxAddAnother',
94 'wrapper' => 'redirect-domain-wrapper',
99 '#button_type' => 'primary',
100 '#value' => $this->t('Save'),
106 * Ajax callback for adding another domain redirect.
109 * The form structure.
110 * @param \Drupal\Core\Form\FormStateInterface $form_state
114 * The new domain redirect form part.
116 public function ajaxAddAnother(array $form, FormStateInterface $form_state) {
117 return $form['redirects'];
121 * Submit callback for adding a new domain field.
123 public function addAnotherSubmit(array $form, FormStateInterface $form_state) {
124 $form_state->set('maximum_domains', $form_state->get('maximum_domains') + 1);
125 $form_state->setRebuild(TRUE);
131 public function validateForm(array &$form, FormStateInterface $form_state) {
132 parent::validateForm($form, $form_state);
133 if ($redirects = $form_state->getValue('redirects')) {
134 foreach ($redirects as $redirect) {
135 if (strpos($redirect['from'], '://') !== FALSE) {
136 $form_state->setErrorByName('redirects', $this->t('No protocol should be included in the redirect domain.'));
145 public function submitForm(array &$form, FormStateInterface $form_state) {
146 $domain_redirects = [];
147 $domain_config = $this->config('redirect_domain.domains');
149 if ($redirects = $form_state->getValue('redirects')) {
150 foreach ($redirects as $redirect) {
151 if (!empty($redirect['from']) && !empty($redirect['destination'])) {
152 // Replace '.' with ':' for an eligible key.
153 $redirect['from'] = str_replace('.', ':', $redirect['from']);
154 $domain_redirects[$redirect['from']][] = [
155 'sub_path' => '/' . ltrim($redirect['sub_path'], '/'),
156 'destination' => $redirect['destination']
161 $domain_config->set('domain_redirects', $domain_redirects);
162 $domain_config->save();
163 drupal_set_message($this->t('The domain redirects have been saved.'));