materio_commerce.module 8.0 KB

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