Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / redirect / modules / redirect_domain / src / Form / RedirectDomainForm.php
1 <?php
2
3 namespace Drupal\redirect_domain\Form;
4
5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Provides a redirect domain configuration form.
10  */
11 class RedirectDomainForm extends ConfigFormBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function getFormId() {
17     return 'form_redirect_domain_form';
18   }
19
20   /**
21   * {@inheritdoc}
22   */
23   protected function getEditableConfigNames() {
24     return [
25       'redirect_domain.domains',
26     ];
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function buildForm(array $form, FormStateInterface $form_state) {
33     if (!$form_state->has('maximum_domains')) {
34       $form_state->set('maximum_domains', 1);
35     }
36
37     $form['redirects'] = [
38       '#type' => 'table',
39       '#tree' => TRUE,
40       '#header' => [
41         $this->t('From domain'),
42         $this->t('Sub path'),
43         $this->t('Destination')
44       ],
45       '#prefix' => '<div id="redirect-domain-wrapper">',
46       '#suffix' => '</div>',
47     ];
48
49     $rows = [];
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'][] = [
55             'from' => [
56               '#type' => 'textfield',
57               '#value' => str_replace(':','.',$key),
58             ],
59             'sub_path' => [
60               '#type' => 'textfield',
61               '#value' => $item['sub_path'],
62             ],
63             'destination' => [
64               '#type' => 'textfield',
65               '#value' => $item['destination'],
66             ],
67           ];
68         }
69       }
70     }
71
72     // Fields for the new domain redirects.
73     for ($i = 0; $i < $form_state->get('maximum_domains'); $i++) {
74       $form['redirects'][] = [
75         'from' => [
76           '#type' => 'textfield',
77         ],
78         'sub_path' => [
79           '#type' => 'textfield',
80           '#value' => '/',
81         ],
82         'destination' => [
83           '#type' => 'textfield',
84         ],
85       ];
86     }
87
88     $form['add'] = [
89       '#type' => 'submit',
90       '#value' => $this->t('Add another'),
91       '#submit' => ['::addAnotherSubmit'],
92       '#ajax' => [
93         'callback' => '::ajaxAddAnother',
94         'wrapper' => 'redirect-domain-wrapper',
95       ],
96     ];
97     $form['submit'] = [
98       '#type' => 'submit',
99       '#button_type' => 'primary',
100       '#value' => $this->t('Save'),
101     ];
102     return $form;
103   }
104
105   /**
106    * Ajax callback for adding another domain redirect.
107    *
108    * @param array $form
109    *   The form structure.
110    * @param \Drupal\Core\Form\FormStateInterface $form_state
111    *   The form state.
112    *
113    * @return array
114    *   The new domain redirect form part.
115    */
116   public function ajaxAddAnother(array $form, FormStateInterface $form_state) {
117     return $form['redirects'];
118   }
119
120   /**
121    * Submit callback for adding a new domain field.
122    */
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);
126   }
127
128   /**
129    * {@inheritdoc}
130    */
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.'));
137         }
138       }
139     }
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   public function submitForm(array &$form, FormStateInterface $form_state) {
146     $domain_redirects = [];
147     $domain_config = $this->config('redirect_domain.domains');
148
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']
157           ];
158         }
159       }
160     }
161     $domain_config->set('domain_redirects', $domain_redirects);
162     $domain_config->save();
163     drupal_set_message($this->t('The domain redirects have been saved.'));
164   }
165 }