fixed #1998 disabled roles not posted so not saved, so removed from user

This commit is contained in:
Bachir Soussi Chiadmi 2022-11-18 12:58:14 +01:00
parent dd38d8d2b2
commit 6255d58a95
1 changed files with 36 additions and 2 deletions

View File

@ -4,7 +4,7 @@
use \Drupal\Core\Link; use \Drupal\Core\Link;
// use Drupal\Core\Entity\EntityInterface; // use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityFormInterface; use Drupal\Core\Entity\EntityFormInterface;
// use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\user\EntityOwnerInterface; use Drupal\user\EntityOwnerInterface;
@ -50,6 +50,8 @@ function materio_commerce_form_role_delegation_role_assign_form_alter(&$form, &$
$licenses = $license_storage->loadByProperties(['uid' => $account->id()]); $licenses = $license_storage->loadByProperties(['uid' => $account->id()]);
if ($licenses) { if ($licenses) {
// reorganise licenses by role
// and record by role if a license is active
$licenses_by_role = []; $licenses_by_role = [];
foreach ($licenses as $lid => $license) { foreach ($licenses as $lid => $license) {
$licensed_role_id = $license->license_role->target_id; $licensed_role_id = $license->license_role->target_id;
@ -59,6 +61,7 @@ function materio_commerce_form_role_delegation_role_assign_form_alter(&$form, &$
$licenses_by_role[$licensed_role_id]['license_active'] = true; $licenses_by_role[$licensed_role_id]['license_active'] = true;
} }
} }
// loop throught roles then licenses by role
foreach ($licenses_by_role as $rid => $role_licenses) { foreach ($licenses_by_role as $rid => $role_licenses) {
$license_active = isset($role_licenses['license_active']) ? true : false; $license_active = isset($role_licenses['license_active']) ? true : false;
$description = ""; $description = "";
@ -68,6 +71,7 @@ function materio_commerce_form_role_delegation_role_assign_form_alter(&$form, &$
$description = t("This role is granted by a license. It cannot be removed manually.<br/>"); $description = t("This role is granted by a license. It cannot be removed manually.<br/>");
} }
// reverse the licenses to display the last first
$reversed_licenses = array_reverse($role_licenses['licenses']); $reversed_licenses = array_reverse($role_licenses['licenses']);
foreach ($reversed_licenses as $license) { foreach ($reversed_licenses as $license) {
$license_state = $license->getState()->getValue()['value']; $license_state = $license->getState()->getValue()['value'];
@ -141,6 +145,8 @@ function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) {
$licenses = $license_storage->loadByProperties(['uid' => $entity->id()]); $licenses = $license_storage->loadByProperties(['uid' => $entity->id()]);
if ($licenses) { if ($licenses) {
// reorganise licenses by role
// and record by role if a license is active
$licenses_by_role = []; $licenses_by_role = [];
foreach ($licenses as $lid => $license) { foreach ($licenses as $lid => $license) {
$licensed_role_id = $license->license_role->target_id; $licensed_role_id = $license->license_role->target_id;
@ -150,15 +156,34 @@ function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) {
$licenses_by_role[$licensed_role_id]['license_active'] = true; $licenses_by_role[$licensed_role_id]['license_active'] = true;
} }
} }
// we need to validate the form as disabled cekbox are not sent on submit
// as it remove the disabled roles
$form["#validate"][] = 'materio_commerce_form_user_form_validate';
// hidden licensed role will be re-injected in 'role_change' field values during our validate
$form['licensed_roles'] = array(
'#type'=>"container",
'#tree' => true
);
// loop throught roles then licenses by role
$form["role_change"]["widget"]['#options_attributes'] = [];
foreach ($licenses_by_role as $rid => $role_licenses) { foreach ($licenses_by_role as $rid => $role_licenses) {
$license_active = isset($role_licenses['license_active']) ? true : false; $license_active = isset($role_licenses['license_active']) ? true : false;
$description = ""; $description = "";
if($license_active) { if($license_active) {
$form["role_change"]["widget"][$rid]['#disabled'] = TRUE; $form["role_change"]["widget"][$rid]['#disabled'] = TRUE;
$form["role_change"]["widget"][$rid]['#default_value'] = TRUE; // $form["role_change"][$rid]['#default_value'] = TRUE;
$description = t("This role is granted by a license. It cannot be removed manually.<br/>"); $description = t("This role is granted by a license. It cannot be removed manually.<br/>");
// record the role in hidden element, as disabled checkboxes are not posted
// we'll re-establish the right values in validate function
$form['licensed_roles'][$rid] = array(
'#type' => 'hidden',
'#value' => $rid
);
} }
// reverse the licenses to display the last first
$reversed_licenses = array_reverse($role_licenses['licenses']); $reversed_licenses = array_reverse($role_licenses['licenses']);
foreach ($reversed_licenses as $license) { foreach ($reversed_licenses as $license) {
$license_state = $license->getState()->getValue()['value']; $license_state = $license->getState()->getValue()['value'];
@ -201,5 +226,14 @@ function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) {
} }
} }
}
function materio_commerce_form_user_form_validate($form, FormStateInterface &$form_state) {
$values = $form_state->getValues();
if (!empty($values['licensed_roles'])) {
foreach ($values['licensed_roles'] as $rid => $value) {
$values['role_change'][] = array("target_id"=>$value);
}
$form_state->setValues($values);
}
} }