Skip to content

Commit

Permalink
Maybe this works right now?
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoPino committed Nov 3, 2023
1 parent d735623 commit 706a653
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 19 deletions.
26 changes: 26 additions & 0 deletions src/Controller/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Entity/FragariaRedirectConfigEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* "redirect_http_code",
* "cache",
* "active",
* "do_replacement"
* },
* links = {
* "edit-form" = "/admin/config/archipelago/fragariaredirect/{fragariaredirect_entity}/edit",
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}


}
17 changes: 15 additions & 2 deletions src/Form/FragariaRedirectConfigEntityForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'),
Expand Down Expand Up @@ -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 : []);
Expand Down
56 changes: 39 additions & 17 deletions src/Routing/FragariaRedirectRoutingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
}
}
Expand Down

0 comments on commit 706a653

Please sign in to comment.