abstract class ProtectionRuleFormBase in User protect 8
Provides a base form controller for a protection rule.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\userprotect\Form\ProtectionRuleFormBase
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of ProtectionRuleFormBase
File
- src/
Form/ ProtectionRuleFormBase.php, line 15
Namespace
Drupal\userprotect\FormView source
abstract class ProtectionRuleFormBase extends EntityForm {
/**
* The protection rule entity storage controller.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $protectionRuleStorage;
/**
* The user storage.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userStorage;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a base class for protection rule add and edit forms.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $protection_rule_storage
* The protection rule entity storage controller.
* @param \Drupal\user\UserStorageInterface $user_storage
* The user storage.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(EntityStorageInterface $protection_rule_storage, UserStorageInterface $user_storage, MessengerInterface $messenger) {
$this->protectionRuleStorage = $protection_rule_storage;
$this->userStorage = $user_storage;
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('userprotect_rule'), $container
->get('entity_type.manager')
->getStorage('user'), $container
->get('messenger'));
}
/**
* Construct message for bypass permission.
*/
protected function getBypassMessage($permission, $permission_label) {
$message = $this
->t('Users with the permission "%permission".', [
'%permission' => $permission_label,
]);
$roles = user_role_names(FALSE, $permission);
if (count($roles)) {
$message .= '<br /><div class="description">' . $this
->t('Currently the following roles have this permission: %roles.', [
'%roles' => implode(', ', $roles),
]) . '</div>';
}
else {
$message .= '<br /><div class="description">' . $this
->t('Currently no roles have this permission.') . '</div>';
}
return [
'#markup' => $message,
];
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$protected_entity_type = $this->entity
->getProtectedEntityTypeId();
// Help text.
$items = [];
// User 1.
$account = $this->userStorage
->load(1);
if (!empty($account)) {
$items[] = [
'#markup' => $this
->t('User @id (@name)', [
'@id' => $account
->id(),
'@name' => $account
->label(),
]),
];
}
// User in question (if protected entity type is "user").
if ($protected_entity_type == 'user') {
$account = $this->entity
->getProtectedEntity();
if (!empty($account)) {
$items[] = [
'#markup' => $this
->t('The protected user itself (@name)', [
'@name' => $account
->label(),
]),
];
}
else {
$items[] = [
'#markup' => $this
->t('The protected user itself'),
];
}
}
// Bypass all protections.
$permission = 'userprotect.bypass_all';
$permission_label = $this
->t('Bypass all user protections');
$items[] = $this
->getBypassMessage($permission, $permission_label);
// Bypass this protection.
$permission = $this->entity
->getPermissionName();
if ($permission && !$this->entity
->isNew()) {
$permission_label = $this
->t('Bypass user protection for @label', [
'@label' => $this->entity
->label(),
]);
$items[] = $this
->getBypassMessage($permission, $permission_label);
}
switch ($protected_entity_type) {
case 'user':
$form['help'] = [
'#markup' => $this
->t('This user will be protected for all users except:'),
];
break;
case 'user_role':
$form['help'] = [
'#markup' => $this
->t('This role will be protected for all users except:'),
];
break;
}
$form['help']['list'] = [
'#theme' => 'item_list',
'#items' => $items,
];
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Name'),
'#default_value' => $this->entity
->label(),
'#required' => TRUE,
];
$form['name'] = [
'#type' => 'machine_name',
'#machine_name' => [
'exists' => [
$this->protectionRuleStorage,
'load',
],
],
'#default_value' => $this->entity
->id(),
'#required' => TRUE,
];
switch ($protected_entity_type) {
case 'user':
$form['entity_id'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'user',
'#title' => $this
->t('User'),
'#default_value' => $this->entity
->getProtectedEntity(),
'#required' => TRUE,
];
break;
case 'user_role':
$entities = array_map('Drupal\\Component\\Utility\\Html::escape', user_role_names(TRUE));
$form['entity_id'] = [
'#type' => 'select',
'#title' => $this
->t('Role'),
'#options' => $entities,
'#default_value' => $this->entity
->getProtectedEntityId(),
'#required' => TRUE,
];
break;
}
$protection_options = [];
$enabled_protections = [];
foreach ($this->entity
->getProtections()
->getAll() as $name => $plugin) {
$protection_options[$name] = [
'protection' => [
'data' => [
'#type' => 'item',
'#markup' => $plugin
->label(),
'#description' => $plugin
->description(),
],
],
];
if ($plugin->status) {
$enabled_protections[$name] = TRUE;
}
}
$form['protection'] = [
'#type' => 'tableselect',
'#header' => [
'protection' => $this
->t('Protection'),
],
'#options' => $protection_options,
'#default_value' => $enabled_protections,
];
$roles = array_map('Drupal\\Component\\Utility\\Html::escape', user_role_names());
$form['bypass_roles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Bypass for roles'),
'#description' => $this
->t('Note: this setting will be saved as user permissions.'),
'#options' => $roles,
'#default_value' => $this->entity
->getBypassRoles(),
];
return parent::form($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
// Set protected entity ID.
$entity_id = $form_state
->getValue('entity_id');
$this->entity
->setProtectedEntityId($entity_id);
// Set enabled plugins.
foreach ($form_state
->getValue('protection') as $instance_id => $enabled) {
if ($enabled) {
$this->entity
->enableProtection($instance_id);
}
else {
$this->entity
->disableProtection($instance_id);
}
}
// Set bypass roles.
$bypass_roles = [];
foreach ($form_state
->getValue('bypass_roles') as $rid => $status) {
if (!empty($status)) {
$bypass_roles[] = $rid;
}
}
$this->entity
->setBypassRoles($bypass_roles);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$this->entity
->save();
$form_state
->setRedirect('userprotect.rule_list');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
ProtectionRuleFormBase:: |
protected | property |
The messenger service. Overrides MessengerTrait:: |
|
ProtectionRuleFormBase:: |
protected | property | The protection rule entity storage controller. | |
ProtectionRuleFormBase:: |
protected | property | The user storage. | |
ProtectionRuleFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ProtectionRuleFormBase:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
1 |
ProtectionRuleFormBase:: |
protected | function | Construct message for bypass permission. | |
ProtectionRuleFormBase:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
2 |
ProtectionRuleFormBase:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides EntityForm:: |
|
ProtectionRuleFormBase:: |
public | function | Constructs a base class for protection rule add and edit forms. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |