From 287a57a5d399cead697ad77f815b9f4d17e5b522 Mon Sep 17 00:00:00 2001 From: bach Date: Thu, 17 Nov 2022 12:28:33 +0100 Subject: [PATCH] fixed linvesed roles display in user edit form --- .../materio_commerce/materio_commerce.module | 69 +++++++++++-------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/web/modules/custom/materio_commerce/materio_commerce.module b/web/modules/custom/materio_commerce/materio_commerce.module index 2c1a8936..0170c63f 100644 --- a/web/modules/custom/materio_commerce/materio_commerce.module +++ b/web/modules/custom/materio_commerce/materio_commerce.module @@ -48,14 +48,20 @@ function materio_commerce_form_role_delegation_role_assign_form_alter(&$form, &$ $license_storage = \Drupal::entityTypeManager()->getStorage('commerce_license'); $licenses = $license_storage->loadByProperties(['uid' => $account->id()]); + if ($licenses) { + $description = t("This role is granted by a license. It cannot be removed manually."); foreach ($licenses as $lid => $license) { $licensed_role_id = $license->license_role->target_id; $form["account"]["role_change"][$licensed_role_id]['#disabled'] = TRUE; $form["account"]["role_change"][$licensed_role_id]['#default_value'] = TRUE; + $product_variation = $license->getPurchasedEntity(); - $product = $product_variation->getProduct(); + $product = null; + if ($product_variation){ + $product = $product_variation->getProduct(); + } $granted = $license->getGrantedTime(); $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); $order = $license->getOriginatingOrder(); - $order_url = $order->toUrl(); - $order_state = $order->get('state')->first()->getValue()['value']; - $order_number = $order->get('order_number')->first()->getValue()['value']; - $order_link = Link::fromTextAndUrl(t("order @num (@state)", array( - '@num' => $order_number, - '@state' => $order_state - )), $order_url); + if ($order) { + $order_url = $order->toUrl(); + $order_state = $order->get('state')->first()->getValue()['value']; + $order_number = $order->get('order_number')->first()->getValue()['value']; + $order_link = Link::fromTextAndUrl(t("order @num (@state)", array( + '@num' => $order_number, + '@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.
@product: @variation | granted: @granted | expires: @expires | @license_link | @order_link", array( - '@product' => $product->getTitle(), - '@variation' => $product_variation->getTitle(), + $description .= t("
-- @product: @variation | granted: @granted | expires: @expires | @license_link | @order_link", array( + '@product' => $product ? $product->getTitle() : t('no product'), + '@variation' => $product_variation ? $product_variation->getTitle() : t('no variation'), '@granted' => date('d-m-Y', $granted), '@expires' => $expires === "0" ? 'never' : date('d-m-Y', $expires), '@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) { @@ -116,10 +124,7 @@ function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) { $licenses = $license_storage->loadByProperties(['uid' => $entity->id()]); if ($licenses) { - // $form['license'] = [ - // "#type" => 'fieldset', - // "#title" => 'licenses' - // ]; + $description = t("This role is granted by a license. It cannot be removed manually."); foreach ($licenses as $lid => $license) { $licensed_role_id = $license->license_role->target_id; $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 = $product_variation->getProduct(); + $product = null; + if ($product_variation){ + $product = $product_variation->getProduct(); + } $granted = $license->getGrantedTime(); $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); $order = $license->getOriginatingOrder(); - $order_url = $order->toUrl(); - $order_state = $order->get('state')->first()->getValue()['value']; - $order_number = $order->get('order_number')->first()->getValue()['value']; - $order_link = Link::fromTextAndUrl(t("order @num (@state)", array( - '@num' => $order_number, - '@state' => $order_state - )), $order_url); + if ($order) { + $order_url = $order->toUrl(); + $order_state = $order->get('state')->first()->getValue()['value']; + $order_number = $order->get('order_number')->first()->getValue()['value']; + $order_link = Link::fromTextAndUrl(t("order @num (@state)", array( + '@num' => $order_number, + '@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.
@product: @variation | granted: @granted | expires: @expires | @license_link | @order_link", array( - '@product' => $product->getTitle(), - '@variation' => $product_variation->getTitle(), + $description .= t("
-- @product: @variation | granted: @granted | expires: @expires | @license_link | @order_link", array( + '@product' => $product ? $product->getTitle() : t('no product'), + '@variation' => $product_variation ? $product_variation->getTitle() : t('no variation'), '@granted' => date('d-m-Y', $granted), '@expires' => $expires === "0" ? 'never' : date('d-m-Y', $expires), '@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; }