0
votes

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 accomplished?

Thanks.

3
While this was asked in '09, we do have a sister site now that is specifically for drupal questions: Drupal Answersuser1228

3 Answers

3
votes

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.

0
votes

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));
}
?>
0
votes

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