1

I have a Rule set up that sends a mail to a certain user with a link to a certain node. This link is altered using hook_url_outbound_alter to include additional details.

The problem is that hook_url_outbound_alter also alters the URL everywhere else. Closing an overlay on the node view page rewrites the URL, clearing the cache (admin toolbar + devel) on the node pages rewrites the URL and so on.

I already attempted to check for an absolute URL in $options, but that does not solve the problems above.

function mymodule_url_outbound_alter(&$path, &$options, $original_path) {
  if (preg_match('|^node/([0-9]*)|', $path, $matches) && $options['absolute'] {
      $options['query']['parameter'] = $foo;
    }
  }
}

Can I use hook_url_outbound_alter() to only alter URLs when it was invoked from a specific module or something?

1

Can I use hook_url_outbound_alter() to only alter URLs when it was invoked from a specific module or something?

You cannot, if that module doesn't put in $options (the same argument passed to url()) something that makes you understand that. To make an example, entity_uri() uses $options['entity_type'] and $options['entity']; if you were trying to intercept the calls to url() done from entity_uri(), you could check the presence of those options.
Otherwise, the context given to hook_url_outbound_alter() doesn't allow to understand which module called url(). Maybe you could get that using debugging functions, but I am not sure I would put that kind of code in a module.

Since you seem to trying to alter links in an email sent from Drupal, you could implement hook_mail_alter(), assuming the emails are sent using the correct way of sending them with Drupal.

| improve this answer | |
  • Thanks for answering. I am sending the mails with rules, so there is no way to add any other metadata to $options. Do you know if hook_mail_alter() works with rules? – Neograph734 Jul 13 '16 at 8:32
  • 1
    As long as the module uses drupal_mail(), it works. Alternatively, if the Rules module doesn't use drupal_mail() to send emails, I would create a rule action to do what you need. – kiamlaluno Jul 13 '16 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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