materio_user.module 5.8 KB

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