-1

I installed the Two-factor Authentication (TFA) and the TFA Basic plugins modules.

I want to alter the value returned from tfa_basic_tfa_ready_require(), which is a hook implementation, in a custom module.

/**
 * Implements hook_tfa_ready_require().
 */
function tfa_basic_tfa_ready_require($account) {
  if (tfa_basic_tfa_required($account)) {
    drupal_set_message(t('Login disallowed. You are required to set up two-factor authentication. Please contact a site administrator.'), 'error');
    return TRUE;
  }
  return FALSE;
}

I tried the following code in the custom_tfa.module file, but it does not work.

function custom_tfa_tfa_basic_tfa_ready_require_alter($account) {
  drupal_set_message(t('TEST'), 'warning');
  return FALSE;
}

How can I alter the value returned from a hook?

6
  • If the hook is named hook_tfa_ready_require and your custom module is named custom_tfa then your custom module needs to implement custom_tfa_tfa_ready_require($account).
    – leymannx
    Commented Aug 17, 2023 at 9:21
  • Hi leymannx, but that was the Drupal 7 module of tfa_basic, which is included with tfa (main module). I was thinking whether able to alter the methods inside the tfa_basic only. hook_tfa_ready_require() is from the tfa module though, how do I determine which of their methods is a hook?
    – DevNooby
    Commented Aug 17, 2023 at 9:45
  • 1
    Alter hooks are not supposed to return any value. They get a parameter passed by reference; they just need to alter it.
    – avpaderno
    Commented Aug 17, 2023 at 11:47
  • To understand which functions are hook implementations, it is sufficient to look at the documentation comment for that function: Implements hook_tfa_ready_require(). says that is a hook implementation.
    – avpaderno
    Commented Aug 17, 2023 at 11:49
  • So there is no way to change the method tfa_basic_tfa_ready_require from the tfa_basic module?
    – DevNooby
    Commented Aug 18, 2023 at 1:23

1 Answer 1

0

Since tfa_basic_tfa_ready_require() shows a message when it returns TRUE, altering the value returned from that hook is not sufficient. It would not make sense to show Login disallowed. You are required to set up two-factor authentication. Please contact a site administrator. when the login is allowed.

To avoid Drupal core invokes tfa_basic_tfa_ready_require(), it is necessary to implement hook_module_implements_alter().

function custom_tfa_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'tfa_ready_require') {
    unset($implementations['tfa_basic']);
  }
}

Then, the module can implement its own hook_tfa_ready_require().

function custom_tfa_tfa_ready_require($account) {
  return FALSE;
}

As a side note, custom_tfa_tfa_basic_tfa_ready_require_alter() would eventually be an alter hook implemented by the custom_tfa module for the hook_tfa_basic_tfa_ready_require() hook implementations. Leaving out the fact alter hooks get different parameters, hook_tfa_basic_tfa_ready_require() is not the hook implemented from tfa_basic_tfa_ready_require().

As a further note, alter hooks are invoked when the module that uses them calls drupal_alter(). Usually, for hook_HOOK_TYPE(), hook_HOOK_TYPE_alter() is invoked too; that is not always the case, though. In the cases where hook_HOOK_TYPE() is invoked, but hook_HOOK_TYPE_alter() is not invoked, implementing hook_module_implements_alter() is the solution, although it requires to duplicate code from the other module and to keep the new hook implementation updated each time the other module's hook is updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.