Khanh Hoang - Kenn
Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.
I had a task. It seemed simple enough. It involved a link field with a title and a url (created by the Link module) and a single on/off checkbox field. The checkbox was a toggle for whether the link should open in a new browser tab/window.
The task was to create a Views page that displays an edit icon which links to the url and conditionally opens in a new tab/window based on whether the “Open Link in New Tab” field is checked...without using the PHP filter module.
I went down several paths - Views hooks, Views preprocess functions, template files, template.php, creating a new Link formatter, etc. There was a nice brick wall waiting at the end of each. Then I was introduced by co-worker Derek Deraps to an interesting twist in Views. It was all possible using the Views GUI.
The premise is to output a link that opens in a new tab using the Rewritten Results of the “Open Link in New Tab” field and a link that opens in the same tab through the “No Results Behavior” of the “Open Link in New Tab” field. Genius.
Let’s set up an example view to demonstrate:
Save the view and view the page. Voila! The links with the “Open in new tab” box checked will open in a new tab/window.