0

I'm working on some views and I came across a requirement I'm not sure how to handle.

I need the title for each returned item to link to a specific URL. However, if another field (a link field) isn't empty, link to that link instead. I believe I need to edit the view template files but I'm not sure how to get started.

  • In my view, I've found the "Theme: Information" section and am able to get template files working for my view. However, it's listing display, style, row, and field templates. Where would I incorporate the logic I described above for the title field?
  • Once in a template file I am unsure of what variables are available to me. Is there a list or a way to discover these?
1

Views is rendered by some of *.tpl.php files, they are:

  • views-view.tpl.php. Main view template.
  • views-view-unformatted.tpl.php. Default simple view template to display a list of rows.
  • views-view-fields.tpl.php. Default simple view template to all the fields as a row.
  • views.view-field.tpl.php. This template is used to print a single field in a view.

If you want to use two different fields from one view in one file, I recommend to use views-view-fields.tpl.php where you can manipulate of all fields which you assign to view.

The views-view-fields.tpl.php has three variables to use: $view, $fields and $row. I suggest to use only $fields.

Then, if you have field named field_image you can render its using $fields['field_image']->content or $fields['field_image']->raw.

Also, I recommend to read more about it: Views template files

  • Thanks, that's really helpful. I've created a tpl file names something similar to views-view-fields--page-topic.tpl.php and it is affecting the correct content on the site. However, I've looked around in the $fields var and all the content is already rendered as HTML in $fields['field_lede']->content. I don't have individual pieces of content. Do you know what might cause that? – Vecta Sep 15 '15 at 19:44
  • 1
    Fields in view-views-fields.tpl-php still are rendered by Views UI module. Thats why I mentioned about $fields['field_name']->raw. Also in Views UI you have to set that link generated by Link module is plain URL. – Grzegorz Gajda Sep 15 '15 at 19:57

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.