I've started a dependent module in which I'm trying to find a way to change the link programatically (hook_link_alter) to amend the destination. However, these links are not your typical link and come through as array('title' => (...html...), 'html' => TRUE) instead of array('href' => String, 'title' => String, 'attributes' => array())

Without theming, (and barring regular expressions or taking apart the string) how am I to manipulate these links?

Comments

sirkitree’s picture

Also this would not allow me to manipulate any of the links that a user might throw into their template using flag_create_link()...

quicksketch’s picture

The contents of the flag link are more complicated than hook_link() normally allows, so we actually manually generate the link then put the entire completed result into $link['title'] and set $link['html'] = TRUE. You can change this links contents by overriding theme_flag(). Note that overriding this function is a bit unusual though, since Flag for Drupal 5 is a backport from Drupal 6. You'll need to copy theme_flag() into your template.php, then copy flag.tpl.php into your theme directory and update the theme function to use the .tpl.php file in your theme.

sirkitree’s picture

I can def understand that the flag links are more complex then hook_link() allows, but from a module perspective (vs. theme) there is now no way to manipulate their output? ... puts any modules trying to use this API at quite a disadvantage. Is there any better way then to parse the $link['title'], unset the $link and then create my own?

quicksketch’s picture

Title: hook_link_alter worthless? » Provide hook_preprocess_link() to Drupal 5
StatusFileSize
new1.06 KB

I realized you're actually wanting to affect the link from a *module* which makes this whole ordeal quite a bit more difficult, since Drupal 5 doesn't have hook_preprocess().

So to get around that problem, here's a patch that provides hook_preprocess_link() to Drupal 5, so you can alter the variables before they are sent to the flag.tpl.php file.

quicksketch’s picture

Category: support » task
Status: Active » Needs review
sirkitree’s picture

Status: Needs review » Reviewed & tested by the community

AH! Freaking beautiful! Exactly what I needed here. Works wonderfully! Please put this feature in the module!

mooffie’s picture

+1

This is a feature I too wanted, for two reasons:

1. This will make it possible to write a "Flag image" module (instead of this how-to).

2. This will simplify some how-to's: we won't need to provide an appendix for D5 users.

mooffie’s picture

Status: Reviewed & tested by the community » Needs work

Some comments on the patch:

- Won't module_implements('preprocess_flag') be faster? It caches the results.

- I wish we could prepend the list with the engine's and the theme's preprocess functions as well (or else "2. This will simplify some how-to's" won't apply).

- Since the code repeats in two places, it'd be nice to factor it out.

- Perhaps, in the second hunk, it'd be good to move the $suggestions to the $variables (and rename it to $template_files) so it could be added to (in a preprocess function).

quicksketch’s picture

StatusFileSize
new1.63 KB

Yeah module_implements would probably be better since it's likely we'll be doing this multiple times per page. This patch uses module_implements and creates a dedicated function for calling preprocess functions.

However, I don't add in the 'template_files' suggestions or call preprocess functions in the theme layer. These shouldn't be necessary since phptemplate already provides _phptemplate_variables() in Drupal 5 for these purposes. This patch only backports the ability to preprocess variable in the module layer, since there is no Drupal 5 equivalent without it.

quicksketch’s picture

Status: Needs work » Needs review
sirkitree’s picture

StatusFileSize
new1.65 KB

I reviewed and got a parse error: Function name must be a string

So I changed
${$module .'_preprocess_'. $hook}($variables);
to
call_user_func($module .'_preprocess_'. $hook, $variables);
and it works just fine.

Updated patch.

mooffie’s picture

Status: Needs review » Needs work

@quicksketch, I agree with all your points.

@sirkitree, do you happen to use PHP4?

(it's nice to know that our module still works with PHP4 ;-)

The manual page for call_user_func says "Note that the parameters for call_user_func() are not passed by reference". I think it means that the called function won't be able to modify $variables. Could you change it to the following pattern instead?

$function = $module .'_preprocess_'. $hook;
$function($variables);
mooffie’s picture

Status: Needs work » Needs review
StatusFileSize
new1.89 KB

Here's the patch with the change mentioned in #12 done. In addition, the call to template_preprocess_flag() isn't hardcoded now, because the function gets a $hook paramter.

sirkitree’s picture

Status: Needs review » Reviewed & tested by the community

I'm not using PHP4 actually, but this patch works on PHP5 whereas the other did not. I'm assuming something with the curly braces around the function name?

quicksketch’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thanks mooffie and sirkitree for the reviews and fixes. Apologies for trying to get fancy with my curly braces ;)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.