Files
structural_pages/src/Plugin/ParentEntityHandler/TaxonomyTermHandler.php
Quintino A. G. Souza c1274bf3ce Make site section vocabulary configurable and simplify settings form
Add a vocabulary selector to the settings form so the site section
vocabulary is no longer hardcoded. Remove node bundle checkboxes
(section_page/content_page are always included), keep user and group
as configurable parent targets. All hardcoded 'site_sections'
references in PHP replaced with dynamic config reads via helper.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-05 13:22:57 -03:00

93 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Drupal\structural_pages\Plugin\ParentEntityHandler;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\structural_pages\Attribute\ParentEntityHandler;
use Drupal\structural_pages\ParentEntityHandler\ParentEntityHandlerBase;
use Drupal\taxonomy\TermInterface;
/**
* Handler for taxonomy term entities.
*/
#[ParentEntityHandler(
id: 'taxonomy_term',
label: new TranslatableMarkup('Taxonomy Vocabularies (taxonomy_term)'),
entity_type_id: 'taxonomy_term',
clears_site_section: FALSE,
sort_field: 'name',
bundle_restrictions: [],
weight: 0,
)]
class TaxonomyTermHandler extends ParentEntityHandlerBase {
/**
* {@inheritdoc}
*/
public function getEntityFromRoute(\Drupal\Core\Routing\RouteMatchInterface $route_match): ?EntityInterface {
$term = $route_match->getParameter('taxonomy_term');
if (!$term instanceof TermInterface) {
return NULL;
}
if (!$this->handlesEntity($term)) {
return NULL;
}
return $term;
}
/**
* {@inheritdoc}
*/
public function buildBreadcrumb(Breadcrumb $breadcrumb, EntityInterface $entity): void {
if (!$entity instanceof TermInterface) {
return;
}
// Add all ancestors in the taxonomy hierarchy.
$term_storage = $this->entityTypeManager->getStorage('taxonomy_term');
$ancestors = $term_storage->loadAllParents($entity->id());
$ancestors = array_reverse($ancestors);
foreach ($ancestors as $ancestor) {
$breadcrumb->addCacheableDependency($ancestor);
$breadcrumb->addLink($ancestor->toLink());
}
}
/**
* {@inheritdoc}
*/
public function handlesEntity(EntityInterface $entity): bool {
if ($entity->getEntityTypeId() !== 'taxonomy_term') {
return FALSE;
}
$vocabulary = \Drupal::config('structural_pages.settings')
->get('site_section_vocabulary') ?? 'site_sections';
return $entity->bundle() === $vocabulary;
}
/**
* {@inheritdoc}
*/
public function getSiteSectionId(EntityInterface $entity): int|string|null {
if (!$entity instanceof TermInterface) {
return NULL;
}
if (!$this->handlesEntity($entity)) {
return NULL;
}
return $entity->id();
}
}