materio-d9/web/modules/custom/materio_commerce/materio_commerce.module

76 lines
2.6 KiB
Plaintext

<?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') {
if ($form["#step_id"] == "review") {
$t="t";
// $form['buttons']['continue']['#value'] = t('Desired Custom Text Here');
$form['actions']['next']['#value'] = t('Place your order');
}
}
}
function materio_commerce_form_commerce_order_add_form_alter(&$form, &$form_state, $form_id) {
$currentUser = \Drupal::currentUser();
$roles = $currentUser->getRoles();
if (in_array("admin", $roles)){
$t="t";
$form['type']['#default_value'] = 'materio_order_type';
$form['type']['#disabled'] = true;
$form['customer']['uid']['#selection_handler'] = 'default:user_by_email';
}
}
function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) {
if ($form_id !== 'user_form') return;
$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)
];
}
}
}