materio_commerce.module 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. // use Drupal\Core\Form\FormBuilder;
  3. use \Drupal\Core\Link;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Entity\EntityFormInterface;
  6. use Drupal\Core\Form\FormStateInterface;
  7. use Drupal\user\EntityOwnerInterface;
  8. function materio_commerce_form_alter(&$form, &$form_state, $form_id) {
  9. if ($form_id == 'commerce_checkout_flow_multistep_default') {
  10. if ($form["#step_id"] == "review") {
  11. // $form['buttons']['continue']['#value'] = t('Desired Custom Text Here');
  12. $form['actions']['next']['#value'] = t('Place your order');
  13. }
  14. }
  15. }
  16. function materio_commerce_form_commerce_order_add_form_alter(&$form, &$form_state, $form_id) {
  17. $currentUser = \Drupal::currentUser();
  18. $roles = $currentUser->getRoles();
  19. if (in_array("admin", $roles)){
  20. $form['type']['#default_value'] = 'materio_order_type';
  21. $form['type']['#disabled'] = true;
  22. $form['customer']['uid']['#selection_handler'] = 'default:user_by_email';
  23. }
  24. }
  25. function materio_commerce_form_role_delegation_role_assign_form_alter(&$form, &$form_state, $form_id) {
  26. /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
  27. $form_object = $form_state->getFormObject();
  28. // if (!($form_object instanceof EntityFormInterface)) {
  29. // // We're only interested in forms for an entity.
  30. // return;
  31. // }
  32. /** @var \Drupal\user\UserInterface $account */
  33. $account = $form_state->getBuildInfo()['args'][0];
  34. // $entity = $form_object->getEntity();
  35. // $entity_type_id = $entity->getEntityTypeId();
  36. // if (!($entity instanceof EntityOwnerInterface) && $entity_type_id != 'user') {
  37. // // We only act on entities that have an owner, and user entities.
  38. // return;
  39. // }
  40. $license_storage = \Drupal::entityTypeManager()->getStorage('commerce_license');
  41. $licenses = $license_storage->loadByProperties(['uid' => $account->id()]);
  42. if ($licenses) {
  43. $description = t("This role is granted by a license. It cannot be removed manually.");
  44. foreach ($licenses as $lid => $license) {
  45. $licensed_role_id = $license->license_role->target_id;
  46. $form["account"]["role_change"][$licensed_role_id]['#disabled'] = TRUE;
  47. $form["account"]["role_change"][$licensed_role_id]['#default_value'] = TRUE;
  48. $product_variation = $license->getPurchasedEntity();
  49. $product = null;
  50. if ($product_variation){
  51. $product = $product_variation->getProduct();
  52. }
  53. $granted = $license->getGrantedTime();
  54. $expires = $license->getExpiresTime();
  55. $license_edit_url = $license->toUrl();
  56. $license_link = Link::fromTextAndUrl(t('edit license'), $license_edit_url);
  57. $order = $license->getOriginatingOrder();
  58. if ($order) {
  59. $order_url = $order->toUrl();
  60. $order_state = $order->get('state')->first()->getValue()['value'];
  61. $order_number = $order->get('order_number')->first()->getValue()['value'];
  62. $order_link = Link::fromTextAndUrl(t("order @num (@state)", array(
  63. '@num' => $order_number,
  64. '@state' => $order_state
  65. )), $order_url);
  66. }
  67. $description .= t("<br/>-- @product: @variation | granted: @granted | expires: @expires | @license_link | @order_link", array(
  68. '@product' => $product ? $product->getTitle() : t('no product'),
  69. '@variation' => $product_variation ? $product_variation->getTitle() : t('no variation'),
  70. '@granted' => date('d-m-Y', $granted),
  71. '@expires' => $expires === "0" ? 'never' : date('d-m-Y', $expires),
  72. '@license_link' => $license_link->toString(),
  73. '@order_link' => $order ? $order_link->toString() : t('no order')
  74. ));
  75. }
  76. $form["account"]["role_change"][$licensed_role_id]['#description'] = $description;
  77. }
  78. }
  79. function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) {
  80. if ($form_id !== 'user_form') return;
  81. // WE USE FORM OBJECT TO GET THE ENTITY instead of followings
  82. // $mail = $form['account']['mail']['#default_value'];
  83. // $user_storage = \Drupal::entityTypeManager()->getStorage('user');
  84. // $users = $user_storage->loadByProperties(['mail' => $mail]);
  85. // $user = reset($users);
  86. // $roles = $user->getRoles();
  87. /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
  88. $form_object = $form_state->getFormObject();
  89. if (!($form_object instanceof EntityFormInterface)) {
  90. // We're only interested in forms for an entity.
  91. return;
  92. }
  93. $entity = $form_object->getEntity();
  94. $entity_type_id = $entity->getEntityTypeId();
  95. if (!($entity instanceof EntityOwnerInterface) && $entity_type_id != 'user') {
  96. // We only act on entities that have an owner, and user entities.
  97. return;
  98. }
  99. $license_storage = \Drupal::entityTypeManager()->getStorage('commerce_license');
  100. $licenses = $license_storage->loadByProperties(['uid' => $entity->id()]);
  101. if ($licenses) {
  102. $description = t("This role is granted by a license. It cannot be removed manually.");
  103. foreach ($licenses as $lid => $license) {
  104. $licensed_role_id = $license->license_role->target_id;
  105. $form["role_change"]["widget"][$licensed_role_id]['#disabled'] = TRUE;
  106. $form["role_change"]["widget"][$licensed_role_id]['#default_value'] = TRUE;
  107. $product_variation = $license->getPurchasedEntity();
  108. $product = null;
  109. if ($product_variation){
  110. $product = $product_variation->getProduct();
  111. }
  112. $granted = $license->getGrantedTime();
  113. $expires = $license->getExpiresTime();
  114. $license_edit_url = $license->toUrl();
  115. $license_link = Link::fromTextAndUrl(t('edit license'), $license_edit_url);
  116. $order = $license->getOriginatingOrder();
  117. if ($order) {
  118. $order_url = $order->toUrl();
  119. $order_state = $order->get('state')->first()->getValue()['value'];
  120. $order_number = $order->get('order_number')->first()->getValue()['value'];
  121. $order_link = Link::fromTextAndUrl(t("order @num (@state)", array(
  122. '@num' => $order_number,
  123. '@state' => $order_state
  124. )), $order_url);
  125. }
  126. $description .= t("<br/>-- @product: @variation | granted: @granted | expires: @expires | @license_link | @order_link", array(
  127. '@product' => $product ? $product->getTitle() : t('no product'),
  128. '@variation' => $product_variation ? $product_variation->getTitle() : t('no variation'),
  129. '@granted' => date('d-m-Y', $granted),
  130. '@expires' => $expires === "0" ? 'never' : date('d-m-Y', $expires),
  131. '@license_link' => $license_link->toString(),
  132. '@order_link' => $order ? $order_link->toString() : t('no order')
  133. ));
  134. }
  135. $form["role_change"]["widget"][$licensed_role_id]['#description'] = $description;
  136. }
  137. }