From 2f59fa38df5840d541d31a28434215fcf7ce6ca9 Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Wed, 6 Mar 2024 11:13:24 -0800 Subject: [PATCH] Added unpublish util method for easier logging. --- modules/quant_search/quant_search.module | 3 +- .../src/Form/QuantSearchPageForm.php | 3 +- src/Seed.php | 31 ++++++++++--------- src/Utility.php | 30 ++++++++++++++++++ 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/modules/quant_search/quant_search.module b/modules/quant_search/quant_search.module index 35456252..2e4f23c0 100644 --- a/modules/quant_search/quant_search.module +++ b/modules/quant_search/quant_search.module @@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\node\Entity\Node; use Drupal\quant\Event\QuantEvent; +use Drupal\quant\Utility; use Drupal\quant_search\Controller\Search; /** @@ -99,5 +100,5 @@ function quant_search_run_index($nids, $languages, array &$context) { */ function quant_search_quant_search_page_delete(EntityInterface $entity) { $route = $entity->get('route'); - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $route, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($route, 'Unpublished search page'); } diff --git a/modules/quant_search/src/Form/QuantSearchPageForm.php b/modules/quant_search/src/Form/QuantSearchPageForm.php index 35c2ab6c..4d51c0c2 100644 --- a/modules/quant_search/src/Form/QuantSearchPageForm.php +++ b/modules/quant_search/src/Form/QuantSearchPageForm.php @@ -7,6 +7,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\quant\Event\QuantEvent; use Drupal\quant\Plugin\QueueItem\RouteItem; +use Drupal\quant\Utility; use Drupal\quant_search\Controller\Search; use Drupal\taxonomy\Entity\Vocabulary; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -531,7 +532,7 @@ public function save(array $form, FormStateInterface $form_state) { } // Only unpublish if page already exists, so was sent before. elseif ($status !== SAVED_NEW) { - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $route, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($route, 'Unpublished search page'); } $form_state->setRedirect('entity.quant_search_page.collection'); diff --git a/src/Seed.php b/src/Seed.php index b3083e77..4c3e67dd 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()) { - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $originalSource, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($originalSource, 'Unpublished redirect / => '); } } @@ -204,7 +204,7 @@ public static function deleteRedirect($redirect) { foreach ($redirects as $r) { // QuantEvent can be used to unpublish any resource. Note, the source must // be given here and not the destination. - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $r['source'], [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($r['source'], 'Unpublished redirect / => '); } } @@ -220,7 +220,8 @@ public static function seedTaxonomyTerm($entity, $langcode = NULL) { if (empty($response)) { // The markupFromRoute function works differently for unpublished terms // versus nodes. If the response is empty, the term is unpublished. - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($url, 'Unpublished taxonomy term page'); + return; } @@ -244,7 +245,7 @@ public static function seedTaxonomyTerm($entity, $langcode = NULL) { \Drupal::service('event_dispatcher')->dispatch(new QuantEvent($markup, $url, $meta, NULL, $entity, $langcode), QuantEvent::OUTPUT); } else { - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($url, 'Unpublished taxonomy term page'); } // Handle internal path redirects. @@ -349,7 +350,7 @@ public static function seedNode(EntityInterface $entity, $langcode = NULL) { \Drupal::service('event_dispatcher')->dispatch(new QuantEvent($markup, $url, $meta, $rid, $entity, $langcode), QuantEvent::OUTPUT); } else { - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($url, 'Unpublished content page'); } // Handle internal path redirects. @@ -371,13 +372,13 @@ public static function unpublishNode(EntityInterface $entity) { $site_config = \Drupal::config('system.site'); $front = $site_config->get('page.front'); if ((strpos($front, '/node/') === 0) && $nid == substr($front, 6)) { - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', '/', [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl('/', 'Unpublished home page'); } // Handle internal path redirects. self::handleInternalPathRedirects($entity, $langcode, $url); - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($url, 'Unpublished content page'); } /** @@ -395,7 +396,7 @@ public static function unpublishTaxonomyTerm(EntityInterface $entity) { // Handle internal path redirects. self::handleInternalPathRedirects($entity, $langcode, $url); - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($url, 'Unpublished taxonomy term page'); } /** @@ -408,7 +409,7 @@ public static function unpublishFile(EntityInterface $entity) { $url = $entity->createFileUrl(); - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($url, 'Unpublished file'); // If the file is an image, unpublish any image styles. $uri = $entity->getFileUri(); @@ -418,7 +419,7 @@ public static function unpublishFile(EntityInterface $entity) { // Don't check filesystem because files have been deleted by this point. $path = parse_url($style->buildUrl($uri))['path']; - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $path, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($path, 'Unpublished image style'); } } } @@ -508,13 +509,13 @@ public static function unpublishView(EntityInterface $entity) { if ($i === 0) { $url = "{$prefix}/{$path}"; - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($url, 'Unpublished views page'); } // Handle the pager path. $pager_url = "{$prefix}/{$path}?page={$i}"; - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $pager_url, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($pager_url, "Unpublished views pager page [$i]"); } } } @@ -527,7 +528,7 @@ public static function unpublishPathAlias($pathAlias) { $alias = Utility::getUrl($pathAlias->get('alias')->value, $pathAlias->get('langcode')->value); - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $alias, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($alias, 'Unpublished path alias'); } /** @@ -580,11 +581,11 @@ public static function handleInternalPathRedirects($entity, $langcode, $url) { // Unpublish redirects. if (!$defaultPublished) { - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $internalPath, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($internalPath, 'Unpublished internal path'); } if (!$published && $usesPrefixes) { // Handle redirects with path prefix too. - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', "/{$langcode}{$internalPath}", [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl("/{$langcode}{$internalPath}", 'Unpublished internal path'); } } diff --git a/src/Utility.php b/src/Utility.php index b4895ed6..5d6d4750 100644 --- a/src/Utility.php +++ b/src/Utility.php @@ -266,4 +266,34 @@ public static function getPageInfo(array $urls = NULL) : string { } + /** + * Unpublish the given URL and optionally log a message. + * + * @param string $url + * The URL to unpublish. + * @param string $message + * The message to log. + * @param bool $log + * Whether or not to log the message. + */ + public static function unpublishUrl(string $url, string $message = '', bool $log = TRUE) : void { + if (!trim($url)) { + \Drupal::logger('quant')->notice('Cannot unpublished empty url.'); + + return; + } + + $response = \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); + + // Only log if it's not a 404. + if ($log && $response) { + if (empty($message)) { + $message = 'Unpublished url'; + } + $message .= ' @url'; + + \Drupal::logger('quant')->notice($message, ['@url' => $url]); + } + } + }