Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
flag_create_link() could benefit from an $options parameter.
What could it be useful for?
1. Changing the link type.
2. Adding to the "suggestions" array.
3. ...
Examples:
// Disable AJAX:
print flag_create_link('bookmarks', 910, array('link_type' => 'normal'));
// Use the "flag-teaser.tpl.php" template, if exists.
print flag_create_link('bookmarks', 910, array('suggestion' => 'teaser'));
Comment | File | Size | Author |
---|---|---|---|
#12 | 405580-flag-link_variables-12.patch | 1.62 KB | markhalliwell |
Comments
Comment #1
quicksketchYou could also easily change the link text with this approach. I'm not sure if this would be a good place for passing in template suggestions, though it does seem like it'd be easy enough to implement. Hrmhmm. Maybe this could be useful for advanced uses of Flag in contributed modules.
Comment #2
mansspams CreditAttribution: mansspams commentedother options:
allow only flag
allow only unflag
Comment #3
quicksketchConsidering this is an API change, it will be handled in the 2.x version.
This would not be a good idea to handle with flag_create_link(), since it is essentially a theme-level display for actual functionality. Even if the link is not displayed, the content may still be flagged if the API allows it. $flag->access() and hook_flag_access() should be used instead if wanting to deny/allow flagging access.
Comment #4
ManyNancy CreditAttribution: ManyNancy commentedPlease allow adding css class as option, and preferably to the anchor rather than the wrapper.
Comment #5
texas-bronius CreditAttribution: texas-bronius commentedAn options array might also include "what" to be clicked, so when called from a view, even an imagecache preset could be passed in to allow it to be clicked rather than the normal flag-generated field below it.
Comment #6
JordanMagnuson CreditAttribution: JordanMagnuson commentedI agree that flag_create_link needs an $options parameter. I was shocked to take a look at the code today and find that I could not even add a simple css class to the link that is being generated.
Because this is a theme-level function that ends up generating a link, it essentially sits on top of drupal's built-in l function. As such, it seems like it would make sense for this function to handle most of the options the the l function handles, namely:
I realize that some of this stuff is not necessarily relevant to a flag link, and some of it is handled via the administrative UI (e.g. the link title)... but surely flag_create_link should allow for something like css styling (and even overriding options set in the UI)?
Comment #7
joachim CreditAttribution: joachim commentedUpping the version.
Comment #8
mrweiner CreditAttribution: mrweiner commentedI'm going to add another +1 for this functionality being added. Being able to add at least a class to the flag link would be awesome. Essentially, I'm going to echo all of JordanMagnuson's sentiments. I'm surprised that this isn't already readily available, especially since the issue was opened nearly 4 years ago.
Comment #9
joachim CreditAttribution: joachim commentedCare to work on a patch for this? :)
(Tagging.)
Comment #10
mrweiner CreditAttribution: mrweiner commentedI could try, but I have no idea where to begin. My knowledge on writing modules is quite minimal. If you want to point me in the right direction, I can try and give it a shot. :)
Comment #11
joachim CreditAttribution: joachim commentedI'll try and break it down into steps :)
- add a new parameter to flag_create_link(), called $options. This should be optional, with a default of array().
- add documentation for this too in the function docblock
- pass this in to the call to $flag->theme()
- Next problem is that $flag->theme() already has a 3rd parameter. I'd like this to remain at the end, so we actually need to change rather than just extend its signature. That's ok, we're still in alpha on a new branch!
- Knock-on consequence: find all calls to $flag->theme() that use the 3rd param and change them. AFAICT that's only flag_build_javascript_info().
- pass the options array as a new item in the array in the two calls to theme().
- Another knock-on consequence: adding a theme variable requires a change to the declaration in flag_theme().
- Finally, do the actual work with the $options array in template_preprocess_flag(), before the theme template gets output.
At this point we have to decide what to actually do with the new parameter! There are some good suggestions above, but:
> Because this is a theme-level function that ends up generating a link, it essentially sits on top of drupal's built-in l function.
That's not actually true. The template file flag.tpl.php makes the link out of raw HTML itself.
So we need to figure out what to actually support here.
Comment #12
markhalliwellActually we should just pass
$variables
to the$flag->theme()
method, which in turns passes to core's theme API. We can do anything we want on the preprocess/process/template level to enhance or changes "options". We don't need to worry about assigning set "options", which would just muddy the waters even more. Here's a patch.Comment #13
Fabianx CreditAttribution: Fabianx commentedThe last patch works very well and looks very nice in my code. It also leaves the flexibility to the themer, who needs to use it.
Comment #14
joachim CreditAttribution: joachim commentedCommitted, with fix for comment wrapping.
git commit -m "Issue #405580 by Mark Carver: Added \$variables parameter to flag_create_link() to pass through to flag.tpl.php." --author="markcarver "
Not sure if this needs a change notice, as it's an addition -- if someone feels motivated to write one, go ahead :)