PluginConditionSettingsForm.php in RNG - Events and Registrations 3.x
File
src/Form/PluginConditionSettingsForm.php
View source
<?php
namespace Drupal\rng\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\rng\EventManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\user\Entity\Role;
use Drupal\Core\Session\AccountInterface;
class PluginConditionSettingsForm extends FormBase {
protected $eventManager;
public function __construct(EventManagerInterface $event_manager) {
$this->eventManager = $event_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('rng.event_manager'));
}
public function getFormId() {
return 'rng_plugin_condition_settings';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$all_roles = Role::loadMultiple();
unset($all_roles[AccountInterface::ANONYMOUS_ROLE]);
unset($all_roles[AccountInterface::AUTHENTICATED_ROLE]);
$roles = [];
$values = [];
foreach ($all_roles as $role) {
$roles[$role
->id()] = $role
->label();
if ($role
->getThirdPartySetting('rng', 'condition_rng_role', FALSE)) {
$values[] = $role
->id();
}
}
$form['roles'] = [
'#type' => 'checkboxes',
'#options' => $roles,
'#title' => $this
->t('Roles'),
'#description' => $this
->t('Expose these roles to condition plugin. If no roles are selected, all roles will be made available.'),
'#default_value' => $values,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Save'),
'#button_type' => 'primary',
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state
->getValue('roles') as $role_id => $checked) {
$role = Role::load($role_id);
$role
->setThirdPartySetting('rng', 'condition_rng_role', (bool) $checked)
->save();
}
$this->eventManager
->invalidateEventTypes();
$this
->messenger()
->addMessage(t('Updated condition plugin settings.'));
}
}