materio_user.module 5.9 KB

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