Closed (fixed)
Project:
Flag
Version:
5.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
31 Oct 2008 at 00:54 UTC
Updated:
17 Nov 2008 at 21:02 UTC
Jump to comment: Most recent file
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?
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | flag_preprocess_drupal5.patch | 1.89 KB | mooffie |
| #11 | flag_preprocess_drupal5.patch | 1.65 KB | sirkitree |
| #9 | flag_preprocess_drupal5.patch | 1.63 KB | quicksketch |
| #4 | flag_preprocess_drupal5.patch | 1.06 KB | quicksketch |
Comments
Comment #1
sirkitree commentedAlso this would not allow me to manipulate any of the links that a user might throw into their template using flag_create_link()...
Comment #2
quicksketchThe 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.
Comment #3
sirkitree commentedI 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?
Comment #4
quicksketchI 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.
Comment #5
quicksketchComment #6
sirkitree commentedAH! Freaking beautiful! Exactly what I needed here. Works wonderfully! Please put this feature in the module!
Comment #7
mooffie commented+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.
Comment #8
mooffie commentedSome 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).
Comment #9
quicksketchYeah 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.
Comment #10
quicksketchComment #11
sirkitree commentedI 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.
Comment #12
mooffie commented@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?
Comment #13
mooffie commentedHere'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.
Comment #14
sirkitree commentedI'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?
Comment #15
quicksketchCommitted. Thanks mooffie and sirkitree for the reviews and fixes. Apologies for trying to get fancy with my curly braces ;)