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:
2026-02-04 08:35:04 -03:00
parent 43fa9208a9
commit 0c8f0fc778
17 changed files with 1153 additions and 117 deletions

View File

@@ -6,12 +6,12 @@ namespace Drupal\site_structure\Breadcrumb;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\NodeInterface;
use Drupal\site_structure\ParentEntityHandler\ParentEntityHandlerManagerInterface;
/**
* Provides a breadcrumb builder for section_page and content_page content types.
@@ -27,6 +27,13 @@ class SectionBreadcrumbBuilder implements BreadcrumbBuilderInterface {
*/
protected EntityTypeManagerInterface $entityTypeManager;
/**
* The parent entity handler manager.
*
* @var \Drupal\site_structure\ParentEntityHandler\ParentEntityHandlerManagerInterface
*/
protected ParentEntityHandlerManagerInterface $handlerManager;
/**
* Content types that this builder applies to.
*
@@ -39,9 +46,15 @@ class SectionBreadcrumbBuilder implements BreadcrumbBuilderInterface {
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\site_structure\ParentEntityHandler\ParentEntityHandlerManagerInterface $handler_manager
* The parent entity handler manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
ParentEntityHandlerManagerInterface $handler_manager,
) {
$this->entityTypeManager = $entity_type_manager;
$this->handlerManager = $handler_manager;
}
/**
@@ -79,25 +92,8 @@ class SectionBreadcrumbBuilder implements BreadcrumbBuilderInterface {
$context = $this->getParentContext($node);
if ($context) {
switch ($context['type']) {
case 'user':
// User context: Home > User Name > ... > Current Page.
$this->addUserBreadcrumb($breadcrumb, $context['entity']);
break;
case 'group':
// Group context: Home > Group Name > ... > Current Page.
$this->addGroupBreadcrumb($breadcrumb, $context['entity']);
break;
default:
// Node or taxonomy context: use site section.
if ($node->hasField('field_site_section') && !$node->get('field_site_section')->isEmpty()) {
$term_id = $node->get('field_site_section')->target_id;
$this->addTaxonomyBreadcrumbs($breadcrumb, $term_id);
}
break;
}
// Use the handler manager to build breadcrumbs for the context entity.
$this->handlerManager->buildBreadcrumbForEntity($breadcrumb, $context['entity']);
}
else {
// No parent context, check for site section directly.
@@ -193,32 +189,6 @@ class SectionBreadcrumbBuilder implements BreadcrumbBuilderInterface {
return NULL;
}
/**
* Adds user breadcrumb.
*
* @param \Drupal\Core\Breadcrumb\Breadcrumb $breadcrumb
* The breadcrumb object.
* @param \Drupal\Core\Entity\EntityInterface $user
* The user entity.
*/
protected function addUserBreadcrumb(Breadcrumb $breadcrumb, EntityInterface $user): void {
$breadcrumb->addCacheableDependency($user);
$breadcrumb->addLink($user->toLink());
}
/**
* Adds group breadcrumb.
*
* @param \Drupal\Core\Breadcrumb\Breadcrumb $breadcrumb
* The breadcrumb object.
* @param \Drupal\Core\Entity\EntityInterface $group
* The group entity.
*/
protected function addGroupBreadcrumb(Breadcrumb $breadcrumb, EntityInterface $group): void {
$breadcrumb->addCacheableDependency($group);
$breadcrumb->addLink($group->toLink());
}
/**
* Adds breadcrumbs based on taxonomy hierarchy.
*