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'));
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quicksketch’s picture

You 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.

mansspams’s picture

other options:

allow only flag
allow only unflag

quicksketch’s picture

Version: 6.x-1.x-dev » 6.x-2.x-dev

Considering this is an API change, it will be handled in the 2.x version.

allow only flag
allow only unflag

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.

ManyNancy’s picture

Please allow adding css class as option, and preferably to the anchor rather than the wrapper.

texas-bronius’s picture

An 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.

JordanMagnuson’s picture

Version: 6.x-2.x-dev » 7.x-2.x-dev

I 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:

  • 'attributes': An associative array of HTML attributes to apply to the anchor tag. If element 'class' is included, it must be an array; 'title' must be a string; other elements are more flexible, as they just need to work in a call to drupal_attributes($options['attributes']).
  • 'html' (default FALSE): Whether $text is HTML or just plain-text. For example, to make an image tag into a link, this must be set to TRUE, or you will see the escaped HTML image tag. $text is not sanitized if 'html' is TRUE. The calling function must ensure that $text is already safe.
  • 'language': An optional language object. If the path being linked to is internal to the site, $options['language'] is used to determine whether the link is "active", or pointing to the current page (the language as well as the path must match). This element is also used by url().
  • Additional $options elements used by the url() function.

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)?

joachim’s picture

Version: 7.x-2.x-dev » 7.x-3.x-dev

Upping the version.

mrweiner’s picture

I'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.

joachim’s picture

Issue tags: +Intermediate

Care to work on a patch for this? :)

(Tagging.)

mrweiner’s picture

I 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. :)

joachim’s picture

I'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.

markhalliwell’s picture

Title: Add $options parameter to flag_create_link() » Add $variables parameter to flag_create_link()
Component: Miscellaneous » Flag core
Issue summary: View changes
Status: Active » Needs review
FileSize
1.62 KB

Actually 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.

Fabianx’s picture

Status: Needs review » Reviewed & tested by the community

The 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.

joachim’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Intermediate

Committed, 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 :)

Status: Fixed » Closed (fixed)

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