"Table of Contents" Plugin for Cotonti - User Guide
Detailed guide on how to build hierarchical menus (trees) consisting of items of various types: links to categories, individual pages, or custom URLs.
Guide to Table of Contents Page (toc_page) Plugin for Cotonti
Version: 1.1.4
Date: August 24, 2026
Table of Contents
- Introduction
- Installation and Configuration
- Managing Trees
- Managing Items
- Multilingual Support
- Integration with Cotonti
- AJAX Page Search (Select2)
- SQL Dump Generator
- Advanced Functions
- Frequently Asked Questions
- Examples
Introduction
About the Plugin
Table of Contents Page (toc_page) is a plugin for the Cotonti content management system designed to create manageable tables of contents on website pages. It allows building hierarchical menus (trees) consisting of items of different types: links to categories, individual pages, or arbitrary URLs. Items can be manually sorted, assigned parent-child relationships, enabled or disabled, and their titles and URLs translated into different languages.
The plugin is especially useful for:
- creating knowledge bases and reference sections;
- manually managed navigation menus;
- hierarchical tables of contents for long guides;
- multilingual navigation structures.
The main feature is full control over the order and nesting of items, which distinguishes the plugin from automatically generated menus based on category or page structure.
Features
- Unlimited number of trees: you can create multiple independent tables of contents for different sections of the site.
- Three item types:
- Category — a link to a Cotonti category;
- Page — a link to a specific page;
- Custom Link — any external or internal URL.
- Manual sorting: each item is assigned a numeric order within its parent.
- Hierarchy: unlimited nesting of items.
- Multilingual support: translation of titles and URLs into additional languages (when enabled).
- AJAX page search: convenient page selection via Select2 instead of a bulky dropdown list.
- Pagination in the admin panel for working with large trees.
- Editing item type after creation.
- SQL dump generator: allows quickly importing a ready tree structure from an HTML list.
- Easy integration: displaying a tree with a single function in any template.
- Security: access rights support, AJAX requests protected.
Requirements
- Cotonti version 1.0 or higher (the latest is recommended).
- PHP 8.5 or higher.
- MySQL 8.4 or higher (with InnoDB and utf8mb4 support).
- The Page module must be enabled (mandatory).
- For multilingual support, the i18n plugin is recommended (optional).
Installation and Configuration
Installing the Plugin
- Download the
toc_pageplugin archive. - Unpack the archive into the
plugins/directory of your Cotonti site. The following structure should be obtained:
plugins/
└── toc_page/
├── toc_page.setup.php
├── toc_page.admin.php
├── toc_page.functions.php
├── toc_page.ajax.php
├── lang/
│ ├── toc_page.ru.lang.php
│ ├── toc_page.en.lang.php
│ └── toc_page.uk.lang.php
├── tpl/
│ ├── toc_page.admin.tpl
│ └── toc_page.tpl
└── setup/
└── toc_page.install.sql- Log in to the Cotonti administration panel.
- Go to the Extensions section → Plugins.
- Find Table of Contents Page in the list and click Install.
- After successful installation, the plugin will be activated.
During installation, three tables will be created in the database:
cot_toc_page_trees— trees (titles, descriptions, timestamps);cot_toc_page_items— tree items (type, reference, title, URL, sorting, status);cot_toc_page_i18n— translations of item fields (item_title, item_url, etc.).
Note. If you have a different table prefix (for example,
my_), replacecot_in the SQL file with your prefix before installation. Usually this can be done automatically during installation if the system uses a dynamic prefix, but check for reliability.
Initial Setup
After installation, go to Settings → Plugins → Table of Contents Page. The following parameters are available:
| Parameter | Description | Default Value |
|---|---|---|
default_tree | Default tree ID. Used if the cot_toc_page_render() function is called without an explicit ID. | 0 |
toc_page_i18n_use | Enable/disable multilingual support. | Off |
toc_page_i18n_lang_code_default | Code of the main site language (must match $cfg['defaultlang']). | ru |
toc_page_i18n_lang_code_first | Code of the first additional language. | en |
toc_page_i18n_lang_code_first_use | Use the first additional language. | On |
toc_page_i18n_lang_code_second | Code of the second additional language. | ua |
toc_page_i18n_lang_code_second_use | Use the second additional language. | Off |
toc_page_select2_use | Use AJAX page search via Select2. | Off |
It is recommended to set default_tree immediately after creating the first tree so that the output function works without parameters.
Multilingual Configuration
- Enable the Activate and use multilingual support option.
- Set the main language code in the
toc_page_i18n_lang_code_defaultfield. This must be the same code as the global Cotonti setting$cfg['defaultlang']. Item titles in the main tablecot_toc_page_itemsare considered the original for this language. - For each additional language:
- set its code (for example,
en,uk,de); - enable the Use ... flag.
- set its code (for example,
- Save the settings.
Now a Translations tab will appear in the plugin admin panel, where you can enter translations of titles and URLs for each active additional language.
Important: if you already have items, enabling multilingual support will not change their original values; they will remain in the main table.
Select2 Setup
Select2 is an enhanced dropdown list with search. In the plugin, it is used to select a page in the add/edit item forms.
How to enable:
- In the plugin settings, find the Use AJAX page search via Select2 option.
- Set the value to Yes.
- Save.
After that, in the add item form of the Page type, the regular <select> will be replaced by an autocomplete field. When entering at least two characters, an AJAX request is sent to the server, which returns a list of pages matching the query.
Select2 is automatically loaded from Cotonti resources. If you want to configure the minimum input length, you can change it in the code (minimumInputLength: 2) in the toc_page.admin.php files (two places: the tree editing tab and the item editing tab). The default threshold is 2 characters.
Managing Trees
Creating a Tree
- Go to the plugin admin section: Other → Table of Contents Page.
- Open the Trees tab.
- At the bottom of the page, there is an Add Tree form:
- Tree name — mandatory field (e.g., "Knowledge Base").
- Description — optional short description (displayed only in the admin panel).
- Click the Create button.
After saving, the tree will appear in the list of trees with an assigned ID.
Editing a Tree
Each tree in the list has an Edit button (pencil icon). Clicking it opens the edit form directly in the list (the Trees tab). The editable fields are name and description. Click Update to save.
Deleting a Tree
In the list of trees, each tree has a Delete button. When deleting:
- all items of this tree are removed from
cot_toc_page_items; - all translations of these items are removed from
cot_toc_page_i18n(cascade); - the tree record is removed from
cot_toc_page_trees.
Deletion is irreversible — it is recommended to back up the database beforehand.
Insertion Code for a Tree
In the Insertion Code column for each tree, ready-made code is automatically generated for insertion into a TPL template or PHP code.
Code example:
<!-- IF {PHP|cot_plugin_active('toc_page')} -->
{PHP|cot_toc_page_render(1)}
<!-- ENDIF -->Here 1 is the tree ID. You can copy this code with the Copy button and paste it into the desired location.
Managing Items
Adding an Item
- Go to the Tree Editing tab (the Items button next to the desired tree in the tree list).
- At the bottom of the page, there is an Add Item form.
- Select the item type:
- Category — you will need to choose a category from the dropdown list.
- Page — you will need to choose a page (regular list or via Select2).
- Custom Link — you will need to enter a URL.
- Fill in the title (if left empty for Page or Category types, the title may be automatically taken from the linked object).
- If necessary, select a parent from the list (default is "Top level").
- Click Add.
The new item will be added to the end of its parent's list with an automatically calculated order number (item_sort).
Item Types
Category
When this type is selected, you must specify a category from the Cotonti page structure. The category code is stored in the item_ref field. When displayed on the site, the link will lead to the category page, and the title (if not manually set) will be taken from the category name.
If the category is later deleted or renamed, the plugin will show the title stored in item_title, or display "Category not found".
Page
The Page type requires selecting a specific page from the list of published pages (page_state = 0). The numeric page ID is stored in item_ref. When displayed, the page URL is generated, and the title is taken from the page name or from the item_title field.
When using AJAX Select2 search, you can quickly find the required page by name.
Custom Link
This type allows specifying any external or internal URL (e.g., https://example.com or /some/page). Nothing is stored in item_ref, and the entered URL is placed in item_url. The title should be manually set in the Title field.
Editing an Item
To edit an item, click the Edit button (pencil icon) in the item row on the Tree Editing tab. A separate Item tab will open with a full edit form.
Here you can change:
- item type;
- linked object (category, page, or URL);
- title;
- parent item;
- sorting order;
- status (enabled/disabled).
After saving, you will remain on the edit page to see the changes made.
Changing Item Type
Previously, after creating an item, its type could not be changed. In version 1.1.4, this issue has been resolved: in the item edit form (Item tab), there is a Type dropdown. You can freely switch the type between Category, Page, and Custom Link. The corresponding object selection fields will change automatically via JavaScript.
Sorting and Order
The order of items within one parent is determined by the number in the item_sort field. It is filled automatically when adding an item (maximum value + 1). You can change the order in two ways:
- In the general list (Tree Editing tab): change the number in the Order column and click Save Changes at the bottom of the table.
- In the item edit form: specify the desired number in the Order field.
It is recommended to use sequential numbers (1, 2, 3...). Items with the same order are sorted by ID.
Hierarchy and Parent Items
Each item can have a parent selected from the list of all items in the same tree. Top level corresponds to parent_id = 0. Nesting is unlimited. When displayed on the site, nested items are automatically placed inside the parent item's <ul>.
In the parent item list, it is impossible to select the item itself (to avoid cycles) — this is taken into account in the item edit form.
Enabling and Disabling
Each item has an Enabled flag. If unchecked, the item is not displayed on the site but remains in the database. This is convenient for temporarily hiding menu items without deleting them.
Flag management:
- In the item list table — a checkbox in the Enabled column.
- In the item edit form — a similar checkbox.
Deleting an Item
To delete an item on the Tree Editing tab, click the Delete button (red icon). When deleting:
- the item itself is deleted;
- all its child items are deleted recursively;
- all translations of the deleted items are also deleted (cascade due to foreign key).
A confirmation prompt will appear before deletion.
Multilingual Support
Enabling and Configuring Languages
Detailed configuration is described in the Multilingual Configuration section. To recap:
- Enable the
toc_page_i18n_useoption. - Set the main language (
toc_page_i18n_lang_code_default). - Enable the required additional languages (
toc_page_i18n_lang_code_first_use, etc.) and set their codes.
Translating Titles and URLs
After enabling multilingual support, a Translations tab appears in the plugin admin panel. It is available only if a specific tree is selected (on the left in the tree list). This tab displays a table of all tree items with fields for each active additional language:
Title— translation of the item name;URL— translation of the link (if needed, e.g., for different language versions of pages).
Original values (in the main language) are shown in the Title column without editing capability (they are edited on the Tree Editing or Item tab).
To save translations, fill in the required fields and click Save Changes. Empty fields delete the translation (if it existed), leaving the original for that language.
How Language Detection Works
Language detection is performed in the toc_page_i18n_get_value() function. Logic:
- If multilingual support is disabled, the original is returned.
- The main language is determined from the plugin settings (
toc_page_get_default_lang()). - The current visitor's language is determined (
Cot::$usr['lang']or the main language if guest). - If the current language matches the main language, the original is always returned (translations for the main language are not used).
- Otherwise, the translation is loaded from
cot_toc_page_i18nbyitem_id,field_name,lang. If a translation is found, it is returned; otherwise, the original.
Thus, original fields are always used for the main language, and translations are applied only for additional languages.
Functions for i18n Developers
For developers, the following functions are available in toc_page.functions.php:
toc_page_i18n_enabled()— checks whether multilingual support is enabled.toc_page_get_active_langs()— returns an array of codes of active additional languages.toc_page_get_default_lang()— returns the main language code.toc_page_i18n_load($itemId, $fieldName, $lang)— loads a translation.toc_page_i18n_save($itemId, $fieldName, $lang, $value)— saves/deletes a translation.toc_page_i18n_get_value($itemId, $fieldName, $originalValue)— returns the value considering translation.
These functions can be used in custom extensions or templates.
Integration with Cotonti
Displaying a Tree on a Page
The plugin provides a simple way to display a tree anywhere on the site using the cot_toc_page_render($treeId) function.
Call methods:
- In a TPL template:
{PHP|cot_toc_page_render(1)}Where 1 is the tree ID.
- In PHP code (e.g., in a module or plugin):
$out = cot_toc_page_render(1);Then pass $out to the template.
The function returns ready HTML generated according to the toc_page.tpl template.
The cot_toc_page_render() Function
Signature:
function cot_toc_page_render($treeId, $tpl = 'toc_page')Parameters:
$treeId— the ID of the tree to display.$tpl— template name (without the.tplextension). Default istoc_page.
Function:
- Gets all enabled tree items via
toc_page_get_tree(). - Calls recursive rendering
toc_page_display(). - Returns HTML.
If the tree is empty or the ID is invalid, it returns an empty string.
The toc_page.tpl Template
The toc_page.tpl file (in the tpl/ folder) controls the tree markup. By default, it has the following structure:
<!-- BEGIN: LIST -->
<ul class="toc-page level-{LIST_LEVEL}">
<!-- BEGIN: ROW -->
<li class="toc-page-item level-{ROW_LEVEL}">
<!-- IF {ROW_URL} -->
<a href="{ROW_URL}">{ROW_TITLE}</a>
<!-- ELSE -->
<span class="toc-page-title">{ROW_TITLE}</span>
<!-- ENDIF -->
{ROW_ITEMS}
</li>
<!-- END: ROW -->
</ul>
<!-- END: LIST -->You can modify this template to fit your design. Available variables:
{LIST_LEVEL}— nesting level of the root list (usually 0);{ROW_LEVEL}— level of a specific item;{ROW_URL}— generated link (if any);{ROW_TITLE}— title (taking into account translation and HTML cleaning);{ROW_ITEMS}— recursively generated HTML of child items.
Using in TPL Templates
The most common scenario is to insert a tree into a sidebar or page content. Example:
<div class="block">
<h3>Knowledge Base</h3>
{PHP|cot_toc_page_render(1)}
</div>If you need to display different trees on different pages, you can use Cotonti conditional operators or PHP logic in the template.
For global visibility of the function in TPL, ensure the plugin is active and the function is registered (this happens automatically when toc_page.functions.php is loaded).
AJAX Page Search (Select2)
Enabling
To use Select2 for page selection, enable the Use AJAX page search via Select2 option in the plugin settings.
How It Works
When Select2 is enabled, in the add/edit item forms of the Page type, instead of a standard <select>, a hidden field and a Select2 element appear. The user enters at least 2 characters, after which an AJAX request is sent to the public URL index.php?r=toc_page&ajax=search&q=.... The server (file toc_page.ajax.php) searches for pages where page_state = 0 and the title contains the entered substring (case-insensitive). Results are returned in JSON format for Select2.
Access rights are additionally filtered: only administrators can use the search (in the current implementation, cot::$usr['isadmin'] is checked). Access for non-administrators is not provided, as AJAX is used only in the admin panel.
Configuring Minimum Length
The minimum input length for search is set in the JavaScript code in the toc_page.admin.php files (two places: the tree editing tab and the item editing tab). Find the line:
minimumInputLength: 2,and replace 2 with the desired number. After the change, save the file and clear the browser cache.
SQL Dump Generator
Purpose
The generate_toc_dump.php script is designed to automatically create an SQL dump of the plugin tables based on an HTML list. This is convenient when you need to quickly import a ready tree structure (for example, from an existing static menu) without manually adding items in the admin panel.
Preparing an HTML List
The HTML list must be a valid nested <ul><li> list. Rules:
- Each item is an
<li>, which may contain a nested<ul>. <nav></nav>inside<li>is ignored (you may omit it).- HTML entities are automatically converted.
- Do not leave extra spaces between the closing
</li>and the next<li>.
Example:
<ul>
<li>Parent
<ul>
<li>Child 1</li>
<li>Child 2</li>
</ul>
</li>
<li>Another parent</li>
</ul>Creating a Dump
- Open the
generate_toc_dump.phpfile in an editor. - Insert your HTML into the
$htmlvariable (between<<<'HTML'andHTML;). - Fill in the
$translationsarray (if translations are needed), where the key is the original and the value is the translation. - Specify tree parameters:
$treeId,$treeTitle,$treeDescription. - Check the table prefix (default
cot_). Replace if necessary. - Upload the file to the site root via FTP.
- Run it in the browser:
https://your-domain/generate_toc_dump.php. - The script will create a
toc_page_dump_YYYYMMDD_HHMMSS.sqlfile in the site root. - Download this file via FTP.
Importing into Database
- Go to phpMyAdmin and select your database.
- Go to the Import tab.
- Select the downloaded
.sqlfile. - Make sure the encoding is utf-8.
- Click Import.
Important: the script, when imported, deletes existing cot_toc_page_* tables and creates them anew. Make a backup if you already have data.
After import, the tree will appear in the plugin admin panel.
Advanced Functions
Functions for Retrieving Data
The following functions are defined in toc_page.functions.php:
toc_page_get_tree($treeId)— returns a nested array of all enabled tree items. Each item contains all table fields +children(array of children).cot_toc_page_render($treeId, $tpl = 'toc_page')— renders a tree according to the template.toc_page_display($items, $level, $tpl)— recursively outputs items. Usually not called directly.
Multilingual functions described earlier are also available for developers.
Working with Translations Directly
If you need to programmatically add or retrieve a translation, use:
// Load a translation
$translated = toc_page_i18n_load($itemId, 'item_title', 'en');
// Save a translation
toc_page_i18n_save($itemId, 'item_title', 'en', 'English title');
// Delete a translation (pass null or an empty string)
toc_page_i18n_save($itemId, 'item_title', 'en', '');These functions automatically handle insert/update/delete.
Frequently Asked Questions
Why is the tree not displayed?
- Check that the plugin is activated.
- Make sure the tree ID is correct and the tree contains at least one enabled item.
- Check that the correct call
{PHP|cot_toc_page_render(ID)}is used in the TPL template. - Clear the Cotonti cache.
- Make sure items are not disabled (Enabled flag).
How do I change the item type?
- Open the Tree Editing tab.
- Find the desired item and click the Edit button.
- In the opened Item tab, change the type in the dropdown list.
- Fill in the corresponding fields (category, page, or URL).
- Click Update.
How do I add a new language?
- In the plugin settings, enable multilingual support.
- In the field for an additional language, enter the code (e.g.,
de) and enable the Use flag. - Save the settings.
- Go to the Translations tab and fill in translations.
A maximum of two additional languages is supported (plus the main one).
How do I remove the plugin?
- Back up the database.
- In the Cotonti admin panel, go to Extensions → Plugins.
- Find Table of Contents Page and click Uninstall.
- If necessary, delete the plugin files from the
plugins/toc_pagedirectory.
Warning: when uninstalling the plugin, the
cot_toc_page_*tables are usually not removed automatically. If you want to completely clean the database, delete them manually via phpMyAdmin or run SQL commandsDROP TABLE IF EXISTS ....
Examples
Simple Tree
Suppose you need to create a sidebar menu with three items:
- Home
- About Us
- ContactsSteps:
- Create a tree with ID 1.
- Add three items of the Custom Link type with URLs:
/,/about,/contacts. - Set the order 1, 2, 3.
- Insert into the template:
<div class="sidebar">
{PHP|cot_toc_page_render(1)}
</div>Tree with Sublevels
Structure:
- Services
- Development
- Design
- Portfolio
- Websites
- LogosImplementation:
- Add the items Services and Portfolio as parent items (parent = 0).
- Add child items, selecting the corresponding parent item in the Parent field.
- Configure the order.
Multilingual Tree
It is required to display a menu in Russian and English.
- Enable multilingual support, set the main language
ru, additionalen. - Create items with Russian titles.
- Go to the Translations tab, enter English titles for each item.
- When visiting the site in English, the titles will automatically be replaced.
If no translation is set for an item, the Russian original will be displayed.
This guide covers the main aspects of working with the Table of Contents Page plugin version 1.1.4. For additional information, refer to the documentation on GitHub or the support forum.
Comments (0)
Page Discussion in Telegram
Recommended Products and Services
Table of Contents Page plugin for Cotonti
Content author
Offline
Sodium Carbonate
Last logged: 2026-08-25 00:09
- Page published: 2026-08-24 23:30
- Last update: 2026-08-24 23:46
- Language:
Связанные статьи
Overview and purpose of the "ToC Page" plugin for Cotonti
Overview and Purpose of the toc_page Plugin for CotontiVersion: 1.0.1Author: webitproffLicense:
Русский