I'm attempting to create anchor tags for a views 3 rendered list of content where each change in the first character of the title gets an <a name="a"></a> or "b" or "c" added to the html output.

The only way I've found to be outside the foreach loop where I can monitor the change in the title is in a preprocess function like this:

function mymodule_preprocess_views_view(&$vars) {
  $alphc = 'a';
  $target = "<a name='$alphc'></a>";

  foreach( $vars['view']->result as $key => $values) {
    $title = $values->node_title;
    $tmp = substr($title,0,1);

    if (strtolower($tmp) != $alphc) {
      $alphc = strtolower($tmp);
      $target = "<a name='$alphc'></a>";

The problem of course is that Views has already rendered the html output in $rows. I'm sure I'm not the first person trying to do this since having a series of links A B C D E F G H etc. to long list of content is pretty standard html practice. I'm not sure what the value of a preprocess function is if the html content has already been rendered, a preprocess function seems an ideal place to override the output.

The closest I got to a solution was with the above code and then adding a new $variable to the views->result array but I have no idea how to capture that $variable in a Views theme (display, style, row or field) so that I can append the anchor tag for the title.

Any help will be appreciated. Thanks Eric

  • After posting I found this link and answer on StackExchange: link I still would like to know if you can interrogate the fields in a view through a loop over all the results in a function call rather than having this buried in the Views rendered $rows (html text) output where the html is next to impossible to alter.
    – Eric
    Apr 22, 2012 at 9:20
  • yes, you can use the pre_render hook: api.drupal.org/api/views/views.api.php/function/…
    – Jimajamma
    Apr 23, 2012 at 5:11
  • You can find all the hooks for Views here.
    – Countzero
    Apr 23, 2012 at 6:35


Your Answer

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

Browse other questions tagged or ask your own question.