View source
<?php
namespace Drupal\accordion_menus\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class AccordionMenusConfigForm extends ConfigFormBase {
const SETTINGS = 'accordion_menus.settings';
public function getFormId() {
return 'accordion_menus_config_form';
}
protected function getEditableConfigNames() {
return [
static::SETTINGS,
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config(static::SETTINGS);
$menus = menu_ui_get_menus();
$form['accordion_menus'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Accordion Menus'),
'#options' => $menus,
'#description' => $this
->t('Select menu to make them accordion menu.'),
'#default_value' => !empty($config
->get('accordion_menus')) ? $config
->get('accordion_menus') : [],
];
$form['accordion_advanced'] = [
'#type' => 'details',
'#title' => $this
->t('Advanced settings'),
'#open' => FALSE,
];
$form['accordion_advanced']['accordion_menus_no_submenus'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Accordion without sub menu item'),
'#options' => $menus,
'#description' => $this
->t('Menus which have no sub menu item, will show also in accordion menu.'),
'#default_value' => !empty($config
->get('accordion_menus_no_submenus')) ? $config
->get('accordion_menus_no_submenus') : [],
];
$form['accordion_advanced']['accordion_menus_default_closed'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Accordion menu closed by default'),
'#options' => $menus,
'#description' => $this
->t('Allow all trees closed at beginning.'),
'#default_value' => !empty($config
->get('accordion_menus_default_closed')) ? $config
->get('accordion_menus_default_closed') : [],
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->configFactory
->getEditable(static::SETTINGS)
->set('accordion_menus', $form_state
->getValue('accordion_menus'))
->set('accordion_menus_no_submenus', $form_state
->getValue('accordion_menus_no_submenus'))
->set('accordion_menus_default_closed', $form_state
->getValue('accordion_menus_default_closed'))
->save();
parent::submitForm($form, $form_state);
drupal_flush_all_caches();
}
}