Here is part of flag_link
// The flag links are actually fully rendered theme functions.
// The HTML attribute is set to TRUE to allow whatever the themer desires.
$links['flag-' . $flag->name] = array(
'title' => $flag->theme($flag->is_flagged($entity_id) ? 'unflag' : 'flag', $entity_id),
'html' => TRUE,
);
I can customise the flag.tpl.php to make the link look how I want. The problem is, cores theme_link will wrap all this in a span because a href has not been specified. So now the html for my node links will look like
<ul>
<li><a>Comment link or something</a></li>
<li><span><a>Flag link gets wrapped in a span</a></span></li>
</ul>This is breaking my theme as the jquery selectors only tweak an <a> following a <li>. It would make other themes look dodgy as well, as the spans might get some CSS padding which the other links wont have. Its really annoying!
I could not find another way to get the link array as a 'proper-link-array' (with href, title, attributes>class, etc). A new function to return this would be great, because I could replace the links in a node hook or something. Or even the ability to return an un-themed link in the flags options (which will get themed as a normal link).
Incase this never gets addressed and other people are also looking for a solution, I worked my way around it. I made a flag--FLAGNAME.tpl.php for my theme. I made its contents (in php tags, because its a html based file)
$output = array(
'href' => $link_href,
'title' => $link_text,
'attributes' => array(
'title' => $link_title,
'class' => array($flag_classes),
'rel' => 'nofollow',
),
'html' => TRUE,
);
print serialize($output);
which returns the array as a string. Then in my themes template.php I have the code
function THEME_preprocess_node(&$vars) {
$flag = unserialize($vars['content']['links']['flag']['#links']['flag-FLAGNAME']['title']);
$vars['content']['links']['flag']['#links']['flag-FLAGNAME'] = $flag;
Comments
Comment #1
joachim commentedI'm not sure I follow entirely.
Are you saying the problem is that this:
> 'title' => $flag->theme($flag->is_flagged($entity_id) ? 'unflag' : 'flag', $entity_id),
is too baked-in already, because it's a fully-made HTML link already?
Comment #2
neRok commentedYes. And when the nodes links get themed by theme_links without a href set, it wraps the title in span tags.
Here is some of the intro text to theme_links
and the relevant code from theme_links
Comment #3
joachim commentedThe thing is, the flag theme function is designed to output a flag link that can be used anywhere -- in your own templates, by Views, and in node links.
I'm not sure how we can keep that generality and make the change you want, but then again, I've not looked at this part of the flag module much. So feel free to suggest changes you think will work! :)
Comment #4
neRok commentedIve not used flags a lot, and only ever for very basic use cases, so I dont know why things are the way the are. I also foresee that you would not want to break existing users functionality, so you would have to work around that hurdle too.
Here are my thoughts...
1) Why does the link need to be themed anyway? Lets say I call flag_create_link in node.tpl.php, and it returns a bunch of spans, maybe an image, and the link etc (as per flag.tpl.php). Why couldnt I just have all the spans etc in my node.tpl.php, and flag_create_link just gives me the link. Same scenario is possible with views.
2) A flag does not have to be part of a nodes links. It could just be attached to node as its own 'component', and the flag.tpl.php would make sense in this case. If I did want the flag to be part of the flag links, I would want it to be like all the links.
3) How to do all this in the back-end...? Im no expert, but I think when you create a flag link that it should be created as a standard link array, and there should only be 1 function that does this. With the link array, you can then attach it straight to the node links, or it could be sent straight to l() for output as a link anywhere, or it could be sent to theme_link (which would break the array down into variables and send to flag.tpl.php). So you only ever make the link once, and then you can use it for anything.
4) If it were coded up like (3), I think the front-end setup would be easy. You can have an option to attach the themed flag to the node, attach it as a node link, or not attach the flag at all.
Comment #5
joachim commentedThe flag template is responsible for the flag link itself, but also any messages that come back -- take a look at the contents of flag.tpl.php to see. I expect that's why we need the wrapper span too -- for the AJAX to have something to replace.
> 2) A flag does not have to be part of a nodes links. It could just be attached to node as its own 'component', and the flag.tpl.php would make sense in this case. If I did want the flag to be part of the flag links, I would want it to be like all the links.
See #1892930: Placement of flag links as pseudofields.
Comment #6
ivnishDrupal 7 is EOL. No more new features will be added.