ckeditor_taxonomy_glossary-1.0.0-alpha1/ckeditor_taxonomy_glossary.module
ckeditor_taxonomy_glossary.module
<?php
/**
* @file
* Ckeditor Taxonomy Glossary module.
*
* Provides glossary functionality with CKEditor 5 integration and tooltips.
*/
declare(strict_types=1);
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Access\CsrfRequestHeaderAccessCheck;
/**
* Implements hook_help().
*/
function ckeditor_taxonomy_glossary_help($route_name, RouteMatchInterface $route_match): string {
switch ($route_name) {
case 'help.page.ckeditor_taxonomy_glossary':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Ckeditor Taxonomy Glossary module provides glossary functionality with CKEditor 5 integration. It allows editors to link words to glossary terms, which display tooltips on the frontend.') . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Creating glossary terms') . '</dt>';
$output .= '<dd>' . t('Glossary terms are managed as taxonomy terms in the "Glossary" vocabulary.') . '</dd>';
$output .= '<dt>' . t('Linking to terms') . '</dt>';
$output .= '<dd>' . t('Use the Glossary Link button in CKEditor 5 to link text to glossary terms.') . '</dd>';
$output .= '<dt>' . t('Viewing tooltips') . '</dt>';
$output .= '<dd>' . t('When users hover over or click on linked glossary terms, a tooltip displays the term description.') . '</dd>';
$output .= '</dl>';
return $output;
default:
return '';
}
}
/**
* Implements hook_page_attachments().
*/
function ckeditor_taxonomy_glossary_page_attachments(array &$attachments): void {
$route_match = \Drupal::routeMatch();
$route_name = $route_match->getRouteName();
// Attach admin CSS library on text format configuration pages.
if (in_array($route_name, [
'filter.format_form',
'entity.filter_format.edit_form',
'entity.filter_format.collection',
])) {
$attachments['#attached']['library'][] = 'ckeditor_taxonomy_glossary/admin.glossarylink';
}
// Attach modal utilities library and user permissions on node edit pages
if (in_array($route_name, [
'node.add',
'entity.node.edit_form',
'entity.node.content_translation_add',
'entity.node.content_translation_edit',
])) {
$current_user = \Drupal::currentUser();
$language_manager = \Drupal::languageManager();
// Pass user permissions to frontend
$user_permissions = [];
if ($current_user->hasPermission('create glossary terms via editor')) {
$user_permissions[] = 'create glossary terms via editor';
}
if ($current_user->hasPermission('link to glossary terms')) {
$user_permissions[] = 'link to glossary terms';
}
// Pass language information for multilingual sites
$languages = $language_manager->getLanguages();
$language_list = [];
foreach ($languages as $langcode => $language) {
$language_list[$langcode] = [
'name' => $language->getName(),
'direction' => $language->getDirection(),
];
}
$attachments['#attached']['drupalSettings']['user']['permissions'] = $user_permissions;
$attachments['#attached']['drupalSettings']['language'] = $language_list;
$attachments['#attached']['drupalSettings']['path']['currentLanguage'] = $language_manager->getCurrentLanguage()->getId();
$attachments['#attached']['drupalSettings']['csrfToken'] = \Drupal::csrfToken()->get(CsrfRequestHeaderAccessCheck::TOKEN_KEY);
$attachments['#attached']['library'][] = 'ckeditor_taxonomy_glossary/modal_utilities';
}
}
