Hi,

I need to call toc inside a tpl, using php.
How can i do this?
Which function should i use?
How?

So, i cant use:

i need something in php like:
$node = headinganchors_filter('process', null, null, $node);
$node = tableofcontents_filter('prepare', null, null, $node);
$node = tableofcontents_filter('process', null, null, $node);

but i'm not sure...
how can i achive this goal?

thanks,
Carlos
PS: the tpl is for a view, because i couldn't use it the html in header (it doesn't process)

Comments

AlexisWilke’s picture

Hi Carlos,

If you still have stuff about Heading Anchors, you want to read the switch to the newer version (3.1+ and 3.x-dev now) on the front page.

Otherwise, I would think that calling the filter directly would work. The 'prepare' is necessary only in order to support the <-- tableofcontent --> syntax.

Note also that the $format needs to be defined properly (3rd parameter.) It is used to determine the current setup. You are free to create a format just and only for that purpose then use its identifier (that you see in your URL/location bar then you edit the format.)

Thank you.
Alexis Wilke

carvalhar’s picture

Hi Alexis,

I'm using a view tpl...so i have a div where all rows are printed.
I'd like to place the TOC above it..so in my tpl i tried this code:

<div class="view-content">
	<!--tableofcontents list:ol; title:Table of Contents; minlevel: 2; maxlevel: 4; attachments: 0;-->
      <?php print $rows; ?>
 </div>

But the filter doesn't work and toc doesn't show.

Do you know how could i call toc?
I think i should use php for it. There is a function (function theme_tableofcontents_toc($toc) ) but how can i pass this variable $toc?

I'm not sure how to make a php to call the module TOC and its output.

thanks,
Carlos

luco’s picture

what carlos needs - and so do I - is a way to have a Views-compatible TOC.

the first thing that crossed our minds was to add a TOC filter to the view's header, in order for it to call the titles in the view's body. that didn't work, because - probably - the view's header has its own scope. therefore what happens in the header, stays in the header ;]

so we need to know what is the function that converts

<!--tableofcontents-->

into actual links. we'd embed that function staight into the Views TPL and that would hpefully work.

care to help us?

AlexisWilke’s picture

Carlos & Luco,

Would what I describe here work for you guys?

  http://new.m2osw.com/doc_toc_for_views

Thank you.
Alexis

luco’s picture

nope. that's the opposite of what we're trying to accomplish. we're trying to see nodes inside a view, grouped by some criteria (in our case, taxonomy) and whose automatically generated H2s are called by a TOC.

if we insert a view in a node, that means having to repeat it every time a node was published. plus, as your solution stated, it doesn't support dinamically created views. and we need to be able to supply arguments.

carvalhar’s picture

I'm trying to call TOC using php inside the tpl of my custom view.

But i don't know the configuration of TOC object and also, i couldn't find an easy function as "print_toc($text)"...

I found the function _tableofcontents_replace_toc($format, $text) inside tableofcontents.pages.inc and i tryed to adapt it, but i couldn't succeed.

Do you have any tips how to "print" TOC using php?

Thanks,
Carlos

 <div class="view-content">
	<?php
//$theTOC = _tableofcontents_replace_toc(-1, $rows) ;
//$theTOC = fgv_tableofcontents_toc($theTOC); 
//print $theTOC;
$format = 1;
$toc = new TOC;
$text = _tableofcontents_headers($toc, $format, $rows);
$toc->hidden         = FALSE;
$toc->title          = variable_get('tableofcontents_title_'          . $format, 'Table of contents');
$toc->list           = variable_get('tableofcontents_list_type_'      . $format, 'ol');
$toc->minlevel       = variable_get('tableofcontents_minlevel_'       . $format, 2);
$toc->maxlevel       = variable_get('tableofcontents_maxlevel_'       . $format, 3);
$toc->attachments    = variable_get('tableofcontents_attachments_'    . $format, FALSE);
$toc->hideshow       = variable_get('tableofcontents_hide_show_'      . $format, TRUE);
$toc->collapsed      = variable_get('tableofcontents_collapsed_'      . $format, FALSE);
$toc->allow_override = variable_get('tableofcontents_allow_override_' . $format, TRUE);
$toc->has_toc        = FALSE;

if ($toc->minlevel < $toc->header_min) { // avoid empty indentations
    $toc->minlevel = $toc->header_min;
    if ($toc->minlevel > $toc->maxlevel) {
      $toc->maxlevel = $toc->minlevel;
    }
  }
  if ($toc->maxlevel > $toc->header_max) { // this is much less important
    if ($toc->minlevel > $toc->header_max) {
      $toc->maxlevel = $toc->minlevel;
    }
    else {
      $toc->maxlevel = $toc->header_max;
    }
  }

    // Add a back to top at the very end too
    if (!$toc->first_header && $toc->back_to_top && $toc->back_to_top_location == 'bottom') {
      $result .= '<div class="toc-back-to-top"><a href="#' . $toc->back_to_top_anchor . '">' . $toc->back_to_top . '</a></div>';
    }

    // If we have back to top and scroll on, advice script
    if ($toc->back_to_top && $toc->scroll_back_to_top) {
      $result .= '<script type="text/javascript">toc_scroll_back_to_top = 1;</script>';
    }

print $result;
?>

      <?php print $rows; ?>
    </div>

AlexisWilke’s picture

Luco,

The views are updated in nodes when you use [view:<name>] (unless you have some cache going on)

So adding new entries that appear in the view will update the node automatically.

The problem is arguments if you need dynamic arguments to the view. You can add static arguments though. So if you just have 4 or 5 specific arguments, it would work just fine to create 4 or 5 nodes with a [view] tag.

Luco & Carlos,

Try something more like this:

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

If you want a <div> around the [toc], then add that inside the string '<div ...>[toc]</div>'. Similarly, you can wrap the rows and the entire page.

The 2 in the call is the format number. Please, go to your admin/settings/filter and check the format number that includes support for [toc]. If you don't yet have such a format, create one or edit one and add the [toc] filter to it.

Thank you.
Alexis

carvalhar’s picture

Hi Alexis,

thanks a lot for your php code!

It worked fine in my tpl, i could print the TOC and the links are fine too.

Just two lines and everything loads fine.

thanks :)
Carlos

luco’s picture

yeah, we tried without changing the first argument (the number "2"), then we changed to 0, didn't make any difference.

anyway it worked - it's all that matters ;]

thanks a bunch.

AlexisWilke’s picture

Assigned: Unassigned » AlexisWilke
Status: Active » Fixed

Glad it worked 8-)

I added a new page to my Drupal site here: http://new.m2osw.com/drupal_doc_toc_dynamic_views

It explains the same thing as I just mention in this post.

Note that the number "2" does not matter if the default [toc] tag works for you. If you want to be able to change the defaults, then you will need to make use of a filter appropriately.

Thank you.
Alexis Wilke

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.