mirror of
https://gitlab.unicamp.br/infimecc_drupal11_modules/structural_pages.git
synced 2026-03-09 18:07:42 -03:00
Implement ParentEntityHandler plugin system for extensible entity support
Replace hardcoded entity type checks with a plugin-based architecture using PHP 8 attributes. This allows adding new parent entity types without modifying core module files. Changes: - Add ParentEntityHandler attribute, interface, base class, and manager - Create built-in handlers for taxonomy_term, user, and node entities - Move Group support to site_structure_group submodule (fixes class not found error when Group module is not installed) - Refactor SiteStructureSettingsForm to use handler manager - Refactor SiteStructureMenuBlock to use handler manager - Refactor SectionBreadcrumbBuilder to use handler manager - Update site_structure.module to use handler manager for clearsSiteSection - Update documentation and translations Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
80
src/Plugin/ParentEntityHandler/TaxonomyTermHandler.php
Normal file
80
src/Plugin/ParentEntityHandler/TaxonomyTermHandler.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\site_structure\Plugin\ParentEntityHandler;
|
||||
|
||||
use Drupal\Core\Breadcrumb\Breadcrumb;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
use Drupal\site_structure\Attribute\ParentEntityHandler;
|
||||
use Drupal\site_structure\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: ['site_section'],
|
||||
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 getSiteSectionId(EntityInterface $entity): int|string|null {
|
||||
if (!$entity instanceof TermInterface) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Only site_section terms can be site sections.
|
||||
if ($entity->bundle() !== 'site_section') {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return $entity->id();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user