Files
structural_pages/src/ParentEntityHandler/ParentEntityHandlerInterface.php
Quintino A. G. Souza 0c8f0fc778 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>
2026-02-04 08:35:04 -03:00

125 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Drupal\site_structure\ParentEntityHandler;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Interface for parent entity handler plugins.
*
* Parent entity handlers define how different entity types behave when used
* as parent entities for content_page nodes in the site structure hierarchy.
*/
interface ParentEntityHandlerInterface {
/**
* Gets the entity type ID this handler manages.
*
* @return string
* The entity type ID.
*/
public function getEntityTypeId(): string;
/**
* Gets the human-readable label of this handler.
*
* @return string
* The handler label.
*/
public function getLabel(): string;
/**
* Checks if this handler is available.
*
* A handler is available if its provider module is installed and the
* entity type exists.
*
* @return bool
* TRUE if the handler is available, FALSE otherwise.
*/
public function isAvailable(): bool;
/**
* Checks if this handler clears field_site_section when used as parent.
*
* Some entity types (like users and groups) act as context containers
* themselves, so content pages under them don't inherit a site section.
*
* @return bool
* TRUE if field_site_section should be cleared, FALSE otherwise.
*/
public function clearsSiteSection(): bool;
/**
* Gets the field to use for sorting entities of this type.
*
* @return string
* The sort field name.
*/
public function getSortField(): string;
/**
* Gets the route parameter name for detecting entities on routes.
*
* @return string
* The route parameter name.
*/
public function getRouteParameter(): string;
/**
* Gets the bundle restrictions for this handler.
*
* @return array
* An array of bundle IDs, or empty array for no restrictions.
*/
public function getBundleRestrictions(): array;
/**
* Gets an entity from the current route.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
*
* @return \Drupal\Core\Entity\EntityInterface|null
* The entity from the route, or NULL if not found or not applicable.
*/
public function getEntityFromRoute(RouteMatchInterface $route_match): ?EntityInterface;
/**
* Checks if this handler handles the given entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to check.
*
* @return bool
* TRUE if this handler handles the entity, FALSE otherwise.
*/
public function handlesEntity(EntityInterface $entity): bool;
/**
* Builds breadcrumb entries for the given entity.
*
* @param \Drupal\Core\Breadcrumb\Breadcrumb $breadcrumb
* The breadcrumb to add entries to.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to build breadcrumb for.
*/
public function buildBreadcrumb(Breadcrumb $breadcrumb, EntityInterface $entity): void;
/**
* Gets the site section ID from an entity of this type.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* @return int|string|null
* The site section term ID, or NULL if not applicable.
*/
public function getSiteSectionId(EntityInterface $entity): int|string|null;
}