开发者

Conditional link to node within views?

I have two content types, book and chapter. Each chapter node contains a node reference of the book to which it belongs. I have created a view which displays the title of each chapter for a given book. Within the view, the title field has been configured to link to its node.

All works well. I am now interested in updating the view to not display a link to a chapter's node when the chapter's body is empty. Thus this view would display a list of chapter titles for a book and link only to those chapters that have a body.

Can this be accomplished using out of the box Views functionality? If not, how would something like this be accomp开发者_如何学Golished?

Thanks.


I'd use the Views Custom Field module to implement your custom logic. It allows you to grab the data fetched by Views and manipulate it at will with PHP. Very handy.


I'm answering my own question because my response to ceejayoz is poorly formatted.

What I did to accomplish this was to first download and enable views_customfield. Second, I placed Title and Body fields within the view, both excluded from display. Third, within a Customfield: PHP code field I placed the following code:

<?php
if (strlen(trim($data->node_revisions_body)) == 0) {
  return $data->node_title;
} else {
return l($data->node_title, drupal_get_path_alias('node/' . $data->nid));
}
?>


There's also this clever workaround which allows you to achieve this very easily:

  1. Add 2 title fields one with link and one with no link, and make both them exclude from display.
  2. Add body field,
  3. In No result behavior put title with no link replacement token to it.
  4. In Rewrite results behavior put title with link replacement token to it.
  5. Tick hide if empty.

Source

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜