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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

agentrickard’s picture

Discussed with merlin in IRC. This will be refactored as a core Views patch.

agentrickard’s picture

Status: Needs work » Needs review
FileSize
6.53 KB

Ok, 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.

agentrickard’s picture

FileSize
58.6 KB

Updated screen shot. This field is exposed to all views types.

agentrickard’s picture

FileSize
6.56 KB
55.31 KB

Fixed an oversight in the Suffix name and re-shuffled the order of the fields.

agentrickard’s picture

FileSize
6.47 KB

Very slight re-roll to remove field size constraints.

merlinofchaos’s picture

Ok, 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!

agentrickard’s picture

Makes sense.

agentrickard’s picture

FileSize
7.9 KB

Done. We read the handlers for arguments and fields and supply a list.

drewish’s picture

Status: Needs review » Needs work

Some minor stuff:

+      '#description' => t('The text to disaply for this link. You may enter the view arguments, such as %1 or database field names, such as [nid].'),

display

+      '#description' => t('The drupal path or complete URL for this link.  You may enter the view arguments, such as %1 or database field names, such as [nid].'),

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.

+    // We don't do anything here.  But doing so avoids errors in trying to load a non-existent field.

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.

merlinofchaos’s picture

Let's make an expansion of this a phase 2 thing.

agentrickard’s picture

FileSize
8.13 KB

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

agentrickard’s picture

Status: Needs work » Needs review
mean0dspt’s picture

very 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

text<em>important text</em>

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

prom3theus’s picture

Hello! 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)

agentrickard’s picture

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

mean0dspt’s picture

Thank you, will try new patch. One question though

RCS file: /cvs/drupal-contrib/contributions/modules/views/includes/view.inc,v

+    // Store the arguments for later use.
+    $this->build_info['substitutions'] = $substitutions;
+
+    // Store the arguments for later use.
+    $this->build_info['substitutions'] = $substitutions;
+

Is this repetition necessary?

agentrickard’s picture

Status: Needs review » Needs work

No. It is a glitch caused by re-rolling the patch. Perhaps you can fix it and post a revised patch.

mean0dspt’s picture

Title: Generic Link handler » I'd love to but can't

Sorry, but I currently have no CVS installed and work in Windows environment. So I'd pass on revising the patch.

mean0dspt’s picture

Title: I'd love to but can't » Generic Link handler

Ups, ocassionally changed the issue title...

agentrickard’s picture

Status: Needs work » Needs review
FileSize
8.51 KB

Removed. This still applies, with a little offset.

dww’s picture

Status: Needs review » Needs work

This 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 a Link 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...

agentrickard’s picture

IMO, 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.

agentrickard’s picture

Status: Needs work » Needs review
FileSize
9.17 KB

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

agentrickard’s picture

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

  // Sample field output...
  $output = filter_xss_admin($this->prefix);
  $output .= l($this->result, 'custom/link/path');
  $output .= filter_xss_admin($this->suffix);

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?

merlinofchaos’s picture

FileSize
13.39 KB

Ok, 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.

agentrickard’s picture

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

merlinofchaos’s picture

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

agentrickard’s picture

Agreed. But I have to have the Global: Text field or the current work I am doing will break....

merlinofchaos’s picture

Ok, will get it in with this (this is why I posted a patch which is unusual for me =)

merlinofchaos’s picture

FileSize
18.71 KB

I'm like a kid in a candy store with this one. This also gained automatic field trimming.

merlinofchaos’s picture

FileSize
30.68 KB

Ok, 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.

sun’s picture

Component: Miscellaneous » Code
Status: Needs review » Needs work

While scanning through the patch:

+++ modules/comment/views_handler_field_node_new_comments.inc
+      $this->options['alter']['fragument'] = 'new';
mean0dspt’s picture

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

merlinofchaos’s picture

Aliasing will happen automatically, as l() is smart and checks.

If you set a link to node/[nid] then it'll just happen.

mean0dspt’s picture

Thank you. Sorry to bother, but I guess something like
[title] <?php image_display([iid], 'thumbnail'); ?> in the link title field
is too much to ask for?
I can't see non-php solutions right now, I'm not good with this tokens stuff

agentrickard’s picture

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

drewish’s picture

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

drewish’s picture

Status: Needs work » Needs review
FileSize
30.56 KB

re-roll with merlin's fix from IRC.

drewish’s picture

FileSize
30.56 KB

another re-roll that fixes the issue sun spotted.

merlinofchaos’s picture

Status: Needs review » Fixed

Committed!

agentrickard’s picture

Hm. 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:

mypath/[type]/[title]

But as it stands, [type] is ignored by the Title field element.

agentrickard’s picture

Status: Fixed » Needs work
FileSize
74.65 KB

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

Text: [title]
Link path: mypath/[type]/[title]

All links point to item 10 in the table.

mean0dspt’s picture

Replacement 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

merlinofchaos’s picture

It only lists other fields prior to your field.

What other tokens were you expecting?

agentrickard’s picture

Tokens for all fields in the View display, plus all arguments.

upupax’s picture

This is a really new cool feature!
I'm really curious to see it in action!
Thanks guys!

merlinofchaos’s picture

Status: Needs work » Fixed

Ok, 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.

ayalon’s picture

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

drewish’s picture

yeah i've found myself needing to add classes to the links and resorting to custom field formatters.

Status: Fixed » Closed (fixed)

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

aleagi’s picture

+1 here!

How can I add a custom attibute (like target, class or rel) to a single link?

Thanks!

tbertin’s picture

Has 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!

tbertin’s picture

Status: Closed (fixed) » Needs review

sorry to bump, forgot to change the status

agentrickard’s picture

Status: Needs review » Closed (fixed)

The feature is in 6.x.2.6. There is no support for attribute handling. That needs a separate issue.