Skip to content

Commit

Permalink
Added unpublish util method for easier logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
kepol committed Mar 6, 2024
1 parent a6342f1 commit 2f59fa3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
3 changes: 2 additions & 1 deletion modules/quant_search/quant_search.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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');
}
3 changes: 2 additions & 1 deletion modules/quant_search/src/Form/QuantSearchPageForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
31 changes: 16 additions & 15 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()) {
\Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $originalSource, [], NULL), QuantEvent::UNPUBLISH);
Utility::unpublishUrl($originalSource, 'Unpublished redirect / => ');
}
}

Expand Down Expand Up @@ -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 / => ');
}
}

Expand All @@ -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;
}

Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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');
}

/**
Expand All @@ -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');
}

/**
Expand All @@ -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();
Expand All @@ -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');
}
}
}
Expand Down Expand Up @@ -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]");
}
}
}
Expand All @@ -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');
}

/**
Expand Down Expand Up @@ -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');
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

}

0 comments on commit 2f59fa3

Please sign in to comment.