robertgarrigos's picture

I'm porting a drupal module which uses hook_preprocess_link, missing in backdrop. How should I replace this function?

Accepted answer

I see. You are right. In D7, l() would call theme('link'...) when possible. That was removed from Backdrop. What a shame. That makes it really hard to theme links or modify them through preprocessing, when they are created directly through l() (which is VERY common in combination with t()).

Perhaps you should open a feature request in the issue queue? I don't think it'd be hard to bring that functionality back.

Comments

robertgarrigos's picture

I should say that I know that there is the hook_preprocess_hook() function which is the one to use. However, hook_preprocess_link() in drupal can be used on a module file to preprocess ANY link generated by drupal. This is not working in Backdrop.

Hi Robert.

I'm testing this and hook_preprocess_link() does work as expected, but only if you use the function theme('link', array('text' => 'My text', 'path' => 'my-path')). It will not be invoked if you use a render element of '#type' => 'link', or even if you use l() or url().

The call (as is usual with preprocess function) is only invoked when you theme something. I believe this is the same behavior as Drupal, so my guess is that it's not working for you for a different reason in your ported module.

I can post here a small custom module that shows this behavior. Just let me know.

robertgarrigos's picture

That's the point: backdrop uses l() to render a local task menu, for instance. Also, I'm having troubles to preprocess view fields...

robertgarrigos's picture

I'm porting Content lock drupal module. It blocks concurrent editions by adding a token to the node edit url. It does it with hook_preprocess_link() and it works for any node edit link, whether it is a menu local task link, a contextual link or an edit link in a view.

In backdrop, I need to have a hook_preprocess_menu_local_task and a hook_preprocess_views_view_field. This last one function needs to handle the token adding with regex, which is working, but it's not nice. It's a shame backdrop didn't preserve this preprocess functionality for all links. I guess there was a reason for that.

Thanks, argiepiano.

I see. You are right. In D7, l() would call theme('link'...) when possible. That was removed from Backdrop. What a shame. That makes it really hard to theme links or modify them through preprocessing, when they are created directly through l() (which is VERY common in combination with t()).

Perhaps you should open a feature request in the issue queue? I don't think it'd be hard to bring that functionality back.