Drupal 8: Adding Events To Existing Behaviours

15th September 2020

I recently needed to add functionality to the Password Policy module so I thought I would outline the steps I took in a blog post. The Password Policy module is used to enforce a stricter password policy on users on a Drupal site. This means that when a user creates or changes their password they must conform to certain rules like the password length, or if it contains upper and lower case characters. There are a set of rules to chose from and they can be fully customised by the site administrators. It's a good module, you should check it out.

I was recently working on a site that had used this module for some time. I was looking at improving the user experience across the site and one thing that stood out was that the Password Policy module only triggers once you have left the password field. I decided to build a little module that would add a real time check to the password field that would update as the user filled in their password. Due to the way in which the Password Policy module has been created I needed to override hooks and augment the existing JavaScript with some of my own.

This post was put together from my working out how to override the Password Policy module in very specific areas to add this real time checking functionality. It also shows how you can augment existing JavaScript events using a few lines of code.

The first step was to remove a hook that the Password Policy module adds to the system. The module has a hook_element_info_alter() hook that is used to add in a form process feature to the password confirmation field. As we are looking to alter this method we need to remove this hook from Drupal so that we can add out own version. It is possible to remove implemented hooks from a Drupal system using the hook_module_implements_alter() hook. We just need to find the relevant hook and ensure that it doesn't exist.

  1. /**
  2.  * Implements hook_module_implements_alter().
  3.  */
  4. function my_module_module_implements_alter(&$implementations, $hook) {
  5. if ($hook == 'element_info_alter' && isset($implementations['my_module'])) {
  6. if (isset($implementations['password_policy'])) {
  7. // If the password_policy module is set then remove its
  8. // element_info_alter hook as we will be implementing our own.
  9. unset($implementations['password_policy']);
  10. }
  11. }
  12. }

With the hook_module_implements_alter() hook gone we need to create our own version. This is done by copying the Password Policy version of this hook and changing a few names. The #process element on the password confirm allows us to alter the element quite late in the processing of the element itself, which means that we can effect every password confirmation element straight away. The use of the process element here was why we needed to overwrite this method completely as there is no way to intercept this upstream.

  1. /**
  2.  * Implements hook_element_info_alter().
  3.  */
  4. function my_module_element_info_alter(array &$types) {
  5. if (isset($types['password_confirm'])) {
  6. $types['password_confirm']['#process'][] = 'my_module_password_policy_check_constraints_password_confirm_process';
  7. }
  8. }

An alternative to this approach would have been to use the hook_module_implements_alter() hook to inject our version of hook_element_info_alter() to be executed after the Password Policy module version. I have written about changing hook weights in a previous post if you are interested.

With that in place we can create a our process function. This function exists in the Password Policy module and is used to inject the needed ajax options into the field. The Password Policy module adds a disable-refocus parameter to the ajax call which creates a problem for what we are trying to achieve here. This disable-refocus parameter prevents the field from being refocused after the ajax event is triggered. This is removed and a new library added to the element that we can inject JavaScript to use control the automatic update. Fundamentally, we are keeping the existing functionality and events provided by the module, but adding our own library and tweaking the existing settings.

Remember that the password confirmation element actually exists as two fields. The password field (called pass1) and the password confirmation field (called pass2) so this action is being applied to the first password field.

  1. function my_module_password_policy_check_constraints_password_confirm_process(array $element, FormStateInterface $form_state, array $form) {
  2. $form_object = $form_state->getFormObject();
  4. if (method_exists($form_object, 'getEntity') && $form_object->getEntity() instanceof UserInterface) {
  5. if (_password_policy_show_policy()) {
  6. $element['pass1']['#ajax'] = [
  7. 'event' => 'change',
  8. 'callback' => '_password_policy_check_constraints',
  9. 'method' => 'replace',
  10. 'wrapper' => 'password-policy-status',
  11. ];
  12. $element['pass1']['#attached']['library'][] = 'my_module/password_event';
  13. $element['pass1']['#attributes']['class'][] = 'passwordtimer';
  14. }
  15. }
  17. return $element;
  18. }

As we have added a new JavaScript library to the module we need to create a my_module.libraries.yml file for it.

  1. password_event:
  2.   js:
  3.   js/password-event.js: {}
  4.   dependencies:
  5. - core/jquery
  6. - core/drupal
  7. - core/drupal.ajax

With this in place we can create the js/password-event.js file. This JavaScript file is used to wrap the existing change event that the Password Policy module put in place. In reality it is Drupal that creates this JavaScript from the options in the Password Policy module, not the module itself injecting code into the site. Adding to the existing event uses some of the lessons learnt in a previous post about inspecting and reusing jQuery events. What we are doing here is pulling out the existing change event from the password field and wrapping it in a event that detects if the user has pressed a key on the same field with an offset of a second. This means that if a user stops typing then we trigger the original change event and show the user their password policy update.

  1. (function ($, Drupal) {
  2. 'use strict';
  4. Drupal.behaviors.bform = {
  5. attach : function(context, settings) {
  6. // Load the existing change event.
  7. var events = $._data($(".passwordtimer")[0], "events");
  9. // Create function to trigger the loaded event.
  10. function doneTyping(event) {
  11. $.each(events.change, function() {
  12. this.handler(event);
  13. });
  14. }
  16. var typingTimer;
  17. var doneTypingInterval = 1000;
  19. // Trigger an event when the user presses a key.
  20. $('.passwordtimer').keyup(function(event) {
  21. clearTimeout(typingTimer);
  22. typingTimer = setTimeout(doneTyping, doneTypingInterval, event);
  23. return false;
  24. });
  26. }
  27. };
  29. })(jQuery, Drupal);

The reason we wait for a second before triggering this event is because otherwise the screen would be jumping around as the user types into the field. By offsetting the update by a second we can ensure that the user has broken from typing long enough to want to see the update.

With all that in place the Password Policy module now has a real time password checking feature and but still has the original check after the user has swapped fields.

Would I recommend always doing this kind of thing? Looking back on it now, a few weeks later, probably not. Although it is possible to override functionality like this in Drupal it does create some technical debt., The more you do this, the more you will open code up for errors. If the underlying module changes its structure then your code will not function and you'll be forced to revisit it. There comes a point where a complex series of hook overrides  would be better solved by a simple patch against the module, which in retrospect is probably what I should have done here.

Add new comment

The content of this field is kept private and will not be shown publicly.