materio_user.module 4.0 KB

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