mirror of
https://gitlab.unicamp.br/infimecc_drupal11_modules/structural_pages.git
synced 2026-03-08 01:27:42 -03:00
Renames the module from site_structure to structural_pages and pluralizes the taxonomy vocabulary machine name from site_section to site_sections, updating all config, PHP, translations, and documentation references while preserving field_site_section, clears_site_section, and $site_section_id. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
224 lines
6.4 KiB
Plaintext
224 lines
6.4 KiB
Plaintext
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Install, update and uninstall functions for the Structural Pages module.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Drupal\taxonomy\Entity\Term;
|
|
|
|
/**
|
|
* Implements hook_install().
|
|
*/
|
|
function structural_pages_install(): void {
|
|
// Create default terms for site_sections vocabulary.
|
|
_structural_pages_create_default_terms();
|
|
|
|
// Display success message.
|
|
\Drupal::messenger()->addStatus(t('Structural Pages module installed successfully. Configure Pathauto patterns at /admin/config/search/path/patterns'));
|
|
}
|
|
|
|
/**
|
|
* Creates default terms for the site_sections vocabulary.
|
|
*/
|
|
function _structural_pages_create_default_terms(): void {
|
|
$vocabulary = 'site_sections';
|
|
|
|
// 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 structural_pages_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_sections vocabulary.
|
|
$tids = \Drupal::entityQuery('taxonomy_term')
|
|
->accessCheck(FALSE)
|
|
->condition('vid', 'site_sections')
|
|
->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_sections_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_sections',
|
|
];
|
|
|
|
$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('Structural Pages module uninstalled successfully.'));
|
|
}
|
|
|
|
/**
|
|
* Update content_page content type name from "Guide Page" to "Content Page".
|
|
*/
|
|
function structural_pages_update_10001(): void {
|
|
$config = \Drupal::configFactory()->getEditable('node.type.content_page');
|
|
if (!$config->isNew()) {
|
|
$current_name = $config->get('name');
|
|
// Update if still using old name.
|
|
if ($current_name === 'Guide Page') {
|
|
$config->set('name', 'Content Page');
|
|
$config->set('description', 'Pages with hierarchical parent-child structure for content organization.');
|
|
$config->save();
|
|
\Drupal::messenger()->addStatus(t('Updated content_page content type name to "Content Page".'));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_requirements().
|
|
*/
|
|
function structural_pages_requirements(string $phase): array {
|
|
$requirements = [];
|
|
|
|
if ($phase === 'runtime') {
|
|
// Check if site_sections vocabulary has terms.
|
|
$term_count = \Drupal::entityQuery('taxonomy_term')
|
|
->accessCheck(FALSE)
|
|
->condition('vid', 'site_sections')
|
|
->count()
|
|
->execute();
|
|
|
|
if ($term_count === 0) {
|
|
$requirements['structural_pages_terms'] = [
|
|
'title' => t('Structural Pages'),
|
|
'value' => t('No terms in site_sections vocabulary'),
|
|
'description' => t('The Structural Pages module requires terms in the "Site Sections" vocabulary. <a href=":url">Add terms</a>.', [
|
|
':url' => '/admin/structure/taxonomy/manage/site_sections/add',
|
|
]),
|
|
'severity' => REQUIREMENT_WARNING,
|
|
];
|
|
}
|
|
else {
|
|
$requirements['structural_pages_terms'] = [
|
|
'title' => t('Structural Pages'),
|
|
'value' => t('@count terms configured', ['@count' => $term_count]),
|
|
'severity' => REQUIREMENT_OK,
|
|
];
|
|
}
|
|
}
|
|
|
|
return $requirements;
|
|
}
|