View source
<?php
namespace Drupal\civicrm_entity\Routing;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\Routing\RouteCollection;
final class RouteSubscriber extends RouteSubscriberBase {
private $entityTypeManager;
protected $moduleHandler;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler) {
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
}
protected function alterRoutes(RouteCollection $collection) {
if (!$this->moduleHandler
->moduleExists('field_ui')) {
return;
}
$has_layout_builder = $this->moduleHandler
->moduleExists('layout_builder');
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
if (!$entity_type
->get('civicrm_entity_ui_exposed')) {
continue;
}
if (!$entity_type
->hasKey('bundle')) {
continue;
}
$field_ui_routes = [
"entity.{$entity_type_id}.field_ui_fields" => [
'bundle' => $entity_type_id,
],
"entity.field_config.{$entity_type_id}_field_edit_form" => [
'bundle' => $entity_type_id,
],
"entity.field_config.{$entity_type_id}_storage_edit_form" => [
'bundle' => $entity_type_id,
],
"entity.field_config.{$entity_type_id}_field_delete_form" => [
'bundle' => $entity_type_id,
],
"field_ui.field_storage_config_add_{$entity_type_id}" => [
'bundle' => $entity_type_id,
],
"entity.entity_form_display.{$entity_type_id}.default" => [
'bundle' => $entity_type_id,
],
"entity.entity_form_display.{$entity_type_id}.form_mode" => [
'bundle' => $entity_type_id,
],
"entity.entity_view_display.{$entity_type_id}.default" => [
'bundle' => $entity_type_id,
],
"entity.entity_view_display.{$entity_type_id}.view_mode" => [
'bundle' => $entity_type_id,
],
];
if ($has_layout_builder) {
$field_ui_routes["layout_builder.defaults.{$entity_type_id}.view"] = [
'bundle' => $entity_type_id,
];
$field_ui_routes["layout_builder.overrides.{$entity_type_id}.view"] = [
'bundle' => $entity_type_id,
];
$field_ui_routes["layout_builder.defaults.{$entity_type_id}.discard_changes"] = [
'bundle' => $entity_type_id,
];
$field_ui_routes["layout_builder.defaults.{$entity_type_id}.disable"] = [
'bundle' => $entity_type_id,
];
}
foreach ($field_ui_routes as $route_name => $defaults) {
$route = $collection
->get($route_name);
assert($route !== NULL);
foreach ($defaults as $name => $default) {
$route
->setDefault($name, $default);
}
}
}
}
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
$events[RoutingEvents::ALTER] = [
'onAlterRoutes',
-200,
];
return $events;
}
}