0

I've got a content type of "Course" with entity references to users for Professor, TA, and Designer. Each user can be any of the three roles for any course.

I've got a View that shows, for the logged-in user, what courses they are the professor, OR TA, OR designer for.

What I'd like to add to that View is a column that tells the user which role they play in the course - or, in Drupal terms - which entity reference field their account is referenced in. I'd like to do this with exactly one column in the view.

The view already has Relationships from the Course to each of the referenced entities - but I am not sure how to get the conditional into a single column.

Is there a way to accomplish this with the Drupal 7 Views features, or do I need to re-write a column...?

0

So you can print the user reference fields, but that will show all users and their roles, and you only want to show the one?

This might seem a little hacky, but it should work.

  1. First of all, add all three user reference fields, and add them to the same table cell using the views table settings.

  2. Next, you need to hide the user reference fields that do not contain the current logged in users name. To do this, we will be overriding the fields with a tpl.php template. In your view, click on "advanced" and at the bottom, click on "theme: information". In the relevant row, you will find something like views-view-field--course--professor.tpl.php. Note that this is overriding the field, and has the view name type in it (which will not override any other views with the same field).

  3. In your FTP, go to the views module folder. Click on the "theme" folder, and find the file views-view-field.tpl.php. Copy this file to your themes template folder, and rename it views-view-field--course--professor.tpl.php, or whatever your field name was in step 2. Then flush your cache. This should now be overriding that field in the view. (To make sure, you could edit that file, and add some text below the <?php print $output; ?> to see if it worked.)

  4. Now that we are overriding the field, we can influence what will be shown in that field. I would replace <?php print $output; ?> with the following....

php example

<?php 
global $user; 
$username = $user->name;

if (strpos($output, $username) !== false) {     
  print $output; 
} 
?>

I haven't tested the above, but something similar to that should print the fields output, only if the current logged in users username matches the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.