This module creates a Simple TOC (Table of Contents) for node text areas.

Simple TOC for Drupal 7.x, sandbox at: http://drupal.org/sandbox/neRok/1912730
git clone --recursive --branch 7.x-1.x http://git.drupal.org/sandbox/neRok/1912730.git simple_toc
* Module has been run through the Coder module and ventral.org/pareview, all errors fixed.

Setup is per content type and is enabled by selecting a long text field to apply the TOC modifications to. Upon node views, the module will parse the selected field using the PHP5 DOMDocument feature and modify any HTML headers with organised ID numbers. A TOC 'field' is also added to the nodes content array, enabling the TOC to be displayed anywhere (ie used by other modules).

Other options are limited by design (KISS principle), but include;
- Adding a TOC before and/or after the selected field.
- Adding "Back to Top" links before and/or after the content following a HTML heading.
The simplicity is the main difference to the existing Table of Contents module, plus the DOMDocument parsing.

In addition to inserting the TOC into the nodes content, a Block, Display Suite field and Panel Pane are provided, which make use of the afforementioned TOC 'field' that is created in the hook_node_view. This gives site admins a lot of options for when, where and how to display the TOC. This is why the Simple TOC module has few settings, as there a lot of other modules that can provide required features.

CommentFileSizeAuthor
#14 coder-results.txt3.73 KBklausi
#9 screen001.png21.05 KBaw030
#6 Simple-TOC.png72.6 KBneRok
#5 Simple-TOC.png72.6 KBneRok

Comments

drupaldrop’s picture

Hello neRok,

Found no coding errors, Great!!

Manual review

  • Fresh installation of drupal 7.19
  • Install Module
  • Created a long text field
  • Created two content nodes
  • Enable the Simple toc block

Expected Result - Block will be visible showing the toc links
Actual result - No block is displaying

Also, please elaborate more why this module is different than existing table of contents other than simplicity.

neRok’s picture

Did you put some content with headings in the selected node fields? If it doesnt find headings in the processed field (< h3 > etc), it does not produce any output, block or otherwise.
You also need to enable TOC for a field, and this is set per node type. You do this at admin/structure/types/manage/*your_type* under the Display Settings tab. The block etc only show a TOC if it has been enabled for the currently viewed nodes content type.

  1. Existing TOC module is an input filter and uses regex to find and replace headings in the field. This module uses DOMDocument to do those modifications, which a quick google search reveals is a more reliable method of such manipulations.
  2. The input filter of existing TOC module requires tags such as [[toc]] being written into the field, making it more of an end-user feature. Simple TOC is configured by the site admin (ie turned on and block enabled), which produces consistent results (I wanted to say is simpler ;-) ...)
  3. Simple TOC takes the fields markup after input filters are applied, so it does not matter if the field is written in HTML, Wiki style , BBcode etc. If the processed field has headings, Simple TOC will make a TOC from that field.
  4. The existing TOC module also does not have a stable 7.x release.
aw030’s picture

Review:

*Automated review run through without errors or warnings.
*Manual review:

- Install a fresh drupal 7
- Move the module folder to sites/all/modules
- Install module
- Add a new "test"-field to the "Article" content type
- Assign TOC options in display settings of the content type
- Fill in some text in 2 Test-Articles (with headings/ in the new configured "test"-field-type)
- Placing the block in a region (under structure/blocks) / region: Sidebar first

Result: no block appearing in the first sidebar

-> Try to overwrite the Title via block-configuration:
Result: nothing to see of the TOC-block

-> Try assigning the TOC-options to the Body-field of the "Article"-content-type:
Result: I see the TOC in the first sidebar as expected!!!

Issue found: After installing the module the admin/structure/types/add form throws a fatal error:
Fatal error: Unsupported operand types in ...\includes\form.inc on line 2623
Can someone reproduce that?

klausi’s picture

We are currently quite busy with all the project applications and I can only review projects with a review bonus. Please help me reviewing and I'll take a look at your project right away :-)

neRok’s picture

StatusFileSize
new72.6 KB

@#3, yes I have that error too. Good catch! I will investigate.

Did you have other problems, or it worked as expected? It is a bit hard to work out if you were saying it did nothing as expected, or it did nothing when you thought it would.

Image attached to clarify the options and output.

neRok’s picture

StatusFileSize
new72.6 KB

This was a double post...

Problem from #3 solved, except I cannot push to git here at work. The problem is line 129 of simple_toc.module needs changing from $options = ''; to $options = array();

aw030’s picture

No there were no other problems in my test. all ok.
When all was configured well and the headings are
added to the text of the assigned field, the TOC-block
became visible with a correct table of content of this text.

Nice module!

neRok’s picture

Cool, and thanks.

I have updated the code on Git now.

aw030’s picture

Status: Needs review » Needs work
StatusFileSize
new21.05 KB

Automated review:
PAreview.sh on ventral.org: OK, no errors/warnings
Coder module: OK, no errors/warnings

Manual review #2:

Issues:
Tested on Drupal 7.19
1. On the second run i made one article with "more complex" structure of headings with h1 and h2 tags for chapters and subtitles of that chapter.
There seems to be a problem with detecting the headings: a empty list-element is rendered, see the attached screenshot (screen001.png with the html and the result of the block view, but its also happening in node view).
I filled in these html structures intuitively, and i think it's a common structure for chapters (i tried it with/without p-tags around the blindtext paragraphs / and other combinations), that should be parsed well? Or it should be provided as description somewhere which html-structure is expected to get a correct result?
2. On line 47 in simple_toc.module there is a variable $curr that not have been initialized, but it's only a visual problem (php-function sscanf() will handle it well).

Successfully re-tested:
1. Added a new field (Long text/Full HTML) to the content-type "Article", and assigned this field by the Simple TOC display settings: TOC appearing in block as expected.
2. Issue from #3 in admin/structure/types/add fixed as mentioned by neRok in comment #6 and successfully tested.

Other:
1. With DOMDocument you are using saveHTML() at the end and then removing the doctype, opening/closing head and body.
I not tested it, but if you call saveXML() these html-wrappers should not be added and you don't need to remove them, but perhaps saveXML() will add some document-type-declaration too, then it's not better.

I will change it to "needs work" (because of issue 1.)

neRok’s picture

You have unearthed something here! The problem is NOT caused by Simple-TOC, but is due to your use of <h*/> instead of </h*>.

I ran a few tests...
If this is the body <h1>HEADER<h1/>...
With 'correct faulty and chopped off html' enabled in the input filter, the output is <h1>HEADER</h1><h1></h1>...
With 'correct faulty and chopped off html' disabled in the input filter, the output is <h1>HEADER</h1><h1>... THE WHOLE NEXT PARAGRAPH BECOMES H1</h1>

So Simple-TOC is handling what is passed to it correctly, it is just being passed garbage.

I searched google quickly and could not find anything relating to the use of <h*/>. I can raise this error in the core issue queue, but I dont think it is a problem for this module to correct. In your screen shot, even though the output appears broken to the end user, all the links probably still work, and the rendered body should be visually un-altered.

Please comment and bump up the status if you agree.

aw030’s picture

Status: Needs work » Reviewed & tested by the community

Arrgghh! You are right, that's why every test before was successfull, but this one failed: h1/ was a typing error, and i only copied it for the 3 other chapters and don't saw this mistake. Sorry for that. I now made some more other sturcture tests even more complex (down to h6 which is provided in code) and all were rendered correctly.

I will set up the status to "review by the community", because i really can't find issues here anymore.

Nice module, i will surely use it someday...

jthorson’s picture

Status: Reviewed & tested by the community » Needs work

neRok:

One comment ... You define a ctools plugin, but do not list ctools as a dependency. Is the plugin code optional?

Feel free to mark this directly back to RTBC after responding.

neRok’s picture

Status: Needs work » Reviewed & tested by the community

I'm not sure what the norm is, but the way I see it is the ctools plugin is a hook and will only appear when ctools is enabled, otherwise the hook is never called (so it is optional). The display suite field and even the block work in the same manor. I disabled ctools and there were no errors, so I believe it is fine.

klausi’s picture

Status: Reviewed & tested by the community » Fixed
StatusFileSize
new3.73 KB

Sorry for the delay, but you have not listed any reviews of other project applications in your issue summary as strongly recommended in the application documentation.

Review of the 7.x-1.x branch:

  • Coder Sniffer has found some issues with your code (please check the Drupal coding standards). See attachment.
  • DrupalPractice has found some issues with your code, but could be false positives.
    
    FILE: /home/klausi/pareview_temp/simple_toc.module
    --------------------------------------------------------------------------------
    FOUND 0 ERROR(S) AND 2 WARNING(S) AFFECTING 2 LINE(S)
    --------------------------------------------------------------------------------
     428 | WARNING | Class name must be prefixed with the project name "simple_toc"
         |         | (omitting underscores)
     436 | WARNING | Class name must be prefixed with the project name "simple_toc"
         |         | (omitting underscores)
    --------------------------------------------------------------------------------
    
    Time: 0 seconds, Memory: 7.50Mb
    

This automated report was generated with PAReview.sh, your friendly project application review script. You can also use the online version to check your project. You have to get a review bonus to get a review from me.

manual review:

  1. Please list the similar tableofcontents module on your project page and explain the differences there.
  2. simple_toc_node_load(): I would not use that hook to process your stuff because nodes can be loaded for various purposes, and if they are not displayed then your calculation just adds overhead and a performance penalty. Looks like you rather want hook_node_view()?
  3. simple_toc_content_type_edit_form(): empty function, why do you need it? Please add a comment. Same for simple_toc_content_type_edit_form_submit().

Although you should definitively fix those issues I don't think they are critical application blockers, so ...

Thanks for your contribution, neRok!

I updated your account to let you promote this to a full project and also create new projects as either a sandbox or a "full" project.

Here are some recommended readings to help with excellent maintainership:

You can find lots more contributors chatting on IRC in #drupal-contribute. So, come hang out and get involved!

Thanks, also, for your patience with the review process. Anyone is welcome to participate in the review process. Please consider reviewing other projects that are pending review. I encourage you to learn more about that process and join the group of reviewers.

Thanks to the dedicated reviewer(s) as well.

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

Added note that module has been through Coder checks.