drupal views rewrite output to invalid markup

26 Apr 2010

While I was testing the fmag.gr with the w3c validators, I saw several errors about

<h2> and <h5>

elements that where inside <a hrefs. I immediately spotted the problem on the node titles.

More specific: page views at fmag.gr are using fields. The field "node title" has a rewritten output to

<h5>[title]</h5>

and the "link to full node" checked.

Further investigation revealed that views module "links to full node" the whole "rewritten output" and not just the variable [title]. Therefore, the html output was

<h5><a href="...">[title]</a></h5>

 

Solution:

1. Use the semantic views module http://drupal.org/project/semanticviews - http://adaptivethemes.com/semantic-views-module-for-drupal-6-video-tutorial

2. Do not use another module just for this

  • Edit your view and go to fields, edit Node:Title
  • Copy the text as is and uncheck the Rewrite output
  • Check the Exclude from display and update
  • Add new field > select Global > Global: Custom text
  • Paste text and update
  • Rearrange fields
  • Go to Basic Settings and click theme: information
  • Select Row style output > Copy the whole text and press ok > Save your view
  • Create an new file "views-view-fields.tpl.php" into your theme root directory and paste the code
  • find
    <<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_type; ?>>

    an replace with

    <?php print $field->content; ?>
  • htmlTidy again and save (plus upload if you don't edit remotely)
  • Go to your Drupal Administrative Panel and flush all caches.
  • Revalidate

 

What changed

For every row of field-content "views" were adding a . An <h> element inside <span> is invalid. Now, spans are eliminated and everything validates correctly.