class ConfigForm in Nodeaccess 8
Same name and namespace in other branches
- 8.2 src/Form/ConfigForm.php \Drupal\nodeaccess\Form\ConfigForm
Builds the configuration form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\nodeaccess\Form\ConfigForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of ConfigForm
1 string reference to 'ConfigForm'
File
- src/
Form/ ConfigForm.php, line 13
Namespace
Drupal\nodeaccess\FormView source
class ConfigForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getEditableConfigNames() {
return [
'nodeaccess.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'nodeaccess_admin_settings';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$settings = $this
->config('nodeaccess.settings');
$role_alias = $settings
->get('role_alias');
$allowed_grants = $settings
->get('grants');
$allowed_types = $settings
->get('allowed_types');
$role_header = [
$this
->t('Allow Role'),
$this
->t('Alias'),
$this
->t('Weight'),
];
$header = [
$this
->t('ROLE'),
$this
->t('VIEW'),
$this
->t('EDIT'),
$this
->t('DELETE'),
];
$node_types = NodeType::loadMultiple();
$roles = user_roles();
$form['priority'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Give node grants priority'),
'#default_value' => $settings
->get('priority'),
'#description' => $this
->t('If you are only using this access control module, you can safely ignore this. If you are using multiple access control modules, and you want the grants given on individual nodes to override any grants given by other modules, you should check this box.'),
];
// Select whether to preserve hidden grants.
$form['preserve'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Preserve hidden grants'),
'#default_value' => $settings
->get('preserve'),
'#description' => '<small>' . $this
->t('If you check this box, any hidden grants are preserved when you save grants. Otherwise all grants users are not allowed to view or edit are revoked on save.') . '</small>',
];
// Select permissions you want to allow users to view and edit.
$form['grants'] = [
'#type' => 'details',
'#open' => FALSE,
'#title' => $this
->t('Allowed Grants'),
'#tree' => TRUE,
'#description' => '<small>' . $this
->t('The selected grants will be listed on individual node grants. If you wish for certain grants to be hidden from users on the node grants tab, make sure they are not selected here.') . '</small>',
];
$form['grants']['view'] = [
'#type' => 'checkbox',
'#title' => $this
->t('View'),
'#default_value' => $allowed_grants['view'],
];
$form['grants']['edit'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Edit'),
'#default_value' => $allowed_grants['edit'],
];
$form['grants']['delete'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Delete'),
'#default_value' => $allowed_grants['delete'],
];
// Select roles the permissions of which you want to allow users to
// view and edit, and the aliases and weights of those roles.
$form['role'] = [
'#type' => 'details',
'#open' => FALSE,
'#title' => $this
->t('Allowed Roles'),
'#tree' => TRUE,
'#description' => $this
->t('The selected roles will be listed on individual node grants. If you wish for certain roles to be hidden from users on the node grants tab, make sure they are not selected here. You may also provide an alias for each role to be displayed to the user and a weight to order them by. This is useful if your roles have machine-readable names not intended for human users.'),
];
$form['role']['alias'] = [
'#type' => 'table',
'#header' => $role_header,
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'role-weight',
],
],
];
foreach ($role_alias as $id => $role) {
$form['role']['alias'][$id]['allow'] = [
'#type' => 'checkbox',
'#title' => HTML::escape($role['name']),
'#default_value' => $role['allow'],
];
$form['role']['alias'][$id]['alias'] = [
'#type' => 'textfield',
'#default_value' => $role['alias'],
'#size' => 50,
'#maxlength' => 50,
];
$form['role']['alias'][$id]['weight'] = [
'#type' => 'weight',
'#default_value' => $role['weight'],
'#delta' => 10,
'#attributes' => [
'class' => [
'role-weight',
],
],
];
$form['role']['alias'][$id]['name'] = [
'#type' => 'hidden',
'#value' => $role['name'],
];
$form['role']['alias'][$id]['#weight'] = $role['weight'];
$form['role']['alias'][$id]['#attributes']['class'][] = 'draggable';
}
// Generate fieldsets for each node type.
foreach ($node_types as $type => $bundle) {
$user_perms = $settings
->get($type);
$form[$type] = [
'#type' => 'details',
'#open' => FALSE,
'#title' => $bundle
->label(),
'#tree' => TRUE,
'#description' => $this
->t('The settings selected for the node author will define what permissions the node author has. This cannot be changed on individual node grants.'),
];
$form[$type]['show'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show grant tab for this node type'),
'#default_value' => isset($allowed_types[$type]) ? $allowed_types[$type] : 0,
];
$form[$type]['user_permissions'] = [
'#type' => 'table',
'#header' => $header,
];
// Set default role permissions for node type.
foreach ($roles as $id => $role) {
$form[$type]['user_permissions'][$id] = [
'label' => [
'#plain_text' => $role
->label(),
],
'grant_view' => [
'#type' => 'checkbox',
'#default_value' => isset($user_perms[$id]['grant_view']) ? $user_perms[$id]['grant_view'] : 0,
],
'grant_update' => [
'#type' => 'checkbox',
'#default_value' => isset($user_perms[$id]['grant_update']) ? $user_perms[$id]['grant_update'] : 0,
],
'grant_delete' => [
'#type' => 'checkbox',
'#default_value' => isset($user_perms[$id]['grant_delete']) ? $user_perms[$id]['grant_delete'] : 0,
],
];
}
$form[$type]['user_permissions']['author'] = [
'label' => [
'#plain_text' => $this
->t('Author'),
],
'grant_view' => [
'#type' => 'checkbox',
'#default_value' => isset($user_perms['author']['grant_view']) ? $user_perms['author']['grant_view'] : 0,
],
'grant_update' => [
'#type' => 'checkbox',
'#default_value' => isset($user_perms['author']['grant_update']) ? $user_perms['author']['grant_update'] : 0,
],
'grant_delete' => [
'#type' => 'checkbox',
'#default_value' => isset($user_perms['author']['grant_delete']) ? $user_perms['author']['grant_delete'] : 0,
],
];
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Update configuration.
$values = $form_state
->getValues();
$node_types = NodeType::loadMultiple();
$allowed_types = [];
$settings = $this
->config('nodeaccess.settings')
->set('priority', $values['priority'])
->set('preserve', $values['preserve'])
->set('grants', $values['grants']);
foreach ($node_types as $type => $bundle) {
$config = $values[$type]['user_permissions'];
$allowed_types[$type] = $values[$type]['show'];
$settings
->set($type, $config);
}
$settings
->set('allowed_types', $allowed_types);
// Save allowed roles, role aliases and weights.
$settings
->set('role_alias', $values['role']['alias']);
$settings
->save();
node_access_needs_rebuild(TRUE);
parent::submitForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
ConfigForm:: |
public | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
ConfigForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ConfigForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
ConfigFormBase:: |
public | function | Constructs a \Drupal\system\ConfigFormBase object. | 11 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
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 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
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:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
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. |