From 706a653c04b6be24f464b32853327385afd46d93 Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Fri, 3 Nov 2023 01:14:15 -0300 Subject: [PATCH] Maybe this works right now? --- src/Controller/Redirect.php | 26 +++++++++ src/Entity/FragariaRedirectConfigEntity.php | 17 ++++++ src/Form/FragariaRedirectConfigEntityForm.php | 17 +++++- .../FragariaRedirectRoutingService.php | 56 +++++++++++++------ 4 files changed, 97 insertions(+), 19 deletions(-) diff --git a/src/Controller/Redirect.php b/src/Controller/Redirect.php index c4aa8e0..1492116 100644 --- a/src/Controller/Redirect.php +++ b/src/Controller/Redirect.php @@ -10,6 +10,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Controller\ControllerBase; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\fragaria\Entity\FragariaRedirectConfigEntity; @@ -87,6 +88,31 @@ public function redirect_processor(Request $request, $key) { } } + /** + * Capture the payload. Send to that happy place. + * + * @return \Symfony\Component\HttpFoundation\Response + * A simple string and Redirect response. + */ + public function redirect_do(Request $request, ContentEntityInterface $node) { + $entity = $this->getFragariaEntityFromRouteMatch($this->routeMatch); + if ($entity) { + if ($node) { + $url = $node->toUrl('canonical', ['absolute' => FALSE])->toString(); + $response = new RedirectResponse($url, (int) $entity->getRedirectHttpCode()); + return $response; + } + else { + throw new NotFoundHttpException(); + } + } + else { + throw new NotFoundHttpException(); + } + } + + + /** * Capture the payload. Send to that happy place. * diff --git a/src/Entity/FragariaRedirectConfigEntity.php b/src/Entity/FragariaRedirectConfigEntity.php index 9496f5a..5ad3380 100644 --- a/src/Entity/FragariaRedirectConfigEntity.php +++ b/src/Entity/FragariaRedirectConfigEntity.php @@ -49,6 +49,7 @@ * "redirect_http_code", * "cache", * "active", + * "do_replacement" * }, * links = { * "edit-form" = "/admin/config/archipelago/fragariaredirect/{fragariaredirect_entity}/edit", @@ -148,6 +149,14 @@ class FragariaRedirectConfigEntity extends ConfigEntityBase implements FragariaC */ protected $active = TRUE; + + /** + * Ignore everything, just use the NODE UUID. + * + * @var bool + */ + protected $do_replacement = FALSE; + /** * The Label for this config entity. * @@ -314,5 +323,13 @@ public function setSearchApiFieldValuePrefixes(array $search_api_field_value_pre $this->search_api_field_value_prefixes = $search_api_field_value_prefixes; } + public function isDoReplacement(): bool { + return $this->do_replacement; + } + + public function setDoReplacement(bool $do_replacement): void { + $this->do_replacement = $do_replacement; + } + } diff --git a/src/Form/FragariaRedirectConfigEntityForm.php b/src/Form/FragariaRedirectConfigEntityForm.php index ac4b751..85a72a0 100644 --- a/src/Form/FragariaRedirectConfigEntityForm.php +++ b/src/Form/FragariaRedirectConfigEntityForm.php @@ -110,6 +110,13 @@ public function form(array $form, FormStateInterface $form_state) { '#disabled' => !$fragariaredirect_config->isNew(), '#description' => $this->t('Unique Machine name for this Fragaria Redirect Entity.'), ], + 'do_replacement' => [ + '#type' => 'checkbox', + '#title' => $this->t('Enable this if you removed your do/{uuid} path aliases but still (or finally) believe having REAL PURLs is important.'), + '#title' => $this->t('This will disable suffixes, search api field matching and the Variable part will become the UUID of the node'), + '#required' => FALSE, + '#default_value' => (!$fragariaredirect_config->isNew()) ? $fragariaredirect_config->getPathPrefix() : NULL, + ], 'path_prefix' => [ '#type' => 'textfield', '#title' => $this->t('The Prefix (that follows your domain) for the Redirect Route.'), @@ -203,13 +210,19 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $search_api_field_value_prefixes = array_map(function ($line) { return $line ? trim($line) : NULL;}, explode("\n", $search_api_field_value_prefixes)); $search_api_field_value_prefixes = array_filter($search_api_field_value_prefixes); - $search_api_field_value_suffixes = $form_state->getValue('search_api_field_value_suffixes_element',''); $search_api_field_value_suffixes = array_map(function ($line) { return $line ? trim($line) : NULL;}, explode("\n", $search_api_field_value_suffixes)); $search_api_field_value_suffixes = array_filter($search_api_field_value_suffixes); - + if ($form_state->getValue('do_replacement')) { + $search_api_field_value_suffixes = []; + $form_state->setValue('search_api_index', NULL); + $form_state->setValue('search_api_field', NULL); + $search_api_field_value_prefixes = []; + $suffixes = []; + } $this->entity = $this->buildEntity($form, $form_state); + this->entity->setDoReplacement($form_state->getValue('do_replacement') ? TRUE : FALSE); $this->entity->setPathSuffixes(is_array($suffixes) ? $suffixes : []); $this->entity->setSearchApiFieldValueSuffixes(is_array($search_api_field_value_suffixes) ? $search_api_field_value_suffixes : []); $this->entity->setSearchApiFieldValuePrefixes(is_array($search_api_field_value_prefixes) ? $search_api_field_value_prefixes : []); diff --git a/src/Routing/FragariaRedirectRoutingService.php b/src/Routing/FragariaRedirectRoutingService.php index af6826c..3b5f477 100644 --- a/src/Routing/FragariaRedirectRoutingService.php +++ b/src/Routing/FragariaRedirectRoutingService.php @@ -80,27 +80,49 @@ public function redirect_routes(): RouteCollection { ] ); $route->setDefault('fragariaredirect_entity', $entity->id()); - $route_collection->add('fragaria_redirect.'.$entity->id(), $route); - - if ($entity->getVariablePathSuffix()) { - $route_variable = clone $route; - $route_variable->setPath($route_variable->getPath() . '/{catch_all}'); - $route_variable->setOption('_controller', 'Drupal\fragaria\Controller\Redirect::redirect_processor_variable'); - $route_variable->setDefault('catch_all', ''); - $route_collection->add( - 'fragaria_redirect.' . $entity->id() . '.variable', $route_variable + if ($entity->isDoReplacement()) { + $route->setRequirement('key', "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"); + $route->setOptions( + ["parameters" => [ + "key" => [ + "type" => 'entity:node'], + ["resource_type" => + ["type" => 'ado'] + ] + ]]); + $route->setOption( + '_controller', + 'Drupal\fragaria\Controller\Redirect::redirect_do' ); - } - else { - $suffixes = $entity->getPathSuffixes(); - foreach ($suffixes as $key => $suffix) { - $suffix = trim(trim($suffix), '/'); - $route_suffix = clone $route; - $route_suffix->setPath($route_suffix->getPath() . '/' . $suffix); + + if (!$entity->isDoReplacement()) { + if ($entity->getVariablePathSuffix()) { + $route_variable = clone $route; + $route_variable->setPath( + $route_variable->getPath() . '/{catch_all}' + ); + $route_variable->setOption( + '_controller', + 'Drupal\fragaria\Controller\Redirect::redirect_processor_variable' + ); + $route_variable->setDefault('catch_all', ''); $route_collection->add( - 'fragaria_redirect.' . $entity->id() . '.' . $key, $route_suffix + 'fragaria_redirect.' . $entity->id() . '.variable', + $route_variable ); + + } + else { + $suffixes = $entity->getPathSuffixes(); + foreach ($suffixes as $key => $suffix) { + $suffix = trim(trim($suffix), '/'); + $route_suffix = clone $route; + $route_suffix->setPath($route_suffix->getPath() . '/' . $suffix); + $route_collection->add( + 'fragaria_redirect.' . $entity->id() . '.' . $key, $route_suffix + ); + } } } }