SettingsForm.php in Simplifying 8
File
src/Form/SettingsForm.php
View source
<?php
namespace Drupal\simplifying\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\simplifying\Services\Toolbar;
use Drupal\simplifying\Services\EntityFields;
use Drupal\simplifying\Services\SettingsActions;
class SettingsForm extends FormBase {
protected $toolbar;
protected $entityfields;
protected $settingsactions;
public function __construct(Toolbar $toolbar, EntityFields $entityfields, SettingsActions $settingsactions) {
$this->toolbar = $toolbar;
$this->entityfields = $entityfields;
$this->settingsactions = $settingsactions;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('simplifying.toolbar'), $container
->get('simplifying.entity'), $container
->get('simplifying.settings'));
}
public function getFormId() {
return 'simplifying_settings_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form_id = 'simplifying_settings_form_wrapper';
$form['#prefix'] = '<div id="' . $form_id . '">';
$form['#suffix'] = '</div>';
$form['tabs'] = [
'#type' => 'vertical_tabs',
];
$this->toolbar
->formFields($form, $form_state);
$this->entityfields
->formFields($form, $form_state);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$menu_links = $form_state
->getValue('menu_links');
$menu_links = array_filter($menu_links);
$this->settingsactions
->setSettings('menu_links', array_keys($menu_links));
$entity_fields = $form_state
->getValue('entity_fields');
$entity_fields = array_map('array_filter', $entity_fields);
$this->settingsactions
->setSettings('entity_fields', $entity_fields);
$toolbar_tabs = $form_state
->getValue('toolbar_tabs');
$this->settingsactions
->setSettings('toolbar_tabs', $toolbar_tabs);
$design = $form_state
->getValue('design');
$this->settingsactions
->setSettings('design', $design);
$this
->messenger()
->addStatus($this
->t('Settings saved successfully'));
}
}