views:

310

answers:

4

On my Drupal site, I have made a Users page using the Views module, which is simply a nicely styled grid (HTML table) of users. I'm displaying a few fields for each one, and both the name and the profile picture have been set to link to the user node.

What is the best way to change it so that the whole cell (HTML td) links to the user node? EDIT: I'm not concerned with adding the HTML link tags, but with accessing each profile page's URL.

I've looked into modifying the theme of the view (over-riding the Style output e.g. views-view-grid--users.tpl.php), but cant see an elegant way to get the URL of the user node.

EDIT: I've implemented a temporary solution in javascript which looks into the HTML of each cell, extracts the first link's URL, and uses that, but is there not a better way of doing this using the Drupal variables somehow?

Thanks for your help.

+1  A: 

How about something like this...no JavaScript needed

In your table:

<td><a href="..." class="td_link">the link</a></td>
...

In your CSS file:

.td_link {
   display: block; 
   width: 100%;
}

So basically all you need to do is add a class to your link, and a small snippet of CSS.

espais
Thanks for the suggestion, but this produces a horrible effect - it just turns all of the content into a link (all of the text/images), but none of the gaps between content, or to the edge of the div. I guess the only solution to having the entire div clickable is with javascript, which is fine.The problem is I can't see how to get the actual URL of the user node in an elegant way. The information is right there in the contents of the cells - the name and picture field link to the user node, but I cant seem to access that information with custom theme template files...
jackocnr
How embarrassing; I've just realised your block-link method does actually do what I want. Not sure what I was doing wrong before - maybe some strange CSS priority issues. This is indeed a better solution to using JS to make whole divs into links... Thanks.
jackocnr
if you like it then accept it ;) i'm glad you were able to get it to work though
espais
I've marked it up for being helpful, but I can't accept it as an answer because it doesn't solve my main problem: accessing each user's profile URL from a custom views template file.
jackocnr
ahh, sorry, thought it had fixed your issue
espais
A: 

It's important to distinguish between actual links, with <a> tags, and arbitrary elements you can click. Even if you don't care about semantics, you should care about your visitors not running JavaScript, especially search engines.

Rather than turning a block element into a link, you should turn a link into a block element, as espais suggested. One way to get more control over the markup is using custom fields to add opening and closing tags for your link around the rest of your fields.

Scott Reynen
Unfortunately espais' suggestion does not produce the desired effect (see my comment). I'm actually fine with using javascript - I'm happy to require my visitors to allow it, and it's not a problem with search engines because as I said, the cells also contain regular links to the same end. I have updated my original question to be more specific about my problem.
jackocnr
A: 

spais and scott-reynen are right. but instead of placing every field under multiple <a> elements, each styled with css to turn them into blocks (which can have margin and padding), why not use a single <a> element?

if everything is meant to link to the same place, you can place it all together under a single <a> element, although every element should be an inline element (<span> instead of <div>). you can do it by changing the row template: check http://views-help.doc.logrus.com/help/views/using-theme

in your case, copy templates from inside the views module to your theme folder, and rename it accordingly as your view "Theme: Information" says. make sure there is no <div> or <p> or any other block element being output. if you need to break lines, use <br>.

barraponto
note you can style <span> elements with display:block; and thus apply margins and paddings to it.
barraponto
Thanks for your advice. I've since realised that espais was right. My only problem now is accessing the user profile URL from that custom template file. I just don't know what variables are available for my PHP code in those template files...
jackocnr
+1  A: 

OK I found a better (super simple) way of extracting the profile URL, and also I over-came a few issues with the whole block-link solution (attributed to espais), which I thought were worth documenting. So here is the complete solution to my original problem:

1) Add a custom template file to override views-view-fields.tpl.php (see http://views-help.doc.logrus.com/help/views/using-theme - thanks to barraponto for the useful link). In this custom file, you should wrap all the code in a link, and add a clear-fix div just before the end to stretch the link to the full height of the container.

<a class="td-link" href="user/<?php print $row->uid; ?>">
...
<div class="clear-fix"></div>
</a>

2) Now you need to get rid of any other links from inside each grid element, as you are not allowed to nest HTML links (produces really weird behaviour). First thing to do is edit the View, and make sure none of the fields have "link this field to it's user" checked. Then if you want to include the profile picture field, you need to add a small fix module because by default there's no way to stop this field being a link! You can get the module from this comment: http://drupal.org/node/720772#comment-2757536

3) Finally the CSS. Add the following to your theme's style.css:

a.td-link {
    display: block;
    color: #000;
    text-decoration: none;
    border: 1px solid #E9EFF3;
}
a.td-link:HOVER {border-color: #85b3d4;}
a.td-link label {cursor: pointer;}
div.clear-fix {clear: both;}

This removes the link formatting from the text (as we want the whole block to look like a link, not just the text), and stretches the link out to fill the container. It also makes the cursor graphic consistent, and adds a nice border effect when you mouse-over the block. Remember you can also add a custom CSS class to your View, which makes it much easier/neater to select elements for styling in your CSS code.

jackocnr
lol, this may actually solve a menu issue i'm having...points for that ;)
espais