diff --git a/modules/ai_automators/src/Form/AiChainForm.php b/modules/ai_automators/src/Form/AiChainForm.php index 4a6edd81138905e9f15a8127f60fd7d1291a4084..4364ae5cc2e8e1683ee6994943fb7c36737537b7 100644 --- a/modules/ai_automators/src/Form/AiChainForm.php +++ b/modules/ai_automators/src/Form/AiChainForm.php @@ -94,26 +94,25 @@ class AiChainForm extends FormBase { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state): array { - // Get entity type from the route. - [, , $entity_type] = explode(".", $this->routeMatch->getRouteName()); - if (empty($entity_type)) { - throw new NotFoundException('Entity type and bundle are required.'); - } - $this->entityType = $entity_type; + $entity = NULL; - // Load the bundle dynamically. - $bundle = NULL; - $parameters = $this->routeMatch->getParameters()->all(); - foreach ($parameters as $parameter => $value) { - if ($parameter !== 'entity_type') { - $bundle = $value; - } + // This is our route, so we know the only parameter is the entity. We have + // to get it this way, as the parameter is named for the entity's id in + // these routes so we cannot know what it will be until we have the entity. + if ($route_params = $this->routeMatch->getParameters()->all()) { + $entity = reset($route_params); } - if (empty($bundle)) { - $bundle = $entity_type; + $entity_type = $entity->getEntityType()->getBundleOf(); + + // But if there is some unexpected problem, abort trying to build the form. + if (empty($entity_type)) { + throw new NotFoundException('Entity type and bundle are required.'); } - $this->bundle = $bundle; + + // Set the required variables. + $this->entityType = $entity_type; + $bundle = $this->bundle = $entity->id(); // Get all fields, including base fields for the entity type and bundle. try { @@ -213,7 +212,6 @@ class AiChainForm extends FormBase { 'field' => $definition->get('field_name'), ]; - $route_params = $parameters; $route_params['field_config'] = implode('.', $field_config); $links['edit'] = [ diff --git a/modules/ai_automators/src/Plugin/Derivative/ChainLocalTasks.php b/modules/ai_automators/src/Plugin/Derivative/ChainLocalTasks.php index 3477b8562eed28d9b53d2b274d5c30108c98e80c..6f3343d3d974b9e595fe1c7060b326789f3877a9 100644 --- a/modules/ai_automators/src/Plugin/Derivative/ChainLocalTasks.php +++ b/modules/ai_automators/src/Plugin/Derivative/ChainLocalTasks.php @@ -54,10 +54,13 @@ class ChainLocalTasks extends DeriverBase implements ContainerDeriverInterface { if (!$entity_type->getBundleOf()) { continue; } + + // Handle the different menu structure of taxonomy terms. + $base_route = $entity_type->hasLinkTemplate('overview-form') ? "entity.$entity_type_id.overview_form" : "entity.$entity_type_id.edit_form"; $this->derivatives["entity.$entity_type_id.automator_chain"] = $base_plugin_definition; $this->derivatives["entity.$entity_type_id.automator_chain"]['title'] = $this->t('AI Automator Run Order'); $this->derivatives["entity.$entity_type_id.automator_chain"]['route_name'] = 'ai_automator.config_chain.' . $entity_type->getBundleOf(); - $this->derivatives["entity.$entity_type_id.automator_chain"]['base_route'] = "entity.$entity_type_id.edit_form"; + $this->derivatives["entity.$entity_type_id.automator_chain"]['base_route'] = $base_route; $this->derivatives["entity.$entity_type_id.automator_chain"]['weight'] = 100; } return $this->derivatives; diff --git a/modules/ai_automators/src/Routing/AutomatorRouteSubscriber.php b/modules/ai_automators/src/Routing/AutomatorRouteSubscriber.php index f654e5872a44b23e06f1558336073fa3d4dcc02a..1fa2cc56c967a843c58b8ca053e8997f6c2d6f40 100644 --- a/modules/ai_automators/src/Routing/AutomatorRouteSubscriber.php +++ b/modules/ai_automators/src/Routing/AutomatorRouteSubscriber.php @@ -12,21 +12,15 @@ use Symfony\Component\Routing\Route; */ class AutomatorRouteSubscriber implements ContainerInjectionInterface { - /** - * The entity type manager. - * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface - */ - protected EntityTypeManagerInterface $entityTypeManager; - /** * Constructs a new AutomatorRouteSubscriber object. * - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager) { - $this->entityTypeManager = $entity_type_manager; + public function __construct( + protected readonly EntityTypeManagerInterface $entityTypeManager, + ) { } /** @@ -34,7 +28,7 @@ class AutomatorRouteSubscriber implements ContainerInjectionInterface { */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity_type.manager') + $container->get('entity_type.manager'), ); } @@ -50,8 +44,34 @@ class AutomatorRouteSubscriber implements ContainerInjectionInterface { if (!$entity_type->getBundleOf()) { continue; } + + // Field UI base routes on these entities tend to be based around the + // canonical path, so we will use this as our base for this route. If this + // isn't implemented, the edit form link is a suitable proxy for it. + if (!$entity_type->hasLinkTemplate('edit-form') && !$entity_type->hasLinkTemplate('canonical')) { + continue; + } + + $path = $entity_type->hasLinkTemplate('canonical') ? $entity_type->getLinkTemplate('canonical') : $entity_type->getLinkTemplate('edit-form'); + + // But sometimes the edit-form link is itself a sub-path, usually ending + // with "/edit" so we will accommodate for that. + if (str_ends_with($path, '/edit')) { + $path = substr($path, -5); + } + + // If the path doesn't have the entity_type parameter, we'll need to add + // it. + if (!str_contains($path, '{' . $entity_type_id . '}')) { + $path = $path . '/{' . $entity_type_id . '}'; + } + + // Now we can just add our identifier to the end of the path. + $path .= '/automator_chain'; + + // $path = $entity_route->getPath(); $route = new Route( - '/admin/structure/types/manage/automator_chain/' . $entity_type->getBundleOf() . '/{' . $entity_type_id . '}', + $path, [ '_form' => '\Drupal\ai_automators\Form\AiChainForm', '_title' => 'AI Automator Run Order', @@ -60,6 +80,13 @@ class AutomatorRouteSubscriber implements ContainerInjectionInterface { '_permission' => 'administer ai_automator', ] ); + $route->addOptions([ + 'parameters' => [ + $entity_type_id => [ + 'type' => 'entity:' . $entity_type_id, + ], + ], + ]); $routes['ai_automator.config_chain.' . $entity_type->getBundleOf()] = $route; } return $routes;