diff --git a/nodeaccess.routing.yml b/nodeaccess.routing.yml index db1e119..842edd2 100644 --- a/nodeaccess.routing.yml +++ b/nodeaccess.routing.yml @@ -1,8 +1,8 @@ nodeaccess.administration: path: '/admin/config/people/nodeaccess' defaults: - _form: '\Drupal\nodeaccess\Form\ConfigForm' - _title: 'Nodeaccess' + _form: '\Drupal\nodeaccess\Form\SettingsForm' + _title: 'Nodeaccess Settings' requirements: _permission: 'administer nodeaccess' entity.node.grants: diff --git a/src/Form/ConfigForm.php b/src/Form/ConfigForm.php deleted file mode 100644 index 80532cf..0000000 --- a/src/Form/ConfigForm.php +++ /dev/null @@ -1,241 +0,0 @@ -entityTypeManager = $entitytype_manager; - } - - /** - * {@inheritDoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('config.factory'), - $container->get('entity_type.manager') - ); - } - - /** - * {@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 = $this->entityTypeManager->getStorage('node_type')->loadMultiple(); - $roles = user_roles(); - - // 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' => '' . $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.') . '', - ]; - $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' => $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' => $user_perms[$id]['grant_view'] ?? 0, - ], - 'grant_update' => [ - '#type' => 'checkbox', - '#default_value' => $user_perms[$id]['grant_update'] ?? 0, - ], - 'grant_delete' => [ - '#type' => 'checkbox', - '#default_value' => $user_perms[$id]['grant_delete'] ?? 0, - ], - ]; - } - $form[$type]['user_permissions']['author'] = [ - 'label' => [ - '#plain_text' => $this->t('Author'), - ], - 'grant_view' => [ - '#type' => 'checkbox', - '#default_value' => $user_perms['author']['grant_view'] ?? 0, - ], - 'grant_update' => [ - '#type' => 'checkbox', - '#default_value' => $user_perms['author']['grant_update'] ?? 0, - ], - 'grant_delete' => [ - '#type' => 'checkbox', - '#default_value' => $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 = $this->entityTypeManager->getStorage('node_type')->loadMultiple(); - $allowed_types = []; - - $settings = $this->config('nodeaccess.settings') - ->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); - } - -} diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php new file mode 100644 index 0000000..95b898a --- /dev/null +++ b/src/Form/SettingsForm.php @@ -0,0 +1,254 @@ +entityTypeManager = $entitytype_manager; + } + + /** + * {@inheritDoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('entity_type.manager') + ); + } + + /** + * {@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'); + + // Set allowed_grant_operations. + // Select permissions/grant operations you want to allow users to view and + // edit. + $allowed_grant_operations = $settings->get('allowed_grant_operations'); + $form['allowed_grant_operations'] = [ + '#type' => 'details', + '#open' => TRUE, + '#title' => $this->t('Allowed grants operations'), + '#tree' => TRUE, + '#description' => '' . $this->t('The selected grant operations 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.') . '', + ]; + $form['allowed_grant_operations']['grant_view'] = [ + '#type' => 'checkbox', + '#title' => $this->t('View'), + '#default_value' => $allowed_grant_operations['grant_view'], + ]; + $form['allowed_grant_operations']['grant_update'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Edit'), + '#default_value' => $allowed_grant_operations['grant_update'], + ]; + $form['allowed_grant_operations']['grant_delete'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Delete'), + '#default_value' => $allowed_grant_operations['grant_delete'], + ]; + + $roles = $this->entityTypeManager->getStorage('user_role')->loadMultiple();; + + // Set roles_settings. + // Select roles the permissions of which you want to allow users to + // view and edit, and the aliases and weights of those roles. + $form['roles_settings'] = [ + '#type' => 'details', + '#open' => TRUE, + '#title' => $this->t('Roles settings -- select roles and set role display names'), + '#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 a display name (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['roles_settings']['settings'] = [ + '#type' => 'table', + '#header' => [ + $this->t('Available on grant tab?'), + $this->t('Display name (alias)'), + $this->t('Weight'), + ], + '#tabledrag' => [ + [ + 'action' => 'order', + 'relationship' => 'sibling', + 'group' => 'role-weight', + ], + ], + ]; + $roles_settings = $settings->get('roles_settings'); + foreach ($roles as $role_id => $role) { + $role_settings = $roles_settings[$role_id] ?? []; + $form['roles_settings']['settings'][$role_id]['selected'] = [ + '#type' => 'checkbox', + '#title' => Html::escape($role->label()), + '#default_value' => $role_settings['selected'] ?? FALSE, + ]; + $form['roles_settings']['settings'][$role_id]['display_name'] = [ + '#type' => 'textfield', + '#default_value' => $role_settings['display_name'] ?? Html::escape($role->label()), + '#size' => 50, + '#maxlength' => 50, + ]; + $form['roles_settings']['settings'][$role_id]['weight'] = [ + '#type' => 'weight', + '#default_value' => $role_settings['weight'] ?? 0, + '#delta' => 10, + '#attributes' => ['class' => ['role-weight']], + ]; + $form['roles_settings']['settings'][$role_id]['name'] = [ + '#type' => 'hidden', + '#value' => $role->label(), + ]; + $form['roles_settings']['settings'][$role_id]['#weight'] = $role_settings['weight'] ?? 0; + $form['roles_settings']['settings'][$role_id]['#attributes']['class'][] = 'draggable'; + } + + // Grants per bundle settings. + // Set grants_tab_availability Set bundles_roles_grants. + $grants_tab_availability = $settings->get('grants_tab_availability'); + $bundles_roles_grants = $settings->get('bundles_roles_grants'); + $node_types = $this->entityTypeManager->getStorage('node_type')->loadMultiple(); + + $form['bundles_roles_grants'] = [ + '#type' => 'details', + '#open' => TRUE, + '#title' => $this->t('Grants per content type settings'), + '#tree' => TRUE, + '#description' => $this->t('Grants per content type settings'), + ]; + + // Generate fieldsets for each bundle. + foreach ($node_types as $bundle => $node_type) { + $bundle_roles_grants = $bundles_roles_grants[$bundle]; + $form['bundles_roles_grants'][$bundle] = [ + '#type' => 'details', + '#open' => TRUE, + '#title' => $node_type->label(), + '#tree' => TRUE, + '#description' => $this->t('Note: the settings selected for the node author will define what permissions the node author has. This cannot be changed on individual node grants.'), + ]; + + $form['bundles_roles_grants'][$bundle]['show_grant_tab'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Show grant tab for this content type'), + '#default_value' => $grants_tab_availability[$bundle] ?? FALSE, + ]; + + $form['bundles_roles_grants'][$bundle]['settings'] = [ + '#type' => 'table', + '#header' => [ + $this->t('Role name'), + $this->t('View'), + $this->t('Edit'), + $this->t('Delete'), + ], + ]; + + // Set default role permissions for bundle. + foreach ($roles as $role_id => $role) { + $form['bundles_roles_grants'][$bundle]['settings'][$role_id] = [ + 'label' => [ + '#plain_text' => $role->label(), + ], + 'grant_view' => [ + '#type' => 'checkbox', + '#default_value' => $bundle_roles_grants[$role_id]['grant_view'] ?? 0, + ], + 'grant_update' => [ + '#type' => 'checkbox', + '#default_value' => $bundle_roles_grants[$role_id]['grant_update'] ?? 0, + ], + 'grant_delete' => [ + '#type' => 'checkbox', + '#default_value' => $bundle_roles_grants[$role_id]['grant_delete'] ?? 0, + ], + ]; + } + $form['bundles_roles_grants'][$bundle]['settings']['author'] = [ + 'label' => [ + '#plain_text' => $this->t('Author'), + ], + 'grant_view' => [ + '#type' => 'checkbox', + '#default_value' => $bundle_settings['author']['grant_view'] ?? 0, + ], + 'grant_update' => [ + '#type' => 'checkbox', + '#default_value' => $bundle_settings['author']['grant_update'] ?? 0, + ], + 'grant_delete' => [ + '#type' => 'checkbox', + '#default_value' => $bundle_settings['author']['grant_delete'] ?? 0, + ], + ]; + } + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $values = $form_state->getValues(); + $bundles_roles_grants_values = $values['bundles_roles_grants'] ?? []; + $bundles_roles_grants = []; + $grants_tab_availability = []; + if (!empty($bundles_roles_grants_values)) { + foreach ($bundles_roles_grants_values as $bundle => $roles_grants_values) { + $grants_tab_availability[$bundle] = $roles_grants_values['show_grant_tab']; + $bundles_roles_grants[$bundle] = $roles_grants_values['settings']; + } + } + + $this->config('nodeaccess.settings') + ->set('allowed_grant_operations', $values['allowed_grant_operations'] ?? []) + ->set('bundles_roles_grants', $bundles_roles_grants) + ->set('grants_tab_availability', $grants_tab_availability) + ->set('roles_settings', $values['roles_settings']['settings'] ?? []) + ->save(); + node_access_needs_rebuild(TRUE); + parent::submitForm($form, $form_state); + } + +}