ptoc-8.x-1.x-dev/ptoc.module
ptoc.module
<?php
/**
* @file
* Hook implementations for the ptoc module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function ptoc_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the ptoc module.
case 'help.page.ptoc':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Use the ptoc view mode to create an in-page table of contents.') . '</p>';
return $output;
// The "Manage display" tab for a paragraph type in the ptoc view mode.
case 'entity.entity_view_display.paragraph.view_mode':
$output = '';
if (\Drupal::routeMatch()->getParameter('view_mode_name') === 'ptoc') {
$output .= '<p>' . t('This view mode will be used for the in-page table of contents. If more than one field is enabled for this view mode, then only the first one will be used for the link text.') . '</p>';
}
return $output;
default:
}
}
/**
* Implements hook_preprocess_HOOK().
*
* Add an id attribute to each paragraph's default view mode so that you can
* link to it from the table of contents.
*/
function ptoc_preprocess_paragraph(&$variables) {
if ('default' == $variables['elements']['#view_mode']) {
$variables['attributes']['id'] = 'paragraph-' . $variables['paragraph']->id();
}
if ($variables['elements']['#view_mode'] === 'ptoc') {
// Let the Twig template know which field to use for the link text.
$variables['ptoc_link_text'] = key($variables['content']);
}
if (Drupal::config('ptoc.settings')->get('debug')) {
$variables['attributes']['class'][] = 'ptoc-debug';
$variables['#attached']['library'][] = 'ptoc/ptoc-debug';
}
}
/**
* Implements hook_theme_suggestions_HOOK().
*
* Use our Twig template for paragraphs in the table of contents.
*/
function ptoc_theme_suggestions_paragraph(array $variables) {
$suggestions = [];
if ('ptoc' == $variables['elements']['#view_mode']) {
$suggestions[] = 'ptoc_paragraph';
}
return $suggestions;
}
/**
* Implements hook_theme_suggestions_HOOK().
*
* Use our Twig template for the ptoc node view.
*/
function ptoc_theme_suggestions_node(array $variables) {
$suggestions = [];
if ('ptoc' == $variables['elements']['#view_mode']) {
$suggestions[] = 'ptoc_node';
}
return $suggestions;
}
/**
* Implements hook_theme().
*
* Create a theme hook for our ToC paragraph template.
*/
function ptoc_theme() {
$items = [];
$items['ptoc_paragraph'] = [
'base hook' => 'paragraph',
];
$items['ptoc_node'] = [
'base hook' => 'node',
];
return $items;
}
