5 * Contains \Drupal\front_page\Form\FrontPageSettingsForm.
8 namespace Drupal\front_page\Form;
10 use Drupal\Core\Database\Database;
11 use Drupal\Core\Form\ConfigFormBase;
12 use Drupal\Core\Form\FormStateInterface;
16 * Configure site information settings for this site.
18 class FrontPageSettingsForm extends ConfigFormBase {
21 * Implements \Drupal\Core\Form\FormInterface::getFormID().
23 public function getFormID() {
24 return 'front_page_admin';
30 protected function getEditableConfigNames() {
37 public function buildForm(array $form, FormStateInterface $form_state) {
39 $config = \Drupal::configFactory()->get('front_page.settings');
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,
48 // Load any existing settings and build the by redirect by role form.
51 '#type' => 'fieldset',
52 '#title' => $this->t('Roles'),
55 // Build the form for roles.
56 $roles = user_roles();
59 foreach ($roles as $rid => $role) {
61 $role_config = $config->get('rid_' . $rid);
62 $form['roles'][$rid] = [
65 '#title' => $this->t('Front Page for @rolename', ['@rolename' => $role->label()]),
68 $form['roles'][$rid]['enabled'] = [
69 '#type' => 'checkbox',
70 '#title' => $this->t('Enable'),
71 '#value' => isset($role_config['enabled']) ? $role_config['enabled'] : false,
74 $form['roles'][$rid]['weigth'] = [
76 '#title' => $this->t('Weigth'),
77 '#value' => isset($role_config['weigth']) ? $role_config['weigth'] : 0,
80 $form['roles'][$rid]['path'] = [
81 '#type' => 'textfield',
82 '#title' => $this->t('Path'),
83 '#default_value' => isset($role_config['path']) ? $role_config['path'] : '',
86 '#description' => $this->t('A redirect path can contain a full URL including get parameters and fragment string (eg "/node/51?page=5#anchor").'),
92 '#value' => $this->t('Save Settings'),
101 public function validateForm(array &$form, FormStateInterface $form_state) {
102 // parent::validateForm($form, $form_state);
103 $rolesList = $form_state->getUserInput()['roles'];
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.'));
116 public function submitForm(array &$form, FormStateInterface $form_state) {
117 $config = \Drupal::configFactory()->getEditable('front_page.settings');
119 //Set if all config are enabled or not.
120 $config->set('enable', $form_state->getValue('front_page_enable'));
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);
131 parent::submitForm($form, $form_state);