Files
structural_pages/site_structure.install
Quintino A. G. Souza 8a42a6f1c1 Initial commit: Site Structure module for Drupal
Drupal module that provides hierarchical site structure management
with support for sections, categories, and content items. Includes
path aliases with tokens, breadcrumb integration, and admin interface.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 19:55:00 -03:00

207 lines
5.7 KiB
Plaintext

<?php
/**
* @file
* Install, update and uninstall functions for the Site Structure module.
*/
declare(strict_types=1);
use Drupal\taxonomy\Entity\Term;
/**
* Implements hook_install().
*/
function site_structure_install(): void {
// Create default terms for site_section vocabulary.
_site_structure_create_default_terms();
// Display success message.
\Drupal::messenger()->addStatus(t('Site Structure module installed successfully. Configure Pathauto patterns at /admin/config/search/path/patterns'));
}
/**
* Creates default terms for the site_section vocabulary.
*/
function _site_structure_create_default_terms(): void {
$vocabulary = 'site_section';
// Check if terms already exist (avoid duplication on reinstall).
$existing = \Drupal::entityQuery('taxonomy_term')
->accessCheck(FALSE)
->condition('vid', $vocabulary)
->count()
->execute();
if ($existing > 0) {
return;
}
// Structure: name => children.
$terms_structure = [
'News' => [],
'Events' => [],
'People' => [],
'Institutional' => [
'About',
'Communication',
'Information and Services',
'Team',
'Management',
'Inclusion and Belonging',
],
'Undergraduate' => [
'Statistics',
'Mathematics',
'Applied Mathematics',
'Mathematics Teaching',
],
'Graduate' => [
'Statistics Program',
'Mathematics Program',
'Applied Mathematics Program',
],
'Research' => [],
'Extension' => [],
'Administration' => [],
'Departments' => [
'Statistics Department',
'Mathematics Department',
'Applied Mathematics Department',
],
'Library' => [],
'IT Services' => [],
];
$weight = 0;
foreach ($terms_structure as $parent_name => $children) {
// Create parent term.
$parent_term = Term::create([
'vid' => $vocabulary,
'name' => $parent_name,
'weight' => $weight++,
]);
$parent_term->save();
// Create child terms.
$child_weight = 0;
foreach ($children as $child_name) {
$child_term = Term::create([
'vid' => $vocabulary,
'name' => $child_name,
'parent' => $parent_term->id(),
'weight' => $child_weight++,
]);
$child_term->save();
}
}
}
/**
* Implements hook_uninstall().
*/
function site_structure_uninstall(): void {
$entity_type_manager = \Drupal::entityTypeManager();
// Remove nodes from module's content types.
foreach (['section_page', 'content_page'] as $bundle) {
$nids = \Drupal::entityQuery('node')
->accessCheck(FALSE)
->condition('type', $bundle)
->execute();
if ($nids) {
$nodes = $entity_type_manager->getStorage('node')->loadMultiple($nids);
$entity_type_manager->getStorage('node')->delete($nodes);
\Drupal::messenger()->addWarning(t('Deleted @count @bundle nodes.', [
'@count' => count($nids),
'@bundle' => $bundle,
]));
}
}
// Remove terms from site_section vocabulary.
$tids = \Drupal::entityQuery('taxonomy_term')
->accessCheck(FALSE)
->condition('vid', 'site_section')
->execute();
if ($tids) {
$terms = $entity_type_manager->getStorage('taxonomy_term')->loadMultiple($tids);
$entity_type_manager->getStorage('taxonomy_term')->delete($terms);
}
// Remove configurations in correct order (dependencies first).
$configs_to_delete = [
// Views.
'views.view.child_pages',
// Pathauto patterns.
'pathauto.pattern.section_page',
'pathauto.pattern.content_page',
'pathauto.pattern.site_section_term',
// Entity displays.
'core.entity_form_display.node.section_page.default',
'core.entity_view_display.node.section_page.default',
'core.entity_form_display.node.content_page.default',
'core.entity_view_display.node.content_page.default',
// Field instances.
'field.field.node.section_page.field_site_section',
'field.field.node.section_page.body',
'field.field.node.content_page.field_parent_page',
'field.field.node.content_page.field_site_section',
'field.field.node.content_page.body',
// Field storages (only if not used by other bundles).
'field.storage.node.field_site_section',
'field.storage.node.field_parent_page',
// Node types.
'node.type.section_page',
'node.type.content_page',
// Vocabulary.
'taxonomy.vocabulary.site_section',
];
$config_factory = \Drupal::configFactory();
foreach ($configs_to_delete as $config_name) {
$config = $config_factory->getEditable($config_name);
if (!$config->isNew()) {
$config->delete();
}
}
\Drupal::messenger()->addStatus(t('Site Structure module uninstalled successfully.'));
}
/**
* Implements hook_requirements().
*/
function site_structure_requirements(string $phase): array {
$requirements = [];
if ($phase === 'runtime') {
// Check if site_section vocabulary has terms.
$term_count = \Drupal::entityQuery('taxonomy_term')
->accessCheck(FALSE)
->condition('vid', 'site_section')
->count()
->execute();
if ($term_count === 0) {
$requirements['site_structure_terms'] = [
'title' => t('Site Structure'),
'value' => t('No terms in site_section vocabulary'),
'description' => t('The Site Structure module requires terms in the "Site Section" vocabulary. <a href=":url">Add terms</a>.', [
':url' => '/admin/structure/taxonomy/manage/site_section/add',
]),
'severity' => REQUIREMENT_WARNING,
];
}
else {
$requirements['site_structure_terms'] = [
'title' => t('Site Structure'),
'value' => t('@count terms configured', ['@count' => $term_count]),
'severity' => REQUIREMENT_OK,
];
}
}
return $requirements;
}