3 namespace Drupal\book\Form;
5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\Core\Form\FormStateInterface;
9 * Configure book settings for this site.
11 class BookSettingsForm extends ConfigFormBase {
16 public function getFormId() {
17 return 'book_admin_settings';
23 protected function getEditableConfigNames() {
24 return ['book.settings'];
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'),
38 '#description' => $this->t('Users with the %outline-perm permission can add all content types.', ['%outline-perm' => $this->t('Administer book outlines')]),
41 $form['book_child_type'] = [
43 '#title' => $this->t('Content type for the <em>Add child page</em> link'),
44 '#default_value' => $config->get('child_type'),
48 $form['array_filter'] = ['#type' => 'value', '#value' => TRUE];
50 return parent::buildForm($form, $form_state);
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')]));
62 parent::validateForm($form, $form_state);
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().
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'))
80 parent::submitForm($form, $form_state);