diff --git a/src/Form/UwContentAccessForm.php b/src/Form/UwContentAccessForm.php index fe3790d20b910adf0138265bd1e64dcb034b3ca3..69b38cea7b1779ace0ab947c051b6bbe2b88db8d 100644 --- a/src/Form/UwContentAccessForm.php +++ b/src/Form/UwContentAccessForm.php @@ -129,8 +129,7 @@ class UwContentAccessForm extends FormBase { foreach ($uw_permission as $perm => $uw_permission_roles) { // Set the row name to be use for this table (role-permission_name). - $row_name = strtolower(str_replace(' ', '_', $feature)); - $row_name .= '-' . strtolower(str_replace(' ', '_', $perm)); + $row_name = $feature . '-' . $perm; // If we are on the first of the row, setup the description. if ($row_count == 0) { @@ -175,9 +174,78 @@ class UwContentAccessForm extends FormBase { } } + $form['actions']['#type'] = 'actions'; + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => $this->t('Submit'), + '#button_type' => 'primary', + ); + return $form; } + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + + // The permissions array that was submitted in the form. + $submitted_permissions = $form_state->getValue('permissions'); + + // The array of uw permissions. + $uw_permissions = $this->uw_get_permissions_array(); + + // The array of uw roles. + $uw_roles = $this->uw_get_uw_roles_content_access_form(); + + // Step through each role and store the role object. + foreach ($uw_roles as $uw_role) { + + // Store the role object. + $uw_role_objects[$uw_role['name']] = \Drupal\user\Entity\Role::load($uw_role['id']); + } + + // Step through each of the submitted permissions to grant or revoke it for the role. + // Step through each of the permissions array to get the row name (role-permission). + foreach ($submitted_permissions as $row_name => $submitted_permission) { + + // Step through each of the role-permissions to revoke or grant. + foreach ($submitted_permission as $uw_role_name => $permission_value) { + + // Break up the row name from role-permission into array. + // 0th element is role. + // 1st element is permission. + $perm_names = explode('-', $row_name); + + // Get the list of permissions to revoke/grant from the uw_permissions array. + $uw_perms = $uw_permissions[$perm_names[0]][$perm_names[1]][$uw_role_name]; + + // If the checkbox was selected on the form grant the permissions that is + // in the uw_permissions array for that role. + if ($permission_value) { + + // Step through each of the permissions for that role and grant the permission. + foreach ($uw_perms as $uw_perm) { + + // Grant the permission for the specified role. + $uw_role_objects[$uw_role_name]->grantPermission($uw_perm); + } + } + // If the checkbox was not selected on the form revoke the permissions that is + // in the uw_permissions array for that role. + else { + + // Step through each of the permissions for that role and revoke the permission. + foreach ($uw_perms as $uw_perm) { + + // Revoke the permission for the specified role. + $uw_role_objects[$uw_role_name]->revokePermission($uw_perm); + } + } + } + } + } + /** * Get UW roles that are going to be used for the form. * @@ -331,15 +399,4 @@ class UwContentAccessForm extends FormBase { return $uw_permissions; } - - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, FormStateInterface $form_state) { - - // drupal_set_message($this->t('@can_name ,Your application is being submitted!', array('@can_name' => $form_state->getValue('candidate_name')))); - foreach ($form_state->getValues() as $key => $value) { - drupal_set_message($key . ': ' . $value); - } - } }