SettingsForm.php in Lightning Core 8.4
File
modules/lightning_roles/src/Form/SettingsForm.php
View source
<?php
namespace Drupal\lightning_roles\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SettingsForm extends ConfigFormBase {
public function __construct(ConfigFactoryInterface $config_factory, TranslationInterface $translator) {
parent::__construct($config_factory);
$this
->setStringTranslation($translator);
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('string_translation'));
}
protected function getEditableConfigNames() {
return [
'lightning_roles.settings',
];
}
public function getFormId() {
return 'lightning_roles_settings_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('lightning_roles.settings')
->get('content_roles');
$permission_map = function ($permission) {
return str_replace('?', NULL, $permission);
};
foreach ($config as $key => $role) {
$role['permissions'] = array_map($permission_map, $role['permissions']);
$form['content_roles'][$key] = [
'#type' => 'checkbox',
'#title' => $this
->t('Provide @label role for new content types', [
'@label' => $permission_map($role['label']),
]),
'#default_value' => $role['enabled'],
'#description' => $this
->t('Gives permission to @permissions', [
'@permissions' => implode(', ', $role['permissions']),
]),
];
}
$form['content_roles']['#tree'] = TRUE;
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('lightning_roles.settings');
$roles = $config
->get('content_roles');
foreach ($form_state
->getValue('content_roles') as $key => $value) {
$roles[$key]['enabled'] = (bool) $value;
}
$config
->set('content_roles', $roles)
->save();
parent::submitForm($form, $form_state);
}
}
Classes
Name |
Description |
SettingsForm |
The settings form for controlling Lightning Roles' behavior. |