More Drupal learning

2009 September 17
by Karen

So I’ve been working on a custom content type within our Drupal intranet which represents our fund codes and the subject liaison who is responsible for that code as well as other information. To make this work most efficiently I’m using a user reference in CCK. Basically what this does is that it creates a link between the node which is the fund code and the user who responsible for that fund code.

The problem is that when I let this display with the typical node template I get the users Drupal login name rather than their real name. This is no good. So I added the Profile module, input first and last name into each user, then am using Contemplate module to customize the display of the node.

The key function for doing this is user_load. I take the uid which is stored in my nodereference CCK field pass it to the user_load function and them am able to access all the information about that user. Here’s a snippet from my template in Contemplate

<p>Fund Code: <?php print $node->field_fund_code[0]['view'] ?></p>
<p>Fund Name: <?php print check_plain($node->title) ?></p>
<?php if (content_format(‘field_subject_liason’, $field_subject_liason[0]['uid']) != ”) :
$subject_liaison_user = user_load(array(‘uid’ => $node->field_subject_liason[0]['uid']));
?>
<p>Subject Liaison: <a href=”/user/<?php print $node->field_subject_liason[0]['uid'] ?>/contact”><?php print check_plain($subject_liaison_user->profile_first_name)?> <?php print check_plain($subject_liaison_user->profile_last_name)?></a></p>
<?php endif; ?>

All and all the code is pretty simple and very helpful to know. What is really cool is that you don’t have to look up the user by id you can use other fields such as email and you can use multiple fields. This looks like

user_load(array(‘email’ => ‘person@organization.edu’)); // searching for email person@organization.edu

Incidentally this same technique works when have a nodereference CCK field as well except in that case you use the node_load function and pass it the node id. Here is an snippet that shows that in action

<?php node_load($node->field_display_jpg[0]['nid']); ?>
<a href=”<?php print $base_url $photo_node->field_image_file_jpeg[0]['filepath']?>”><img src=”<?php print imagecache_create_url(‘thumbnail’, $photo_node->field_image_file_jpeg[0]['filepath'])?>”/></a>

Using these types of CCK fields you are able to create relationships between nodes and users. Its very handy if you have complex content types. Once you know about node_load and user_load its easy to incorporate content from the related nodes and users into the main node that you are working with.

3 Responses leave one →
  1. 2009 September 17

    I’m not sure exactly how you want to present the data, but Views should be able to access Profile data along with any CCK fields. Using Views, you can make a table with sticky headers and sortable columns in short order, then add AJAX filters so you could quickly retrieve a [subject x] liason responsible for [fund code y] without reloading the page. Views is super slick, so making something like that is faster/easier/funner than mucking with contemplate and functions.

    Probably the most worthwhile additional Views contrib module is Views Custom Field, which lets you insert markup and logic into the Views template and otherwise mess with the output.

    Hope this helps.

  2. 2009 September 18

    I use Views for this a ton. Although sometime it take a bit to figure out if I want something different than the default display from the referenced node. One issue I encountered on this project that drove me nuts was I wanted to use user reference and then tell it to use a particular View for the data. Problem was that whenever I did this and then went to create a subsequent View of all my Fund Codes with filters by the User reference field that was subject liaison, I got HTML junk in my filter drop-down. Any ideas how to fix this?

    Views Custom Field sounds interesting I’ll have to check it out. I’ve themed my Views in the past to deal with some of this but anything that makes it so I don’t have to edit files on the server is a plus.

    In this case, I was working on how an individual node of a particular content type displays and I wanted to pull in information from users or nodes referenced. Like I said sometimes the default display has what you want but sometimes it doesn’t.

  3. 2010 January 23
    Anonymous permalink

    Thank you for the tutorial. I’m wondering if you know how to find the UID from an email address.

    I have a view with an user id argument (unfortunately email is not an option under node) that I’m using to pull the content profile fields for the user (which is assigned by the cck email field). Unfortunately node-reference won’t work for me, since my node title is a name (which may be repeat if there are two users with the same name).

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS