diff --git a/modules/quant_api/src/EventSubscriber/QuantApi.php b/modules/quant_api/src/EventSubscriber/QuantApi.php index 750fd424..123de22d 100644 --- a/modules/quant_api/src/EventSubscriber/QuantApi.php +++ b/modules/quant_api/src/EventSubscriber/QuantApi.php @@ -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, diff --git a/quant.module b/quant.module index 49457f22..f9902e49 100644 --- a/quant.module +++ b/quant.module @@ -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); + } + } + } + } /** @@ -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. * @@ -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); @@ -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); } /** diff --git a/src/Seed.php b/src/Seed.php index aa0b3054..f10a6c53 100644 --- a/src/Seed.php +++ b/src/Seed.php @@ -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'); } } @@ -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'); } } @@ -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'); } } } @@ -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'); }