materio_user.module 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @file
  4. * Contains materio_user.module.
  5. */
  6. use \Drupal\Core\Form\FormStateInterface;
  7. use \Drupal\Core\Block\BlockPluginInterface;
  8. use \Drupal\Core\Url;
  9. use \Drupal\Core\Link;
  10. use \Drupal\materio_user\MaterioUserLoginBlockAlterRender;
  11. /**
  12. * implements hook_form_FORM_ID_alter()
  13. *
  14. */
  15. function materio_user_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  16. $form['name']['#attributes'] += array(
  17. "v-model" => "mail",
  18. "title" => "",
  19. // "@keyup.enter" => "login"
  20. );
  21. $form['pass']['#attributes'] = array(
  22. "v-model" => "password",
  23. "title" => "",
  24. // "@keyup.enter" => "login"
  25. );
  26. $url = Url::fromRoute('user.pass');
  27. $form['pass']['#description'] = Link::fromTextAndUrl(t('Forgot your password?'), $url)->toString();
  28. // "<a href="{{ path('user.pass') }}">{{ 'Forgot your password?'|t }}</a>";
  29. $form['actions']['submit']['#attributes'] = array(
  30. "@click.prevent" => "login"
  31. );
  32. $form['message'] = array(
  33. '#weight' => -10,
  34. '#markup' => '
  35. <span class="login-message" v-if="loginMessage">
  36. {{ loginMessage }}
  37. </span>
  38. '
  39. );
  40. }
  41. function materio_user_form_user_modal_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  42. $form['account']['mail']['#attributes'] = array(
  43. "v-model" => "mail",
  44. "@keyup.enter" => "register",
  45. "placeholder" => $form['account']['mail']['#title'],
  46. "title" => $form['account']['mail']['#description']
  47. );
  48. // ? https://drupal.stackexchange.com/a/217112
  49. // Get default process function array:
  50. // $element_info = element_info('password_confirm');
  51. // ? https://api.drupal.org/api/drupal/core%21includes%21common.inc/function/element_info/8.2.x
  52. $element_info = \Drupal::service('element_info')->getInfo('password_confirm');
  53. $process = $element_info['#process'];
  54. // Add our process function to the array:
  55. $process[] = '_materio_user_process_password_confirm';
  56. $form['account']['pass']['#process'] = $process;
  57. $form['actions']['submit']['#attributes'] = array(
  58. "@click.prevent" => "register",
  59. "@keyup.enter" => "register",
  60. "ref" => "register",
  61. "disabled" => true
  62. // ":class" => "can_register"
  63. );
  64. $form['account']['message'] = array(
  65. '#weight' => -10,
  66. '#markup' => '
  67. <span class="register-message" v-if="registerMessage">
  68. {{ registerMessage }}
  69. </span>
  70. '
  71. );
  72. }
  73. function _materio_user_process_password_confirm($element){
  74. $element['pass1']['#description'] = t('Password must contains height lowercase, uppercase, digits and special characters.');
  75. $element['pass1']['#attributes'] += array(
  76. "v-model" => "pass1",
  77. "placeholder" => $element['pass1']['#title'],
  78. ":class" => "psswd_class"
  79. );
  80. $element['pass2']['#attributes'] += array(
  81. "v-model" => "pass2",
  82. "placeholder" => $element['pass2']['#title']
  83. );
  84. return $element;
  85. }
  86. /**
  87. * Returns HTML for a form element.
  88. * Prepares variables for form element templates.
  89. *
  90. * Default template: form-element.html.twig.
  91. *
  92. * @param array $variables
  93. * An associative array containing:
  94. * - element: An associative array containing the properties of the element.
  95. * Properties used: #title, #title_display, #description, #id, #required,
  96. * #children, #type, #name, #label_for.
  97. */
  98. // function materio_user_preprocess_form_element(&$vars) {
  99. // $element = $vars['element'];
  100. // if($element['#type'] == 'password' && $element['#array_parents'][0] == 'account'){
  101. // ksm($vars);
  102. // // $vars['attributes']['placeholder'] = $element['#title'];
  103. // $vars['element']['#attributes']['placeholder'] = $element['#title'];
  104. // }
  105. // }
  106. /**
  107. * implements hook_block_view_BASE_BLOCK_ID_alter()
  108. *
  109. * OLD D8 https://www.drupal.org/project/drupal/issues/2626224
  110. * NEW D9 https://www.drupal.org/node/2966725
  111. */
  112. function materio_user_block_view_user_login_block_alter(array &$build, BlockPluginInterface $block) {
  113. // $build['#pre_render'][] = '_materio_user_user_login_block_pre_render';
  114. $build['#pre_render'][] = [MaterioUserLoginBlockAlterRender::class, 'preRender'];
  115. }
  116. function _materio_user_user_login_block_pre_render(array $build){
  117. $user_links = &$build['content']['user_links'];
  118. $items = &$user_links['#items'];
  119. unset($items['create_account']);
  120. // $items['create_account']['#url']->mergeOptions(array(
  121. // "attributes" => array(
  122. // "@click.prevent" => "create_account"
  123. // )
  124. // ));
  125. // Do not handle passward reset with vue
  126. // $items['request_password']['#url']->mergeOptions(array(
  127. // 'attributes' => array(
  128. // "@click.prevent" => "request_password"
  129. // )
  130. // ));
  131. return $build;
  132. }
  133. function materio_user_entity_type_build(array &$entity_types) {
  134. // https://drupal.stackexchange.com/a/230547
  135. $entity_types['user']->setFormClass('modal', 'Drupal\user\RegisterForm');
  136. }
  137. /*
  138. * hook_menu_local_tasks_alter
  139. */
  140. function materio_user_menu_local_tasks_alter(&$data, $route_name){
  141. $t="t";
  142. // remove some tabs on user pages
  143. // if (in_array($route_name, ['entity.user.canonical', 'entity.user.edit_form']) ) {
  144. if (preg_match('/^entity\.user\./', $route_name)
  145. || in_array($route_name, [
  146. 'simplenews.newsletter_subscriptions_user',
  147. 'commerce_order.address_book.overview',
  148. 'entity.commerce_payment_method.collection',
  149. 'view.commerce_user_orders.order_page',
  150. 'view.commerce_user_subscriptions.page_1'
  151. ])) {
  152. unset($data['tabs'][0]['profile.user_page:member']);
  153. $data['tabs'][0]['entity.user.edit_form']['#weight'] = -20;
  154. $data['tabs'][0]['simplenews.newsletter_subscriptions_user']['#weight'] = -9;
  155. }
  156. // foreach ($data['tabs'] as $key => $tab) {
  157. // if ($key == 'profile.user_page:member') {
  158. // unset($data['tabs'][$key]);
  159. // }
  160. // if ($key == 'entity.user.edit_form') {
  161. // $data['tabs'][$key]['#weight'] = -200;
  162. // }
  163. //
  164. // }
  165. }
  166. /*
  167. * hook_local_tasks_alter
  168. */
  169. // function materio_user_local_tasks_alter(&$local_tasks){
  170. // $t="t";
  171. // }
  172. function materio_user_form_alter(array &$form, FormStateInterface $forme_state, $form_id){
  173. if ($form_id == 'simplenews_subscriber_account_form') {
  174. $user = \Drupal::currentUser();
  175. $roles = $user->getRoles();
  176. $t="t";
  177. if (in_array('adherent', $roles)) {
  178. unset($form['subscriptions']['widget']['#options']['companies']);
  179. unset($form['subscriptions']['widget']['#options']['test']);
  180. }
  181. }
  182. }