Ever found yourself needing to add a custom link to a view?
Something like "I need to link this node to my custom callback at path node/[nid]/mycallback".
Currently, you have to do this in the theme layer. I have tried to abstract this logic into a generic link view handler.
The attached zip file is a stand-alone module, for testing purposes.
See the png for a screenshot.
What works:
Link Text: [title]
Link Path: node/[nid]/somecallback
What needs love:
Argument handling. The argument handling right now is crude and a hack, since we want to respect %1, %2, etc., but those arguments are returned as a simple positional array that does not follow the same ordering.
Comment | File | Size | Author |
---|---|---|---|
#42 | Picture 2.png | 74.65 KB | agentrickard |
#39 | views_349178.patch | 30.56 KB | drewish |
#38 | views_349178.patch | 30.56 KB | drewish |
#31 | base-link3.patch | 30.68 KB | merlinofchaos |
#30 | base-link2.patch | 18.71 KB | merlinofchaos |
Comments
Comment #1
agentrickardDiscussed with merlin in IRC. This will be refactored as a core Views patch.
Comment #2
agentrickardOk, proper patch, which adds a new view_handler_field_link as a global field.
I also added a prefix and suffix for the link text, in case you want to do something like:
(View link text)
Note that if you leave the Link Path blank, you end up with a custom text field that can concatenate one or more other fields.
There may be a better way to handle missing arguments as well.
Comment #3
agentrickardUpdated screen shot. This field is exposed to all views types.
Comment #4
agentrickardFixed an oversight in the Suffix name and re-shuffled the order of the fields.
Comment #5
agentrickardVery slight re-roll to remove field size constraints.
Comment #6
merlinofchaos CreditAttribution: merlinofchaos commentedOk, I am liking what I see so far. This is a long way toward something people really want.
I think we could just list all of the possibly known fields right there, rather than telling the user to check theme: information for the list of fields. It should be easy enough to extract that information from the view. $view->display_handler (or $this->display maybe I forget) ->get_handlers('field') I think. Or 'fields'. (It's late, my memory is fuzzy just now). Then simply print a list of IDs and field names they go to. Likewise we could list arguments and actually state which argument they go to.
Otherwise, this is a very nifty feature!
Comment #7
agentrickardMakes sense.
Comment #8
agentrickardDone. We read the handlers for arguments and fields and supply a list.
Comment #9
drewish CreditAttribution: drewish commentedSome minor stuff:
display
Has an extra space after the period. Need proper capitalization on Drupal. Also I'd like a bit clearer distinction made that %1 are Views arguments and [nid] are fields added to the view. Also mention the list found below.
Extra space.
On a more conceptual level I'd like to see this used to generate the edit/delete node and user links. Meaning that all the values could be passed into the field definition. We'd also need to have the ability to control visibility from a permission check.
Comment #10
merlinofchaos CreditAttribution: merlinofchaos commentedLet's make an expansion of this a phase 2 thing.
Comment #11
agentrickardRevised according to comments in #9. (My old English-teacher brain forces me to put two spaces after a period.)
The purpose here is to generate links to other pages, so access control was not a consideration. I think that access controlled links might need their own handler.
One obvious direction for expansion is the creation of a 'generic text field' that functions in the same way. it would be a textarea field that uses the same logic for tokens. Not sure how to build that on top of the existing code. But note that if you leave the 'link path' empty here, you will get plain text output.
Comment #12
agentrickardComment #13
mean0dspt CreditAttribution: mean0dspt commentedvery nice feature indeed, but after I've tried it, I found out that it doesn't accept html inside the Link text field. For example, I can't make links like
to appear as my custom link issue
It's a real stopper for me, because that's exactly what i intended to use the patch for (see the link for my case).
Also, the list of valid tokens in Replacement patterns is really nice feature, but it looks like it actually doesn't list all avaliable tokens, only a few of them
Still, a great field to use in different situations, great work
Comment #14
prom3theus CreditAttribution: prom3theus commentedHello! This is a great issue, i'm very happy to it becouse it was a need for my website. Thanks for this good work! You save my life :)
(sorry for bad EN)
Comment #15
agentrickard@mean0dspt
That's an oversight in the patch, perhaps. Here is a revised version that uses filter_xss_admin() for all parts of the output, and forces the link to use HTML. It also cleans up one small code error.
Comment #16
mean0dspt CreditAttribution: mean0dspt commentedThank you, will try new patch. One question though
RCS file: /cvs/drupal-contrib/contributions/modules/views/includes/view.inc,v
Is this repetition necessary?
Comment #17
agentrickardNo. It is a glitch caused by re-rolling the patch. Perhaps you can fix it and post a revised patch.
Comment #18
mean0dspt CreditAttribution: mean0dspt commentedSorry, but I currently have no CVS installed and work in Windows environment. So I'd pass on revising the patch.
Comment #19
mean0dspt CreditAttribution: mean0dspt commentedUps, ocassionally changed the issue title...
Comment #20
agentrickardRemoved. This still applies, with a little offset.
Comment #21
dwwThis is very slick, and almost what I need to solve point (D) over at #76725-43: Refactor project issue module to use Views. ;) Basically, I need the first column at http://drupal.org/project/issues -- a view column of project titles that link to project/issues/[project_shortname], not to node/[project_nid].
However, there seem to be two problems with this patch, one fundamental, one minor, that prevent me from using it in its current form for project_issue:
A) [fundamental]: There's no way to sort the resulting field. In my case, the link text is using the node title from a relationship in the view. I'd be happy to sort directly on that field, but the generic link field provides no way to specify that. When I first started thinking about my problem over at #76725, I thought what I really wanted was instead of the existing
[x] Link this field to its node
checkbox, I wanted aLink this field to [_________________]
text area that supported the kind of arg/field placeholders you've got here. Then, the field itself would be the underlying field I care about, I could sort on it "natively", etc, but I could link where I need to, instead of just to node/[nid].B) [minor]: My particular view has 2 title fields, one directly from the project issue nodes, and another from a relationship with the project nodes. However, when I tried to use [title_1] as my link text as per the "Replacement patters" fieldset, I would just see "[title_1]" as my link text, instead of the field that was supposed to represent.
Thanks for working on this, it promises to be a very slick addition to views once it's ready and in...
Comment #22
agentrickardIMO, merlin may have different opinions.
A) Cannot be supported. Since the field itself does not exist in the database, there is no way to sort the query. Specifying a sort field for this element may be possible, but would take serious code and UI work. I think it is beyond the scope of the initial patch. (Think incremental improvements.)
Having custom links per-field is a different feature.
B) I was afraid of that. Detecting field aliases like that may or may not be possible. Needs testing.
Comment #23
agentrickardRevised patch that should fix issue #21 A, so that [name] and [name_1] are properly treated as unique fields.
I think the sorting of this field is a) beyond my abilities, and b) not critical enough to delay the patch.
If someone else can figure out how to sort on this field, I think that would be great. Perhaps another approach would be to take this same logic, and abstract it to be used as a link handler for any field.
I think that is probably the best way to go, but would be a separate patch.
Comment #24
agentrickardI was thinking about this issue this morning (#21 B) and feel that we should solve that separately.
I can conceive of a patch that would allow any Views field to define its own link path, possibly with a prefix and a suffix, that would wrap the output of the field in an href tag. Think of it like so:
Custom link path would allow the use of all arguments and fields available in the current display (%1, %2, [nid] and so on, as supported in the current patch.)
Doing so would mean moving some of the logic from views_handler_field_link.inc to a core file (not sure which one right now.) Then, we might make it so that any field definition can provide its own link (like the current 'link to node' option) or use the custom link creator. That, to me, is a better solution to the problem dww is trying to solve.
Any ideas about the proper home for the find/replace logic if we abstract this functionality?
Comment #25
merlinofchaos CreditAttribution: merlinofchaos commentedOk, I went crazy with #24. This makes it so that any field gets the ability to be rewritten and made into a link. These are controlled with separate checkboxes using dependency to reduce the UI burden. Also, this doesn't use PHP5 only semantics (Sorry Ken, PHP4 is required for Drupal 6 still).
I've tested this lightly and it works brilliantly. My one concern right now is that all of the existing 'link this to ' probably need to become aware of this and be intelligent about what they do.
Comment #26
agentrickardNot a problem. (I had to steal the private OO code snippets from mbutcher and Crell, anyway...)
Does this patch still allow the the creation of a custom stand-alone field, or do the links need to be tied to a specific field?
Comment #27
merlinofchaos CreditAttribution: merlinofchaos commentedRight now they need to be tied to a specific field but a Global: Text field should probably make it back into this which allows a field that basically just does this, which gives the same functionality as your patch entirely, plus allowing every field to have it as well.
Comment #28
agentrickardAgreed. But I have to have the Global: Text field or the current work I am doing will break....
Comment #29
merlinofchaos CreditAttribution: merlinofchaos commentedOk, will get it in with this (this is why I posted a patch which is unusual for me =)
Comment #30
merlinofchaos CreditAttribution: merlinofchaos commentedI'm like a kid in a candy store with this one. This also gained automatic field trimming.
Comment #31
merlinofchaos CreditAttribution: merlinofchaos commentedOk, another reroll. This one allows for alt text and integrates with all the existing render_link facilities; they are backward compatible so it shouldn't *break* people out htere using render_link, though we will need an API note that module authors using render_link style functionality should modify their behavior to use the built in link maker for greater flexibility.
Comment #32
sunWhile scanning through the patch:
Comment #33
mean0dspt CreditAttribution: mean0dspt commentedyou guys rock, this issue is developing very rapidly, but I have one thing unclear to me - how does this handler deals url-aliases?
When I use regular "link to node" feature, it does check for aliases and shows human-readable alias instead of node/lots_of_numbers
when I play with this handler (#23), it basicly offers to use [nid] in Replacement patterns, but I might be just missing something.
I'm a bit stuck with my issue, it looks like this patch should have all the functionality I need but it's a bit beyond my grasp to put all the pieces togeter
if I want to print_r an array with all that the view has to offer, what is the name of the array?
Comment #34
merlinofchaos CreditAttribution: merlinofchaos commentedAliasing will happen automatically, as l() is smart and checks.
If you set a link to node/[nid] then it'll just happen.
Comment #35
mean0dspt CreditAttribution: mean0dspt commentedThank you. Sorry to bother, but I guess something like
[title] <?php image_display([iid], 'thumbnail'); ?>
in the link title fieldis too much to ask for?
I can't see non-php solutions right now, I'm not good with this tokens stuff
Comment #36
agentrickardTotally insecure. You will never be allowed to put PHP in the title line.
The file field should provide a token for you, which will be all that you need. Expand the "Replacement options" list, and you should see what is available.
Tokens are the best solution to the problem. If you need custom PHP code, use the theme layer.
You _can_ however, put an
<img>
tag in there.Comment #37
drewish CreditAttribution: drewish commentedseems like the latest version of this is breaking the existing link to node functionality. both node.title and node.type no longer link for me after applying this patch.
Comment #38
drewish CreditAttribution: drewish commentedre-roll with merlin's fix from IRC.
Comment #39
drewish CreditAttribution: drewish commentedanother re-roll that fixes the issue sun spotted.
Comment #40
merlinofchaos CreditAttribution: merlinofchaos commentedCommitted!
Comment #41
agentrickardHm. The Replacement patterns only seem to show the options available as part of the active field. By design?
In the original patch, the link path pattern could use any arguments or field token set in the view display. This version seems to remove that. Why should I not be able to create the following path for my Node Title:
But as it stands, [type] is ignored by the Title field element.
Comment #42
agentrickardCustom text does not work as designed. It is taking one value, not the value of the active row.
The screen shot shows the output for Custom Text with values:
All links point to item 10 in the table.
Comment #43
mean0dspt CreditAttribution: mean0dspt commentedReplacement options in my view show only 2 tokens -
Fields
[title] == Node: Title
[iid] == Image attach: Attached image
that's why I was doubting that it does list everything in #13
Well, I was hoping to use some UI to manage things, but it looks like I'll have to go back to template.php
Comment #44
merlinofchaos CreditAttribution: merlinofchaos commentedIt only lists other fields prior to your field.
What other tokens were you expecting?
Comment #45
agentrickardTokens for all fields in the View display, plus all arguments.
Comment #46
upupax CreditAttribution: upupax commentedThis is a really new cool feature!
I'm really curious to see it in action!
Thanks guys!
Comment #47
merlinofchaos CreditAttribution: merlinofchaos commentedOk, table style is now fixed to work with this, and a documentation note is in the changelog (and will be in the release notes) that order of field rendering matters.
Comment #48
ayalon CreditAttribution: ayalon commentedGreat feature! I'm using this to link a node title to a file of the node.
What is necessary to add some attributes to the link? I would like to add a target="_blank" to the link. Is this possible?
Comment #49
drewish CreditAttribution: drewish commentedyeah i've found myself needing to add classes to the links and resorting to custom field formatters.
Comment #51
aleagi CreditAttribution: aleagi commented+1 here!
How can I add a custom attibute (like target, class or rel) to a single link?
Thanks!
Comment #52
tbertin CreditAttribution: tbertin commentedHas this feature been integrated into Views 6.x-2.6 or does the patch need applied? If the patch needs applied, can someone explain how to do so? Thanks!
Comment #53
tbertin CreditAttribution: tbertin commentedsorry to bump, forgot to change the status
Comment #54
agentrickardThe feature is in 6.x.2.6. There is no support for attribute handling. That needs a separate issue.