Skip to content

Commit

Permalink
This works. Can even live with Aliases at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoPino committed Nov 3, 2023
1 parent 706a653 commit d36a3b8
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 46 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
6 changes: 3 additions & 3 deletions src/Controller/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ public function redirect_processor(Request $request, $key) {
* @return \Symfony\Component\HttpFoundation\Response
* A simple string and Redirect response.
*/
public function redirect_do(Request $request, ContentEntityInterface $node) {
public function redirect_do(Request $request, ContentEntityInterface $key) {
$entity = $this->getFragariaEntityFromRouteMatch($this->routeMatch);
if ($entity) {
if ($node) {
$url = $node->toUrl('canonical', ['absolute' => FALSE])->toString();
if ($key) {
$url = $key->toUrl('canonical', ['absolute' => FALSE])->toString();
$response = new RedirectResponse($url, (int) $entity->getRedirectHttpCode());
return $response;
}
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;
}

}
2 changes: 1 addition & 1 deletion src/Entity/FragariaRedirectConfigEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,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
8 changes: 3 additions & 5 deletions src/Form/FragariaRedirectConfigEntityForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ public function form(array $form, FormStateInterface $form_state) {
'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'),
'#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->getPathPrefix() : NULL,
'#default_value' => (!$fragariaredirect_config->isNew()) ? $fragariaredirect_config->isDoReplacement() : FALSE,
],
'path_prefix' => [
'#type' => 'textfield',
Expand Down Expand Up @@ -215,14 +215,12 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$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->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
48 changes: 26 additions & 22 deletions src/Routing/FragariaRedirectRoutingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,36 @@ 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());
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}");
$route->setOptions(
["parameters" => [
"key" => [
"type" => 'entity:node'],
["resource_type" =>
["type" => 'ado']
]
]]);
$route->setOption(
'_controller',
'Drupal\fragaria\Controller\Redirect::redirect_do'
$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 {
$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;
Expand Down

0 comments on commit d36a3b8

Please sign in to comment.