Skip to content

Commit

Permalink
Refactor, cleanup and handle deleted translations.
Browse files Browse the repository at this point in the history
  • Loading branch information
kepol committed Mar 7, 2024
1 parent 9f71943 commit 56e3089
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
4 changes: 4 additions & 0 deletions modules/quant_api/src/EventSubscriber/QuantApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public function onRedirect(QuantRedirectEvent $event) {
$dest = $event->getDestinationUrl();
$statusCode = $event->getStatusCode();

if ($source == $dest) {
return;
}

$data = [
'url' => $source,
'redirect_url' => $dest,
Expand Down
55 changes: 26 additions & 29 deletions quant.module
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,19 @@ function quant_entity_predelete(EntityInterface $entity) {
// This needs to not be a shutdown hook and also needs to be on
// the predelete hook as the path alias is removed in the entity
// delete clean-up.
_quant_entity_delete_op($entity);
_quant_unpublish_entity($entity);

// Handle any translations.
if (method_exists($entity, 'isTranslatable') && $entity->isTranslatable()) {
foreach ($entity->getTranslationLanguages() as $language) {
if ($language->getId() != $entity->language()->getId()) {
$translation = $entity->getTranslation($language->getId());

_quant_unpublish_entity($translation);
}
}
}

}

/**
Expand Down Expand Up @@ -231,20 +243,6 @@ function quant_redirect_presave($redirect) {
Seed::seedRedirect($redirect);
}

/**
* Implements hook_redirect_delete().
*/
function quant_redirect_delete($redirect) {
$quant_enabled = \Drupal::config('quant.settings')->get('quant_enabled');
$quant_redirect_enabled = \Drupal::config('quant.settings')->get('quant_enabled_redirects');

if (!$quant_enabled || !$quant_redirect_enabled) {
return;
}

Seed::deleteRedirect($redirect);
}

/**
* Entity update operation hook.
*
Expand Down Expand Up @@ -273,17 +271,17 @@ function _quant_entity_update_op(EntityInterface $entity) {
}

/**
* Entity delete operation hook.
* Unpublish entity.
*
* Used to trigger an unpublish from the Quant API.
*
* @param Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* Used to trigger an unpublish from the Quant API.
*
* @todo Entity support.
*/
function _quant_entity_delete_op(EntityInterface $entity) {
function _quant_unpublish_entity(EntityInterface $entity) {

// @todo Try to handle entities more generically.
// @todo Check tracking is enabled for entity first.
switch ($entity->getEntityTypeId()) {
case 'node':
Seed::unpublishNode($entity);
Expand All @@ -308,22 +306,21 @@ function _quant_entity_delete_op(EntityInterface $entity) {
case 'view':
Seed::unpublishView($entity);
break;

case 'redirect':
Seed::unpublishRedirect($entity);
break;
}

}

/**
* Implements hook_ENTITY_TYPE_translation_delete().
* Implements hook_entity_translation_delete().
*/
function quant_node_translation_delete($entity) {
$quant_enabled = \Drupal::config('quant.settings')->get('quant_enabled');
$quant_node_enabled = \Drupal::config('quant.settings')->get('quant_enabled_nodes');
function quant_entity_translation_delete($entity) {

if (!$quant_enabled || !$quant_node_enabled) {
return;
}
_quant_unpublish_entity($entity);

Seed::unpublishNode($entity);
}

/**
Expand Down
18 changes: 12 additions & 6 deletions src/Seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static function seedRedirect($redirect) {
if (!$redirect->isNew()) {
$originalSource = $redirect->original->getSourcePathWithQuery();
if ($originalSource && $originalSource != $redirect->getSourcePathWithQuery()) {
Utility::unpublishUrl($originalSource, 'Unpublished redirect / => ');
Utility::unpublishUrl($originalSource, 'Unpublished redirect');
}
}

Expand Down Expand Up @@ -197,14 +197,14 @@ public static function getRedirectLocationsFromRedirect($redirect) {
}

/**
* Delete existing redirects via API request.
* Unpublish existing redirects via API request.
*/
public static function deleteRedirect($redirect) {
public static function unpublishRedirect($redirect) {
$redirects = self::getRedirectLocationsFromRedirect($redirect);
foreach ($redirects as $r) {
// QuantEvent can be used to unpublish any resource. Note, the source must
// be given here and not the destination.
Utility::unpublishUrl($r['source'], 'Unpublished redirect / => ');
Utility::unpublishUrl($r['source'], 'Unpublished redirect');
}
}

Expand Down Expand Up @@ -515,7 +515,7 @@ public static function unpublishView(EntityInterface $entity) {
// Handle the pager path.
$pager_url = "{$prefix}/{$path}?page={$i}";

Utility::unpublishUrl($pager_url, 'Unpublished views pager page');
Utility::unpublishUrl($pager_url, 'Unpublished views page');
}
}
}
Expand All @@ -526,7 +526,13 @@ public static function unpublishView(EntityInterface $entity) {
*/
public static function unpublishPathAlias($pathAlias) {

$alias = Utility::getUrl($pathAlias->get('alias')->value, $pathAlias->get('langcode')->value);
$langcode = $pathAlias->get('langcode')->value;
$alias = Utility::getUrl($pathAlias->get('alias')->value, $langcode);

// Strip the 'und'.
if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
$alias = str_replace("/{$langcode}", '', $alias);
}

Utility::unpublishUrl($alias, 'Unpublished path alias');
}
Expand Down

0 comments on commit 56e3089

Please sign in to comment.