fixed linvesed roles display in user edit form

This commit is contained in:
Bachir Soussi Chiadmi 2022-11-17 12:28:33 +01:00
parent aff162514b
commit 287a57a5d3
1 changed files with 40 additions and 29 deletions

View File

@ -48,14 +48,20 @@ function materio_commerce_form_role_delegation_role_assign_form_alter(&$form, &$
$license_storage = \Drupal::entityTypeManager()->getStorage('commerce_license'); $license_storage = \Drupal::entityTypeManager()->getStorage('commerce_license');
$licenses = $license_storage->loadByProperties(['uid' => $account->id()]); $licenses = $license_storage->loadByProperties(['uid' => $account->id()]);
if ($licenses) { if ($licenses) {
$description = t("This role is granted by a license. It cannot be removed manually.");
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;
$form["account"]["role_change"][$licensed_role_id]['#disabled'] = TRUE; $form["account"]["role_change"][$licensed_role_id]['#disabled'] = TRUE;
$form["account"]["role_change"][$licensed_role_id]['#default_value'] = TRUE; $form["account"]["role_change"][$licensed_role_id]['#default_value'] = TRUE;
$product_variation = $license->getPurchasedEntity(); $product_variation = $license->getPurchasedEntity();
$product = $product_variation->getProduct(); $product = null;
if ($product_variation){
$product = $product_variation->getProduct();
}
$granted = $license->getGrantedTime(); $granted = $license->getGrantedTime();
$expires = $license->getExpiresTime(); $expires = $license->getExpiresTime();
@ -64,27 +70,29 @@ function materio_commerce_form_role_delegation_role_assign_form_alter(&$form, &$
$license_link = Link::fromTextAndUrl(t('edit license'), $license_edit_url); $license_link = Link::fromTextAndUrl(t('edit license'), $license_edit_url);
$order = $license->getOriginatingOrder(); $order = $license->getOriginatingOrder();
$order_url = $order->toUrl(); if ($order) {
$order_state = $order->get('state')->first()->getValue()['value']; $order_url = $order->toUrl();
$order_number = $order->get('order_number')->first()->getValue()['value']; $order_state = $order->get('state')->first()->getValue()['value'];
$order_link = Link::fromTextAndUrl(t("order @num (@state)", array( $order_number = $order->get('order_number')->first()->getValue()['value'];
'@num' => $order_number, $order_link = Link::fromTextAndUrl(t("order @num (@state)", array(
'@state' => $order_state '@num' => $order_number,
)), $order_url); '@state' => $order_state
)), $order_url);
}
$form["account"]["role_change"][$licensed_role_id]['#description'] = t("This role is granted by a license. It cannot be removed manually.<br/> @product: @variation | granted: @granted | expires: @expires | @license_link | @order_link", array( $description .= t("<br/>-- @product: @variation | granted: @granted | expires: @expires | @license_link | @order_link", array(
'@product' => $product->getTitle(), '@product' => $product ? $product->getTitle() : t('no product'),
'@variation' => $product_variation->getTitle(), '@variation' => $product_variation ? $product_variation->getTitle() : t('no variation'),
'@granted' => date('d-m-Y', $granted), '@granted' => date('d-m-Y', $granted),
'@expires' => $expires === "0" ? 'never' : date('d-m-Y', $expires), '@expires' => $expires === "0" ? 'never' : date('d-m-Y', $expires),
'@license_link' => $license_link->toString(), '@license_link' => $license_link->toString(),
'@order_link' => $order_link->toString() '@order_link' => $order ? $order_link->toString() : t('no order')
)); ));
} }
$form["account"]["role_change"][$licensed_role_id]['#description'] = $description;
} }
} }
function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) { function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) {
@ -116,10 +124,7 @@ 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) {
// $form['license'] = [ $description = t("This role is granted by a license. It cannot be removed manually.");
// "#type" => 'fieldset',
// "#title" => 'licenses'
// ];
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;
$form["role_change"]["widget"][$licensed_role_id]['#disabled'] = TRUE; $form["role_change"]["widget"][$licensed_role_id]['#disabled'] = TRUE;
@ -127,7 +132,10 @@ function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) {
$product_variation = $license->getPurchasedEntity(); $product_variation = $license->getPurchasedEntity();
$product = $product_variation->getProduct(); $product = null;
if ($product_variation){
$product = $product_variation->getProduct();
}
$granted = $license->getGrantedTime(); $granted = $license->getGrantedTime();
$expires = $license->getExpiresTime(); $expires = $license->getExpiresTime();
@ -136,24 +144,27 @@ function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) {
$license_link = Link::fromTextAndUrl(t('edit license'), $license_edit_url); $license_link = Link::fromTextAndUrl(t('edit license'), $license_edit_url);
$order = $license->getOriginatingOrder(); $order = $license->getOriginatingOrder();
$order_url = $order->toUrl(); if ($order) {
$order_state = $order->get('state')->first()->getValue()['value']; $order_url = $order->toUrl();
$order_number = $order->get('order_number')->first()->getValue()['value']; $order_state = $order->get('state')->first()->getValue()['value'];
$order_link = Link::fromTextAndUrl(t("order @num (@state)", array( $order_number = $order->get('order_number')->first()->getValue()['value'];
'@num' => $order_number, $order_link = Link::fromTextAndUrl(t("order @num (@state)", array(
'@state' => $order_state '@num' => $order_number,
)), $order_url); '@state' => $order_state
)), $order_url);
}
$form["role_change"]["widget"][$licensed_role_id]['#description'] = t("This role is granted by a license. It cannot be removed manually.<br/> @product: @variation | granted: @granted | expires: @expires | @license_link | @order_link", array( $description .= t("<br/>-- @product: @variation | granted: @granted | expires: @expires | @license_link | @order_link", array(
'@product' => $product->getTitle(), '@product' => $product ? $product->getTitle() : t('no product'),
'@variation' => $product_variation->getTitle(), '@variation' => $product_variation ? $product_variation->getTitle() : t('no variation'),
'@granted' => date('d-m-Y', $granted), '@granted' => date('d-m-Y', $granted),
'@expires' => $expires === "0" ? 'never' : date('d-m-Y', $expires), '@expires' => $expires === "0" ? 'never' : date('d-m-Y', $expires),
'@license_link' => $license_link->toString(), '@license_link' => $license_link->toString(),
'@order_link' => $order_link->toString() '@order_link' => $order ? $order_link->toString() : t('no order')
)); ));
} }
$form["role_change"]["widget"][$licensed_role_id]['#description'] = $description;
} }