displaying licenses in user edit form

This commit is contained in:
Bachir Soussi Chiadmi 2022-11-14 22:33:48 +01:00
parent 22feda7263
commit 83a6f13a10
1 changed files with 51 additions and 0 deletions

View File

@ -1,4 +1,6 @@
<?php
// use Drupal\Core\Form\FormBuilder;
use \Drupal\Core\Link;
function materio_commerce_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'commerce_checkout_flow_multistep_default') {
@ -21,3 +23,52 @@ function materio_commerce_form_commerce_order_add_form_alter(&$form, &$form_stat
$form['customer']['uid']['#selection_handler'] = 'default:user_by_email';
}
}
function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) {
$mail = $form['account']['mail']['#default_value'];
$user_storage = \Drupal::entityTypeManager()->getStorage('user');
$users = $user_storage->loadByProperties(['mail' => $mail]);
$user = reset($users);
$roles = $user->getRoles();
$license_storage = \Drupal::entityTypeManager()->getStorage('commerce_license');
$licenses = $license_storage->loadByProperties(['uid' => $user->id()]);
if ($licenses) {
$form['license'] = [
"#type" => 'fieldset',
"#title" => 'licenses'
];
foreach ($licenses as $lid => $license) {
$product_variation = $license->getPurchasedEntity();
$product = $product_variation->getProduct();
$infos = [];
$infos[] = $product->getTitle();
$infos[] = $product_variation->getTitle();
$granted = $license->getGrantedTime();
$infos[] = "granted: " . date('d-m-Y', $granted);
$expires = $license->getExpiresTime();
$infos[] = "expires: " . ($expires === "0" ? 'never' : date('d-m-Y', $expires));
$license_edit_url = $license->toUrl();
$license_link = Link::fromTextAndUrl('edit license', $license_edit_url);
$infos[] = $license_link->toString();
$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("order " . $order_number . " (" . $order_state . ")", $order_url);
$infos[] = $order_link->toString();
$form['license']['license_'.$lid] = [
"#type" => 'item',
"#description" => implode(" | ", $infos)
];
}
}
}