0

I'm using Views 3 with Drupal 7. I'm trying to dynamically add a class to the "views-row" line of a view based on the node type. I have a view with a relationship which is duplicating items when a relationship exists. I thought by adding a class for the node type into the views-row line I could hide the field in the row. I'm not sure whether I should be overriding views-view-unformatted.tpl.php or views-view-fields.tpl.php, to add the class and how I can extract the node type from the data to add it into the the classes for that line.

I'd be grateful for any guidance.

Ben

2
  • have you tried the distinct option? Commented Jan 24, 2013 at 12:27
  • Hi Mohammed, yes, I've already tried the distinct option. Thanks for your suggestion.
    – Ben
    Commented Jan 24, 2013 at 13:16

1 Answer 1

1

Views itself will tell you which file to use as a template. Go to the config page for the relevant view and choose Theme: Information under Other. This will print out a list of potential templates; the template shown in bold is the one that is in use.

To get the node type, you can use the dpm() function of the devel module. For example, if you have devel enabled, you can simply insert dpm($row) in the template file, load a page containing the view in your browser, and then dig through the output.

4
  • Thanks for your reply Patrick. I've tried for a couple of hours now without success, but we'll keep trying.
    – Ben
    Commented Jan 28, 2013 at 7:33
  • You need to clear the cache before the template file will be recognized. Commented Jan 28, 2013 at 8:28
  • I have cleared the cache and I have my custom template files working okay. The difficulty I'm having is using the correct variable in the template. I'm pretty sure the template I need to change is the views-view-unformatted.tpl.php, but I just can't seem to find the variable I need to add to it. I have been using the dpm function.
    – Ben
    Commented Jan 28, 2013 at 12:27
  • Well, if the template is being read correctly, it will appear in bold under Theme: Information in the Views UI. Check that first to make sure you're editing the right template. (Assuming you're not getting dpm() output-- if you are, there's no reason to do this.) Commented Jan 28, 2013 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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