b9139c12525d29c603183a42f1718badd4988883
[yaffs-website] / Form / FrontPageSettingsForm.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\front_page\Form\FrontPageSettingsForm.
6  */
7
8 namespace Drupal\front_page\Form;
9
10 use Drupal\Core\Database\Database;
11 use Drupal\Core\Form\ConfigFormBase;
12 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\Core\Url;
14
15 /**
16  * Configure site information settings for this site.
17  */
18 class FrontPageSettingsForm extends ConfigFormBase {
19
20   /**
21    * Implements \Drupal\Core\Form\FormInterface::getFormID().
22    */
23   public function getFormID() {
24     return 'front_page_admin';
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function getEditableConfigNames() {
31     return [];
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function buildForm(array $form, FormStateInterface $form_state) {
38
39     $config = \Drupal::configFactory()->get('front_page.settings');
40
41     $form['front_page_enable'] = [
42       '#type' => 'checkbox',
43       '#title' => $this->t('Front Page Override'),
44       '#description' => $this->t('Enable this if you want the front page module to manage the home page.'),
45       '#default_value' => $config->get('enable') ?: false,
46     ];
47
48     // Load any existing settings and build the by redirect by role form.
49     $form['roles'] = [
50       '#tree' => TRUE,
51       '#type' => 'fieldset',
52       '#title' => $this->t('Roles'),
53     ];
54
55     // Build the form for roles.
56     $roles = user_roles();
57
58     // Iterate each role.
59     foreach ($roles as $rid => $role) {
60
61       $role_config = $config->get('rid_' . $rid);
62       $form['roles'][$rid] = [
63         '#type' => 'details',
64         '#open' => FALSE,
65         '#title' => $this->t('Front Page for @rolename', ['@rolename' => $role->label()]),
66       ];
67
68       $form['roles'][$rid]['enabled'] = [
69         '#type' => 'checkbox',
70         '#title' => $this->t('Enable'),
71         '#value' => isset($role_config['enabled']) ? $role_config['enabled'] : false,
72       ];
73
74       $form['roles'][$rid]['weigth'] = [
75         '#type' => 'number',
76         '#title' => $this->t('Weigth'),
77         '#value' => isset($role_config['weigth']) ? $role_config['weigth'] : 0,
78       ];
79
80       $form['roles'][$rid]['path'] = [
81         '#type' => 'textfield',
82         '#title' => $this->t('Path'),
83         '#default_value' => isset($role_config['path']) ? $role_config['path'] : '',
84         '#cols' => 20,
85         '#rows' => 1,
86         '#description' => $this->t('A redirect path can contain a full URL including get parameters and fragment string (eg "/node/51?page=5#anchor").'),
87       ];
88     }
89
90     $form['submit'] = [
91       '#type' => 'submit',
92       '#value' => $this->t('Save Settings'),
93     ];
94
95     return $form;
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function validateForm(array &$form, FormStateInterface $form_state) {
102 //    parent::validateForm($form, $form_state);
103     $rolesList = $form_state->getUserInput()['roles'];
104     if ($rolesList) {
105       foreach ($rolesList as $rid => $role) {
106         if ($role['enabled'] && empty($role['path'])) {
107           $form_state->setErrorByName('roles][' . $rid . '][path', $this->t('You must set the path field for redirect mode.'));
108         }
109       }
110     }
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function submitForm(array &$form, FormStateInterface $form_state) {
117     $config = \Drupal::configFactory()->getEditable('front_page.settings');
118
119     //Set if all config are enabled or not.
120     $config->set('enable', $form_state->getValue('front_page_enable'));
121
122     //Set config by role.
123     $rolesList = $form_state->getUserInput()['roles'];
124      if (is_array($rolesList)) {
125       foreach ($rolesList as $rid => $role) {
126         $config->set('rid_' . $rid, $role);
127       }
128     }
129
130     $config->save();
131     parent::submitForm($form, $form_state);
132   }
133 }