Skip to content

Commit

Permalink
Merge pull request #5 from esmero/ISSUE-4
Browse files Browse the repository at this point in the history
ISSUE-4: Bring back purls
  • Loading branch information
DiegoPino committed Nov 6, 2023
2 parents d735623 + d36a3b8 commit ffb8357
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 44 deletions.
14 changes: 14 additions & 0 deletions fragaria.install
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ function fragaria_install() {
\Drupal::entityDefinitionUpdateManager()->installEntityType($entity_type);
}


function fragaria_update_10001() {
$message = "Nothing to update. Fragaria Entity is not installed.";
$entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('fragariaredirect_entity');
if ($entity_type) {
$entity_config_export = $entity_type->get('config_export');
$entity_config_export[] = 'do_replacement';
$entity_type->set('config_export', $entity_config_export);
\Drupal::entityDefinitionUpdateManager()->updateEntityType($entity_type);
$message = "Fragaria Entity's Schema updated with do_replacement setting";
}
return $message;
}

/**
* Implements hook_uninstall().
*/
Expand Down
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 $key) {
$entity = $this->getFragariaEntityFromRouteMatch($this->routeMatch);
if ($entity) {
if ($key) {
$url = $key->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
51 changes: 36 additions & 15 deletions src/Entity/Controller/FragariaRedirectConfigEntityListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,47 @@ private function getDemoUrlForItem(FragariaRedirectConfigEntity $entity, string
private function getOneValuefromSearchAPI(FragariaRedirectConfigEntity $entity) {

/** @var \Drupal\search_api\IndexInterface[] $indexes */
$index = \Drupal::entityTypeManager()
->getStorage('search_api_index')
->load($entity->getSearchApiIndex());

$value = NULL;
if ($index) {
$query = $index->query();
if ($entity->isDoReplacement()) {
$uuid = NULL;
$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->range(0, 1);
$query->setOption('search_api_retrieved_field_values', [$entity->getSearchApiField() => $entity->getSearchApiField()]);
$query->addCondition($entity->getSearchApiField(), NULL, '<>');
$results = $query->execute();
foreach($results->getResultItems() as $itemid => $resultItem) {
foreach ($resultItem->getFields(FALSE) as $key => $field) {
if ($key == $entity->getSearchApiField()) {
$value = $field->getValues();
$node_ids = $query->accessCheck()->execute();
foreach (
\Drupal::entityTypeManager()->getStorage('node')->loadMultiple(
$node_ids
) as $node
) {
$uuid = $node->uuid();
}
return $uuid;
}
elseif ($entity->getSearchApiIndex()) {
$index = \Drupal::entityTypeManager()
->getStorage('search_api_index')
->load($entity->getSearchApiIndex());

$value = NULL;
if ($index) {
$query = $index->query();
$query->range(0, 1);
$query->setOption(
'search_api_retrieved_field_values',
[$entity->getSearchApiField() => $entity->getSearchApiField()]
);
$query->addCondition($entity->getSearchApiField(), NULL, '<>');
$results = $query->execute();
foreach ($results->getResultItems() as $itemid => $resultItem) {
foreach ($resultItem->getFields(FALSE) as $key => $field) {
if ($key == $entity->getSearchApiField()) {
$value = $field->getValues();
}
}
}
}
return is_array($value) ? reset($value) : $value;
}
return is_array($value) ? reset($value) : $value;
return NULL;
}

}
19 changes: 18 additions & 1 deletion 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 @@ -232,7 +241,7 @@ public function getPathSuffixes(): array {
/**
* @return string
*/
public function getSearchApiIndex(): string {
public function getSearchApiIndex(): string|null {
return $this->search_api_index;
}

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;
}


}
15 changes: 13 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.'),
'#Description' => $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->isDoReplacement() : FALSE,
],
'path_prefix' => [
'#type' => 'textfield',
'#title' => $this->t('The Prefix (that follows your domain) for the Redirect Route.'),
Expand Down Expand Up @@ -203,13 +210,17 @@ 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 = [];
$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
78 changes: 52 additions & 26 deletions src/Routing/FragariaRedirectRoutingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,37 +70,63 @@ public function redirect_routes(): RouteCollection {
if ($entity->isActive()) {
$prefix = $entity->getPathPrefix();
$prefix = trim(trim($prefix), '/');
$route = new Route(
'/' . $prefix . '/{key}',
[
'_controller' => 'Drupal\fragaria\Controller\Redirect::redirect_processor',
],
[
'_access' => 'TRUE',
]
);
$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 = new Route(
'/' . $prefix . '/{key}',
[
'_controller' => 'Drupal\fragaria\Controller\Redirect::redirect_do',
],
[
'_access' => 'TRUE',
]
);

$route->setRequirement('key', "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
$options['parameters']['key'] = ['type' => 'entity:node'];
$options['parameters']['resource_type'] = ['type' => 'ado'];
$route->setOptions($options);
$route->setDefault('fragariaredirect_entity', $entity->id());
$route_collection->add('fragaria_redirect.' . $entity->id(), $route);
}
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 = new Route(
'/' . $prefix . '/{key}',
[
'_controller' => 'Drupal\fragaria\Controller\Redirect::redirect_processor',
],
[
'_access' => 'TRUE',
]
);
$route->setDefault('fragariaredirect_entity', $entity->id());
$route_collection->add('fragaria_redirect.' . $entity->id(), $route);
}
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 ffb8357

Please sign in to comment.