materio_user.module 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. );
  46. }
  47. function _materio_user_process_password_confirm($element){
  48. // ksm($element);
  49. $element['pass1']['#attributes'] += array(
  50. "v-model" => "pass1",
  51. "placeholder" => $element['pass1']['#title']
  52. );
  53. $element['pass2']['#attributes'] += array(
  54. "v-model" => "pass2",
  55. "placeholder" => $element['pass2']['#title']
  56. );
  57. return $element;
  58. }
  59. /**
  60. * Returns HTML for a form element.
  61. * Prepares variables for form element templates.
  62. *
  63. * Default template: form-element.html.twig.
  64. *
  65. * @param array $variables
  66. * An associative array containing:
  67. * - element: An associative array containing the properties of the element.
  68. * Properties used: #title, #title_display, #description, #id, #required,
  69. * #children, #type, #name, #label_for.
  70. */
  71. // function materio_user_preprocess_form_element(&$vars) {
  72. // $element = $vars['element'];
  73. // if($element['#type'] == 'password' && $element['#array_parents'][0] == 'account'){
  74. // ksm($vars);
  75. // // $vars['attributes']['placeholder'] = $element['#title'];
  76. // $vars['element']['#attributes']['placeholder'] = $element['#title'];
  77. // }
  78. // }
  79. /**
  80. * implements hook_block_view_BASE_BLOCK_ID_alter()
  81. *
  82. * https://www.drupal.org/project/drupal/issues/2626224
  83. */
  84. function materio_user_block_view_user_login_block_alter(array &$build, BlockPluginInterface $block) {
  85. $build['#pre_render'][] = '_materio_user_user_login_block_pre_render';
  86. }
  87. function _materio_user_user_login_block_pre_render(array $build){
  88. $user_links = &$build['content']['user_links'];
  89. $items = &$user_links['#items'];
  90. // ksm($items);
  91. $items['create_account']['#url']->mergeOptions(array(
  92. "attributes" => array(
  93. "@click.prevent" => "create_account"
  94. )
  95. ));
  96. $items['request_password']['#url']->mergeOptions(array(
  97. 'attributes' => array(
  98. "@click.prevent" => "request_password"
  99. )
  100. ));
  101. return $build;
  102. }
  103. function materio_user_entity_type_build(array &$entity_types) {
  104. // https://drupal.stackexchange.com/a/230547
  105. $entity_types['user']->setFormClass('modal', 'Drupal\user\RegisterForm');
  106. }