29590ccdb573d88e1c80dcb5808390b4b59d001d
[yaffs-website] / web / core / modules / book / src / Form / BookSettingsForm.php
1 <?php
2
3 namespace Drupal\book\Form;
4
5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Configure book settings for this site.
10  */
11 class BookSettingsForm extends ConfigFormBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function getFormId() {
17     return 'book_admin_settings';
18   }
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function getEditableConfigNames() {
24     return ['book.settings'];
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   public function buildForm(array $form, FormStateInterface $form_state) {
31     $types = node_type_get_names();
32     $config = $this->config('book.settings');
33     $form['book_allowed_types'] = [
34       '#type' => 'checkboxes',
35       '#title' => $this->t('Content types allowed in book outlines'),
36       '#default_value' => $config->get('allowed_types'),
37       '#options' => $types,
38       '#description' => $this->t('Users with the %outline-perm permission can add all content types.', ['%outline-perm' => $this->t('Administer book outlines')]),
39       '#required' => TRUE,
40     ];
41     $form['book_child_type'] = [
42       '#type' => 'radios',
43       '#title' => $this->t('Content type for the <em>Add child page</em> link'),
44       '#default_value' => $config->get('child_type'),
45       '#options' => $types,
46       '#required' => TRUE,
47     ];
48     $form['array_filter'] = ['#type' => 'value', '#value' => TRUE];
49
50     return parent::buildForm($form, $form_state);
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function validateForm(array &$form, FormStateInterface $form_state) {
57     $child_type = $form_state->getValue('book_child_type');
58     if ($form_state->isValueEmpty(['book_allowed_types', $child_type])) {
59       $form_state->setErrorByName('book_child_type', $this->t('The content type for the %add-child link must be one of those selected as an allowed book outline type.', ['%add-child' => $this->t('Add child page')]));
60     }
61
62     parent::validateForm($form, $form_state);
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function submitForm(array &$form, FormStateInterface $form_state) {
69     $allowed_types = array_filter($form_state->getValue('book_allowed_types'));
70     // We need to save the allowed types in an array ordered by machine_name so
71     // that we can save them in the correct order if node type changes.
72     // @see book_node_type_update().
73     sort($allowed_types);
74     $this->config('book.settings')
75     // Remove unchecked types.
76       ->set('allowed_types', $allowed_types)
77       ->set('child_type', $form_state->getValue('book_child_type'))
78       ->save();
79
80     parent::submitForm($form, $form_state);
81   }
82
83 }