0

I created a custom module because I want to execute some actions when a users logs in / out.

function mymodule_user_login(&$edit, $account) {        
  $url = 'https://www.some_url';
  $additionalHeaders = '';

  $process = curl_init($url);
  curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders));
  curl_setopt($process, CURLOPT_HEADER, 1);
  curl_setopt($process, CURLOPT_USERPWD, $user.":". $password);
  curl_setopt($process, CURLOPT_TIMEOUT, 30);
  curl_setopt($process, CURLOPT_POST, 1);
  curl_setopt($process, CURLOPT_POSTFIELDS, $data);
  curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($process, CURLOPT_SSL_VERIFYPEER, FALSE);

  curl_close($process);
}

function mymodule_user_logout($account) {
  $url = 'https://www.some_url';
  $additionalHeaders = '';

  $process = curl_init($url);
  curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders));
  curl_setopt($process, CURLOPT_HEADER, 1);
  curl_setopt($process, CURLOPT_USERPWD, $user.":". $password);
  curl_setopt($process, CURLOPT_TIMEOUT, 30);
  curl_setopt($process, CURLOPT_POST, 1);
  curl_setopt($process, CURLOPT_POSTFIELDS, $data);
  curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($process, CURLOPT_SSL_VERIFYPEER, FALSE);

  curl_close($process);
}

I tested the code before to put it in the hook implementations, and it works.

Does anyone have the same problem?

4
  • What do you mean by "I tested the code in a view and works fine"? If it works in a view, why are then you are saying the hooks are not invoked?
    – apaderno
    Sep 10, 2011 at 21:45
  • Sorry for the confusion. I meant that the PHP code inside the hook works because I tested alone. So the problem is that the hooks are not triggered. Sep 10, 2011 at 22:24
  • 1
    May you report the code you are using? Without to see that, I can only report those hooks work, as they are used from Drupal core modules, and third-party modules. Saying that would not help you much, as then you would want to know in what your code is different from the code used from other modules.
    – apaderno
    Sep 10, 2011 at 22:31
  • Updated with the code. If I copy it and I paste this in a view ( PHP code ) it works, this is what make me think the function is not being triggered. Sep 11, 2011 at 17:01

2 Answers 2

4

If you are directly editing the code of a module that is already enabled, then the problem is that the list of hooks implemented by modules are cached by Drupal. Drupal doesn't have a setting for disabling this cache; the only cache you can disable/enable on Drupal is for the content served to the anonymous users.

Both the hooks you are referring are invoked by functions that, directly or indirectly, call module_implements(), which contains the following code:

  if ($reset) {
    $implementations = array();
    cache_set('module_implements', array(), 'cache_bootstrap');
    drupal_static_reset('module_hook_info');
    drupal_static_reset('drupal_alter');
    cache_clear_all('hook_info', 'cache_bootstrap');
    return;
  }

  // Fetch implementations from cache.
  if (empty($implementations)) {
    $implementations = cache_get('module_implements', 'cache_bootstrap');
    if ($implementations === FALSE) {
      $implementations = array();
    }
    else {
      $implementations = $implementations->data;
    }
  }

The cache containing the list of the implemented hooks is cleared when a module is updated, enabled, or disabled. Actually, it is enough you visit the page showing the list of the modules, click on the button to save the configuration, and the cache should be automatically reset; if that doesn't work, then you can disable and then re-enable a module.

I see the code is using variables that are not defined, nor set, by the code you are showing; I take you didn't show all the code, but if you are effectively showing all the code, then the $user, $password, and $data variables are not initialized.

As side note, you should use drupal_http_request(), instead of using the cURL functions.

1
  • Thanks for the answer! No, I'm not showing all the code, all variables are defined, the only thing missing are variables definition. Sep 11, 2011 at 21:58
1

It sounds like Drupal isn't aware of your new hooks. When you add new hooks to a module you need to clear your caches. If you're using drush run drush cc all or in your browser visit http://example.com/admin/config/development/performance

1
  • Since I'm on my dev server I disabled cache so think this may not be the problem. Thanks @Dave Sep 11, 2011 at 17:45

Your Answer

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

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