class ContentAccessAdminSettingsForm in Content Access 8
Node Access settings form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\content_access\Form\ContentAccessAdminSettingsForm uses ContentAccessRoleBasedFormTrait
Expanded class hierarchy of ContentAccessAdminSettingsForm
1 string reference to 'ContentAccessAdminSettingsForm'
File
- src/
Form/ ContentAccessAdminSettingsForm.php, line 15
Namespace
Drupal\content_access\FormView source
class ContentAccessAdminSettingsForm extends FormBase {
use ContentAccessRoleBasedFormTrait;
/**
* The permission handler.
*
* @var \Drupal\user\PermissionHandlerInterface
*/
protected $permissionHandler;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a new ContentAccessAdminSettingsForm.
*
* @param \Drupal\user\PermissionHandlerInterface $permission_handler
* The permission handler.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
*/
public function __construct(PermissionHandlerInterface $permission_handler, ModuleHandlerInterface $module_handler) {
$this->permissionHandler = $permission_handler;
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('user.permissions'), $container
->get('module_handler'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'content_access_admin_settings';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $node_type = NULL) {
$storage = [
'node_type' => $node_type,
];
$form_state
->setStorage($storage);
// Add role based per content type settings.
$defaults = [];
foreach (_content_access_get_operations() as $op => $label) {
$defaults[$op] = content_access_get_settings($op, $node_type);
}
$this
->roleBasedForm($form, $defaults, $node_type);
// Per node:
$form['node'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Per content node access control settings'),
'#collapsible' => TRUE,
'#description' => $this
->t('Optionally you can enable per content node access control settings. If enabled, a new tab for the content access settings appears when viewing content. You have to configure permission to access these settings at the <a href=":url">permissions</a> page.', [
':url' => Url::fromRoute('user.admin_permissions')
->toString(),
]),
];
$form['node']['per_node'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable per content node access control settings'),
'#default_value' => content_access_get_settings('per_node', $node_type),
];
$form['advanced'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Advanced'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['advanced']['priority'] = [
'#type' => 'weight',
'#title' => $this
->t('Give content node grants priority'),
'#default_value' => content_access_get_settings('priority', $node_type),
'#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 you can adjust the priority of this module.'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
'#weight' => 10,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$storage = $form_state
->getStorage();
$roles = array_keys(user_roles());
$roles_permissions = user_role_permissions($roles);
$permissions = $this->permissionHandler
->getPermissions();
$node_type = $storage['node_type'];
// Remove disabled modules permissions, so they can't raise exception
// in ::savePermissions().
foreach ($roles_permissions as $rid => $role_permissions) {
foreach ($role_permissions as $permission => $value) {
if (!array_key_exists($permission, $permissions)) {
unset($roles_permissions[$rid][$permission]);
}
}
}
foreach ([
'update',
'update_own',
'delete',
'delete_own',
] as $op) {
foreach ($values[$op] as $rid => $value) {
$permission = content_access_get_permission_by_op($op, $node_type);
if ($value) {
$roles_permissions[$rid][$permission] = TRUE;
}
else {
$roles_permissions[$rid][$permission] = FALSE;
}
}
// Don't save the setting, so its default value (get permission) is
// applied always.
unset($values[$op]);
}
$this
->savePermissions($roles_permissions);
// Update content access settings.
$settings = content_access_get_settings('all', $node_type);
foreach (content_access_available_settings() as $setting) {
if (isset($values[$setting])) {
$settings[$setting] = is_array($values[$setting]) ? array_keys(array_filter($values[$setting])) : $values[$setting];
}
}
content_access_set_settings($settings, $node_type);
// Mass update the nodes, but only if necessary.
if (content_access_get_settings('per_node', $node_type) || content_access_get_settings('view', $node_type) != $form['per_role']['view']['#default_value'] || content_access_get_settings('view_own', $node_type) != $form['per_role']['view_own']['#default_value'] || content_access_get_settings('priority', $node_type) != $form['advanced']['priority']['#default_value'] || content_access_get_settings('per_node', $node_type) != $form['node']['per_node']['#default_value']) {
// If per node has been disabled and we use the ACL integration, we have
// to remove possible ACLs now.
if (!content_access_get_settings('per_node', $node_type) && $form['node']['per_node']['#default_value'] && $this->moduleHandler
->moduleExists('acl')) {
_content_access_remove_acls($node_type);
}
if (content_access_mass_update([
$node_type,
])) {
$node_types = node_type_get_names();
// This does not gurantee a rebuild.
$this
->messenger()
->addMessage($this
->t('Permissions have been changed for the content type @types.<br />You may have to <a href=":rebuild">rebuild permissions</a> for your changes to take effect.', [
'@types' => $node_types[$node_type],
':rebuild' => Url::fromRoute('node.configure_rebuild_confirm')
->toString(),
]));
}
}
else {
$this
->messenger()
->addMessage($this
->t('No change.'));
}
}
/**
* Saves the given permissions by role to the database.
*/
protected function savePermissions($roles_permissions) {
foreach ($roles_permissions as $rid => $permissions) {
user_role_change_permissions($rid, $permissions);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentAccessAdminSettingsForm:: |
protected | property | The module handler service. | |
ContentAccessAdminSettingsForm:: |
protected | property | The permission handler. | |
ContentAccessAdminSettingsForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ContentAccessAdminSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ContentAccessAdminSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ContentAccessAdminSettingsForm:: |
protected | function | Saves the given permissions by role to the database. | |
ContentAccessAdminSettingsForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ContentAccessAdminSettingsForm:: |
public | function | Constructs a new ContentAccessAdminSettingsForm. | |
ContentAccessRoleBasedFormTrait:: |
public static | function | Checkboxes access for content. | |
ContentAccessRoleBasedFormTrait:: |
protected | function | Builds the role based permission form for the given defaults. | |
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 | 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:: |
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. |