SettingsForm.php in Simple Responsive Table 8
File
src/Form/SettingsForm.php
View source
<?php
namespace Drupal\simple_responsive_table\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SettingsForm extends ConfigFormBase {
protected $moduleHandler;
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
parent::__construct($config_factory);
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('module_handler'));
}
protected function getEditableConfigNames() {
return [
'simple_responsive_table.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('simple_responsive_table.settings');
$form['max_width'] = [
'#type' => 'textfield',
'#title' => $this
->t('Max Width.'),
'#description' => $this
->t('Make table responsive till the screen of size in pixel.'),
'#default_value' => $config
->get('max_width'),
];
$form['enable_admin_page'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable for admin pages'),
'#description' => $this
->t('Make table responsive for admin pages.'),
'#default_value' => $config
->get('enable_admin_page'),
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!is_numeric($form_state
->getValue('max_width'))) {
$form_state
->setErrorByName('max_width', $this
->t('Max width should be number.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->config('simple_responsive_table.settings')
->set('max_width', $form_state
->getValue('max_width'))
->set('enable_admin_page', (bool) $form_state
->getValue('enable_admin_page'))
->save();
parent::submitForm($form, $form_state);
}
public function getFormId() {
return 'simple_responsive_table_settings';
}
}