Score:-1

How can I alter the value returned from a hook?

nz flag

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?

leymannx avatar
ne flag
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)`.
DevNooby avatar
nz flag
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?
apaderno avatar
us flag
Alter hooks are not supposed to return any value. They get a parameter passed by reference; they just need to alter it.
apaderno avatar
us flag
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.
DevNooby avatar
nz flag
So there is no way to change the method tfa_basic_tfa_ready_require from the tfa_basic module?
No Sssweat avatar
ua flag
`how do I determine which of their methods is a hook?` Read the code [block comment](https://git.drupalcode.org/project/tfa_basic/-/blob/7.x-1.x/tfa_basic.module#L277). `So there is no way to change the method tfa_basic_tfa_ready_require from the tfa_basic module?` No, you would create your custom module and implement the hook function there. In other words, just do what leymannx said, m'kay?
Score:0
us flag

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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.