public function ProtectionRuleFormBase::form in User protect 8
Gets the actual form array to be built.
Overrides EntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
1 call to ProtectionRuleFormBase::form()
- ProtectionRuleEditForm::form in src/
Form/ ProtectionRuleEditForm.php - Gets the actual form array to be built.
1 method overrides ProtectionRuleFormBase::form()
- ProtectionRuleEditForm::form in src/
Form/ ProtectionRuleEditForm.php - Gets the actual form array to be built.
File
- src/
Form/ ProtectionRuleFormBase.php, line 87
Class
- ProtectionRuleFormBase
- Provides a base form controller for a protection rule.
Namespace
Drupal\userprotect\FormCode
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);
}