I have a view where I want the link text to be different in the link so I can't use any of the existing formatters to do that. As a result I'm using the default view formatter on the link field and trying to do rewrites instead.
So I have "rewrite the output of this field" for the text of the field which I'm using another title field pulled into the view as well. Works fine.
Using "Output this field as a link" which works fine for link (using raw link [field_global_link-url]).
Them problem is I lost the link "target" attribute now which I was hoping to get back with the raw attribute value that is available:
[field_global_link-attributes] == Raw attributes
The problem is, How do I use this?
I tried [field_global_link-attributes][target] in the target field but I'm assuming that's not how this works.
Please let me know how this Replacement Pattern is supposed to be used.
Comment | File | Size | Author |
---|---|---|---|
#33 | 1508614-32.patch | 2.76 KB | lokapujya |
| |||
#32 | 1508614-32-interdiff.txt | 2.07 KB | lokapujya |
#32 | 1508614-32.patch | 5.04 KB | lokapujya |
| |||
#29 | 1508614-29.patch | 2.71 KB | Marco Vervoort |
#27 | 1508614-27.patch | 3.27 KB | Marco Vervoort |
Comments
Comment #1
zhuber CreditAttribution: zhuber commentedHaving trouble with this as well...
Comment #2
jhodgdonI just looked at the code for the Link module. It looks like it is just using the native Views - Core Field API integration, so that "field" is just the database column... meaning that it has a serialized (or possibly unserialized) data array containing the rel, target, and other attributes of the field item. So I'm pretty sure it wouldn't be possible to access it using Views field rewrites. You would need to write some PHP code to extract values.
Comment #3
jhodgdonActually, I think we should turn this into a feature request.
Comment #4
jeeba CreditAttribution: jeeba commentedHaving the same problem. We have the option to chang the target when editing the node directly but in views, i just can't access to the selected target. Suscribing to see what we can do here
Comment #5
debo7debo CreditAttribution: debo7debo commentedWow, it's surprising this integration wasn't included already. I will subscribe for updates on this, but for now I will have to make a new field for that.
Comment #6
nielsvoo CreditAttribution: nielsvoo commentedIndeed it's a pity this doesn't work but creating a new field using the following manual will for fill your needs.
- Create a taxonomy Link (target) and create one item _blank in it.
- In your content type create a field Ref to terms named "Link (Target)" and choose the above taxonomy
- Now open your view and add the new field to it, turn display off and set to "text only"
- Arrange the field above your image
- Use this new field ass token in the target field of the rewrite section.
Done..
Succes
Comment #7
kholloway CreditAttribution: kholloway commentedThank you for submitting this result. It will work but it's essentially using other fields and taxonomy to manually add in the "target" field again. I would hope the issue is fixed though to avoid having to do this as it just adds unnecessary overhead to the content type.
This solution also won't address allowing user choice in situations where more than one link is used. For example it essentially hard codes the target to _blank for every link. If there is a multi-select field of links where the user could give links different targets trying to do this solution would be a bit chaotic.
Definitely a nice work around in situations where one link is used. My only concern with it (and it may be confusion on my side) is I'm not sure how this is different than just hard coding the target to _blank in the rewrite using text only.
Comment #8
ladybug_3777 CreditAttribution: ladybug_3777 commentedAnother vote for this attribute to be available via views!
In response to kholloway, it's different than hard coding the target because the user may have select some links to open in a new window and others not to. If I hard code to _blank I'm making ALL links in my view open in a new window.
Comment #9
graindor CreditAttribution: graindor commentedI agree with the other comments. It would make sense to include this option since the target of a link may be different for each node and our authors want to set the target (same / new window) for each node individually.
Comment #10
joshuautley CreditAttribution: joshuautley commented#6 is the best answer.
In my case I just created a select list with one value, "_blank|New Window".
The default is "- None -".
Using rewrite in views based on what #6 said works perfectly.
Don't forget to set the Link Target to "Default (no target attribute)" for the Link field type since you are now handling this with a different field type.
However, having this feature included with module would be great. (=
Comment #11
gravisrs CreditAttribution: gravisrs commentedThe quickest way to tamper with link field is to use views_php:
In output code just use this snippet and modify for your cause:
Example:
I'm having field_logo in my view (core image field, styled, hidden in view) and I want it linked (field_link, also hidden in view) I just added views_php field (at the end) :
Comment #12
dqd#5 Please read Descriptions of the Priority and Status values can be found in the Issue queue handbook. Such "tags" should not be set from an emotional and subjective point of view. Interaction with another module is not a "major" issue. Thanks for understanding. Of course I fully agree with the wish to have that feature available.
Comment #13
nonsieHere's a way to alter the link title in your views output with a hook_views_pre_render(). It's tied to a specific view and field but in most cases should be sufficient.
In this case I want the link text to be the node title (the link field named field_event_link only collects url and target from the user).
Comment #14
benjaminbradley CreditAttribution: benjaminbradley commented+1 subscribing upvote for this feature
#11 worked for me as a work-around
Comment #15
prophet108 CreditAttribution: prophet108 commentedThis feature would be appreciated.
Comment #16
lokapujyaI will work on this if anyone wants to help.
Comment #17
lokapujyaAdd a replacement token for target.
Comment #18
lokapujyaRemoving a piece that shouldn't be there.
Comment #19
sumitmadan CreditAttribution: sumitmadan at QED42 commentedHi @lokapujya,
After applying #18 it displays the target token but it removes these tokens [field_link_field-url], [field_link_field-title], [field_link_field-attributes].
Comment #20
lokapujyaI think my code is replacing the handler. It should maybe add the handler. We'll see if it can have an array of handlers.
Comment #21
lokapujyaCannot add a handler. Needed to copy existing views handler functions and add to them.
Comment #22
lokapujyaComment #23
lokapujyaFurther testing shows that the tokens are only shown when on the link field, not the fields that follow. In some situations, using the token still works, but not always. So, needs work.
Comment #24
Summit CreditAttribution: Summit commentedHi,
Using this patch #21 I got the following replacement tokens:
And using those tokens like [field_affiliate1-title] == I got as link result site/raw url ... instead of the correct title.
greetings, Martijn
Comment #25
Summit CreditAttribution: Summit commentedHi,
For me this worked as ..as a workaround within views Rewrite results and than:
Greetings,
Martijn
Comment #26
joe_carvajalSubscribe.
Comment #27
Marco Vervoort CreditAttribution: Marco Vervoort commentedIt looks like patch #21 accidentally copied the contents of views_handler_field_field::document_self_tokens() to LinkfieldHandlerFieldLinkfield::add_self_tokens() and vice versa.
Also, for me the target-attribute was not obtainable from $item['raw']['link']['target'] but from $item['raw']['attributes']['target'].
I revised the code, and with the new code I could use both the new 'xxxx-target' token and the old 'xxx-url' and 'xxx-title' tokens. I'm uploading it as a new patch.
Comment #28
lokapujyaCan you remake the patch relative to the link module (instead of drupal root) directory? I'll test it.
Comment #29
Marco Vervoort CreditAttribution: Marco Vervoort commentedMy apologies, I hadn't noticed. I've generated the new patch using 'git diff --relative'.
To make the code cleaner I've rewritten the class methods to invoke the parent-class methods for the old tokens (instead of copying the code from the parent class).
Additionally, I've rewritten the code which fetches the actual target-value to be more like the parent-class code (specifically, handling the case where $item['raw'] or $item['raw']['attributes'] is an object instead of an array, and ensuring that using the user-supplied target value does not create an XSS-vulnerability).
Comment #30
lokapujyaI didn't explain right. What I meant was make a patch from the Link git repository. The patch provided won't apply with git patch; It will only apply to your repository. I was able to get it to apply using gnu patch. But for future patches, it would be better if they are made from the project repo: Making a Drupal patch with Git
Comment #31
lokapujyaWhen I am in another field and look at replacement tokens, I don't see 'xxxx-target'. However, I am able to use the 'xxxx-target' token in another field. I wonder if there is anyway to get the replacement to show up for other fields.
Comment #32
lokapujyaRerolled from the Link repo, plus some coding standards changes.
Comment #33
lokapujyaOops, somehow the interdiff showed up in the patch. Fixed.
Comment #34
bmango CreditAttribution: bmango commentedCan confirm that the patch in #33 applied cleanly and works properly. The only drawback is that the target attribute is only available as a replacement token on the link field itself.
Many thanks for the patch :)
Comment #35
lokapujyaComment #38
plusproduit CreditAttribution: plusproduit commented#33 Worked for me
Thanks!
Comment #39
knalstaaf CreditAttribution: knalstaaf commentedHmm #33 breaks the module in my case. Views can no longer deal with a Link field (Title, as link) after applying the patch (does it require some of the previous patches?).
Guess I'll give the approach of #10 a go by providing an extra list (text) field to the content type with these two options:
… and use their machine names in a rewritten link field in Views.
Comment #40
idflood CreditAttribution: idflood at Stimul.ch commentedI tested the patch in #33 and it works fine for me. @knalstaaf did you ran update.php after you patched the module?
Comment #41
TravisJohnston CreditAttribution: TravisJohnston commentedI can confirm that this breaks if you use the RC version of Link, you need to use the Dev version.
Also this doesn't provide tokens within the Replacement Patterns in other fields. For instance, I was hoping to be able to have my Image field, which is clickable based on the value in the Link field, and be able to set the Target via the [field_link_1-target] token. But it is not available.
I could however, which I did, rewrite the results of the link field and put my image in there, in this case the token available works.
Comment #42
shevgenyThe handler is corrupted. My patch does not work. #33
Comment #43
scott_earnest CreditAttribution: scott_earnest commentedI ended up creating a custom formatter for a link field that just extracts the target, as such:
Then, I added a new field in my view, exclude from display, using the new fomatter. So the formatter would return something like the word "_blank" for a new window or nothing a all if there was not a target specified on the link.
That new field could then be used as a token in the rewrite field.