materio_user.module 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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('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. '#markup' => '
  31. <span class="login-message" v-if="loginMessage">
  32. {{ loginMessage }}
  33. </span>
  34. '
  35. );
  36. }
  37. function materio_user_form_user_modal_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  38. // Drupal::logger('materio_user')->notice(print_r($form['mail'], true));
  39. // ksm($form);
  40. $form['account']['mail']['#attributes'] = array(
  41. "v-model" => "mail",
  42. "@keyup.enter" => "register",
  43. "placeholder" => $form['account']['mail']['#title'],
  44. "title" => $form['account']['mail']['#description']
  45. );
  46. // https://drupal.stackexchange.com/a/217112
  47. // Get default process function array:
  48. $element_info = element_info('password_confirm');
  49. $process = $element_info['#process'];
  50. // Add our process function to the array:
  51. $process[] = '_materio_user_process_password_confirm';
  52. $form['account']['pass']['#process'] = $process;
  53. $form['actions']['submit']['#attributes'] = array(
  54. "@click.prevent" => "register",
  55. "@keyup.enter" => "register",
  56. "ref" => "register"
  57. // ":class" => "can_register"
  58. );
  59. }
  60. function _materio_user_process_password_confirm($element){
  61. // ksm($element);
  62. $element['pass1']['#description'] = t('Password must contains Lowercase and Uppercase Letters, numbers, and punctuation.');
  63. $element['pass1']['#attributes'] += array(
  64. "v-model" => "pass1",
  65. "placeholder" => $element['pass1']['#title'],
  66. ":class" => "psswd_class"
  67. );
  68. $element['pass2']['#attributes'] += array(
  69. "v-model" => "pass2",
  70. "placeholder" => $element['pass2']['#title']
  71. );
  72. return $element;
  73. }
  74. /**
  75. * Returns HTML for a form element.
  76. * Prepares variables for form element templates.
  77. *
  78. * Default template: form-element.html.twig.
  79. *
  80. * @param array $variables
  81. * An associative array containing:
  82. * - element: An associative array containing the properties of the element.
  83. * Properties used: #title, #title_display, #description, #id, #required,
  84. * #children, #type, #name, #label_for.
  85. */
  86. // function materio_user_preprocess_form_element(&$vars) {
  87. // $element = $vars['element'];
  88. // if($element['#type'] == 'password' && $element['#array_parents'][0] == 'account'){
  89. // ksm($vars);
  90. // // $vars['attributes']['placeholder'] = $element['#title'];
  91. // $vars['element']['#attributes']['placeholder'] = $element['#title'];
  92. // }
  93. // }
  94. /**
  95. * implements hook_block_view_BASE_BLOCK_ID_alter()
  96. *
  97. * https://www.drupal.org/project/drupal/issues/2626224
  98. */
  99. function materio_user_block_view_user_login_block_alter(array &$build, BlockPluginInterface $block) {
  100. $build['#pre_render'][] = '_materio_user_user_login_block_pre_render';
  101. }
  102. function _materio_user_user_login_block_pre_render(array $build){
  103. $user_links = &$build['content']['user_links'];
  104. $items = &$user_links['#items'];
  105. // ksm($items);
  106. $items['create_account']['#url']->mergeOptions(array(
  107. "attributes" => array(
  108. "@click.prevent" => "create_account"
  109. )
  110. ));
  111. $items['request_password']['#url']->mergeOptions(array(
  112. 'attributes' => array(
  113. "@click.prevent" => "request_password"
  114. )
  115. ));
  116. return $build;
  117. }
  118. function materio_user_entity_type_build(array &$entity_types) {
  119. // https://drupal.stackexchange.com/a/230547
  120. $entity_types['user']->setFormClass('modal', 'Drupal\user\RegisterForm');
  121. }