|
@@ -1,11 +1,15 @@
|
|
<?php
|
|
<?php
|
|
// use Drupal\Core\Form\FormBuilder;
|
|
// use Drupal\Core\Form\FormBuilder;
|
|
use \Drupal\Core\Link;
|
|
use \Drupal\Core\Link;
|
|
|
|
+use Drupal\Core\Entity\EntityInterface;
|
|
|
|
+use Drupal\Core\Entity\EntityFormInterface;
|
|
|
|
+use Drupal\Core\Form\FormStateInterface;
|
|
|
|
+use Drupal\user\EntityOwnerInterface;
|
|
|
|
+
|
|
|
|
|
|
function materio_commerce_form_alter(&$form, &$form_state, $form_id) {
|
|
function materio_commerce_form_alter(&$form, &$form_state, $form_id) {
|
|
if ($form_id == 'commerce_checkout_flow_multistep_default') {
|
|
if ($form_id == 'commerce_checkout_flow_multistep_default') {
|
|
if ($form["#step_id"] == "review") {
|
|
if ($form["#step_id"] == "review") {
|
|
- $t="t";
|
|
|
|
// $form['buttons']['continue']['#value'] = t('Desired Custom Text Here');
|
|
// $form['buttons']['continue']['#value'] = t('Desired Custom Text Here');
|
|
$form['actions']['next']['#value'] = t('Place your order');
|
|
$form['actions']['next']['#value'] = t('Place your order');
|
|
}
|
|
}
|
|
@@ -16,7 +20,6 @@ function materio_commerce_form_commerce_order_add_form_alter(&$form, &$form_stat
|
|
$currentUser = \Drupal::currentUser();
|
|
$currentUser = \Drupal::currentUser();
|
|
$roles = $currentUser->getRoles();
|
|
$roles = $currentUser->getRoles();
|
|
if (in_array("admin", $roles)){
|
|
if (in_array("admin", $roles)){
|
|
- $t="t";
|
|
|
|
$form['type']['#default_value'] = 'materio_order_type';
|
|
$form['type']['#default_value'] = 'materio_order_type';
|
|
$form['type']['#disabled'] = true;
|
|
$form['type']['#disabled'] = true;
|
|
|
|
|
|
@@ -24,51 +27,132 @@ function materio_commerce_form_commerce_order_add_form_alter(&$form, &$form_stat
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+function materio_commerce_form_role_delegation_role_assign_form_alter(&$form, &$form_state, $form_id) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
|
|
|
|
+ $form_object = $form_state->getFormObject();
|
|
|
|
+ // if (!($form_object instanceof EntityFormInterface)) {
|
|
|
|
+ // // We're only interested in forms for an entity.
|
|
|
|
+ // return;
|
|
|
|
+ // }
|
|
|
|
+ /** @var \Drupal\user\UserInterface $account */
|
|
|
|
+ $account = $form_state->getBuildInfo()['args'][0];
|
|
|
|
+ // $entity = $form_object->getEntity();
|
|
|
|
+ // $entity_type_id = $entity->getEntityTypeId();
|
|
|
|
+ // if (!($entity instanceof EntityOwnerInterface) && $entity_type_id != 'user') {
|
|
|
|
+ // // We only act on entities that have an owner, and user entities.
|
|
|
|
+ // return;
|
|
|
|
+ // }
|
|
|
|
+
|
|
|
|
+ $license_storage = \Drupal::entityTypeManager()->getStorage('commerce_license');
|
|
|
|
+ $licenses = $license_storage->loadByProperties(['uid' => $account->id()]);
|
|
|
|
+
|
|
|
|
+ if ($licenses) {
|
|
|
|
+ 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();
|
|
|
|
+
|
|
|
|
+ $granted = $license->getGrantedTime();
|
|
|
|
+ $expires = $license->getExpiresTime();
|
|
|
|
+
|
|
|
|
+ $license_edit_url = $license->toUrl();
|
|
|
|
+ $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);
|
|
|
|
+
|
|
|
|
+ $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(
|
|
|
|
+ '@product' => $product->getTitle(),
|
|
|
|
+ '@variation' => $product_variation->getTitle(),
|
|
|
|
+ '@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()
|
|
|
|
+ ));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) {
|
|
function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) {
|
|
if ($form_id !== 'user_form') return;
|
|
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);
|
|
|
|
|
|
+
|
|
|
|
+ // WE USE FORM OBJECT TO GET THE ENTITY instead of followings
|
|
|
|
+ // $mail = $form['account']['mail']['#default_value'];
|
|
|
|
+ // $user_storage = \Drupal::entityTypeManager()->getStorage('user');
|
|
|
|
+ // $users = $user_storage->loadByProperties(['mail' => $mail]);
|
|
|
|
+ // $user = reset($users);
|
|
// $roles = $user->getRoles();
|
|
// $roles = $user->getRoles();
|
|
|
|
|
|
|
|
+
|
|
|
|
+ /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
|
|
|
|
+ $form_object = $form_state->getFormObject();
|
|
|
|
+ if (!($form_object instanceof EntityFormInterface)) {
|
|
|
|
+ // We're only interested in forms for an entity.
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $entity = $form_object->getEntity();
|
|
|
|
+ $entity_type_id = $entity->getEntityTypeId();
|
|
|
|
+ if (!($entity instanceof EntityOwnerInterface) && $entity_type_id != 'user') {
|
|
|
|
+ // We only act on entities that have an owner, and user entities.
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
$license_storage = \Drupal::entityTypeManager()->getStorage('commerce_license');
|
|
$license_storage = \Drupal::entityTypeManager()->getStorage('commerce_license');
|
|
- $licenses = $license_storage->loadByProperties(['uid' => $user->id()]);
|
|
|
|
|
|
+ $licenses = $license_storage->loadByProperties(['uid' => $entity->id()]);
|
|
|
|
|
|
if ($licenses) {
|
|
if ($licenses) {
|
|
- $form['license'] = [
|
|
|
|
- "#type" => 'fieldset',
|
|
|
|
- "#title" => 'licenses'
|
|
|
|
- ];
|
|
|
|
|
|
+ // $form['license'] = [
|
|
|
|
+ // "#type" => 'fieldset',
|
|
|
|
+ // "#title" => 'licenses'
|
|
|
|
+ // ];
|
|
foreach ($licenses as $lid => $license) {
|
|
foreach ($licenses as $lid => $license) {
|
|
|
|
+ $licensed_role_id = $license->license_role->target_id;
|
|
|
|
+ $form["role_change"]["widget"][$licensed_role_id]['#disabled'] = TRUE;
|
|
|
|
+ $form["role_change"]["widget"][$licensed_role_id]['#default_value'] = TRUE;
|
|
|
|
+
|
|
|
|
+
|
|
$product_variation = $license->getPurchasedEntity();
|
|
$product_variation = $license->getPurchasedEntity();
|
|
$product = $product_variation->getProduct();
|
|
$product = $product_variation->getProduct();
|
|
|
|
|
|
- $infos = [];
|
|
|
|
- $infos[] = $product->getTitle();
|
|
|
|
- $infos[] = $product_variation->getTitle();
|
|
|
|
-
|
|
|
|
$granted = $license->getGrantedTime();
|
|
$granted = $license->getGrantedTime();
|
|
- $infos[] = "granted: " . date('d-m-Y', $granted);
|
|
|
|
$expires = $license->getExpiresTime();
|
|
$expires = $license->getExpiresTime();
|
|
- $infos[] = "expires: " . ($expires === "0" ? 'never' : date('d-m-Y', $expires));
|
|
|
|
|
|
|
|
$license_edit_url = $license->toUrl();
|
|
$license_edit_url = $license->toUrl();
|
|
- $license_link = Link::fromTextAndUrl('edit license', $license_edit_url);
|
|
|
|
- $infos[] = $license_link->toString();
|
|
|
|
|
|
+ $license_link = Link::fromTextAndUrl(t('edit license'), $license_edit_url);
|
|
|
|
|
|
$order = $license->getOriginatingOrder();
|
|
$order = $license->getOriginatingOrder();
|
|
$order_url = $order->toUrl();
|
|
$order_url = $order->toUrl();
|
|
$order_state = $order->get('state')->first()->getValue()['value'];
|
|
$order_state = $order->get('state')->first()->getValue()['value'];
|
|
$order_number = $order->get('order_number')->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();
|
|
|
|
|
|
+ $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.<br/> @product: @variation | granted: @granted | expires: @expires | @license_link | @order_link", array(
|
|
|
|
+ '@product' => $product->getTitle(),
|
|
|
|
+ '@variation' => $product_variation->getTitle(),
|
|
|
|
+ '@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()
|
|
|
|
+ ));
|
|
|
|
|
|
- $form['license']['license_'.$lid] = [
|
|
|
|
- "#type" => 'item',
|
|
|
|
- "#description" => implode(" | ", $infos)
|
|
|
|
- ];
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|