materio_user.module 3.6 KB

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