Made to Order Software Corporation Logo

Adding a Table of Contents to a view with parameters

It is possible to add a Table of Contents to a view that has to accept parameters1.

In this case, you do not want to use the solution of including the view in a node (with the Insert view filter, see Can't find doc_table_of_contents_for_views to include!) because then you lose the capability of assigning different parameters to your view (although, if the number of parameters is relatively limited, you could create one page per set of parameters supported.)

So, here, we offer a solution for you to include a Table of Contents using your view template. Basically, it reformats the view output by passing it through the Table of Content filter. The filter requires a format number and the text to be filtered.

<?php
  module_load_include
('pages.inc', 'tableofcontents');
  echo
_tableofcontents_replace_toc(2, '[toc]'.$rows) ;
?>

These two lines of code will prepend a Table of Content to the $rows text. The $rows text comes from the Views.

To append a Table of Contents, simply move the '[toc]' string after the $rows reference as in: $rows.'[toc]'

The module_load_include() call is necessary in case the tableofcontents.pages.inc file was not loaded.

The number 2 in the call to the _tableofcontest_replace_toc() function is the format identifier. The one main reason why you'd want to enter a specific format is to gain the flexibility of changing the Table of Content setup from the Drupal interface (from your Administer » Site configuration » Input format settings form.) Use the number 0 if you just want to use the default settings and what you specify in the view template, since it is possible to include parameters along the [toc] tag such as the title.

<?php
  module_load_include
('pages.inc', 'tableofcontents');
  echo
_tableofcontents_replace_toc(0, '[toc title:My View; minlevel: 2;]'.$rows) ;
?>

In this example I show how the table can be given the name My View and use <H2> as the minimum level.

  • 1. Note that you need to write some PHP code. If you do not know how to do that, I'm afraid that this won't work for you. You may want to check out the Table of Contents and Insert view solution instead.

Comments

Duplicated Rows?

I've tried to use this example in my views-view--[myview].tpl.php I find that if I include the ".rows" in the line: echo _tableofcontents_replace_toc(0, '[toc title:My View; minlevel: 2;]'.$rows) ; then this module duplicates all of my rows. So I get the TOC and then all the rows. Then instead of the page ending, I get the rows again, a full duplicate set. I tried taking out '.$rows which returned me to one set of rows, but also removed the TOC. Any help would be appreciated.

Check out original issue...

Hi there,

You may want to check out the original post on Drupal.

Let me know if you find the way to resolve this problem on that issue. I have to say that I did not try to do exactly what these guys were doing. One day, I may add support in the views directly. It should be relatively easy to add a setup to get the TOC for views to work without any PHP tricks like these.

Thank you.
Alexis

Oh!

Thank for responding so quickly.   I took another look at it and now see that I was just being dumb.

I didn't realize that the TOC was actually echoing the rows as well as the TOC, so of course you need to comment out the "print $rows; " in the view.

Sorry to trouble you and thanks again for your help and this terrific module.