From c0a2efbcccf3102432f001884552cf2bdde7a706 Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Sat, 6 Jan 2024 20:31:30 -0800 Subject: [PATCH 1/4] Workaround disabled drafts publishing issue. --- src/EventSubscriber/CollectionSubscriber.php | 21 ++++++++++---------- src/Form/ConfigForm.php | 3 ++- src/Seed.php | 21 +++++--------------- src/TokenManager.php | 2 +- src/Utility.php | 15 ++++++++++++++ 5 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/EventSubscriber/CollectionSubscriber.php b/src/EventSubscriber/CollectionSubscriber.php index 67f73f15..241d01f1 100644 --- a/src/EventSubscriber/CollectionSubscriber.php +++ b/src/EventSubscriber/CollectionSubscriber.php @@ -2,22 +2,23 @@ namespace Drupal\quant\EventSubscriber; +use Drupal\Core\Config\ConfigFactory; +use Drupal\Core\Entity\EntityTypeManager; +use Drupal\Core\Url; +use Drupal\node\Entity\Node; use Drupal\quant\Event\CollectEntitiesEvent; use Drupal\quant\Event\CollectFilesEvent; use Drupal\quant\Event\CollectRedirectsEvent; use Drupal\quant\Event\CollectRoutesEvent; use Drupal\quant\Event\QuantCollectionEvents; use Drupal\quant\Plugin\QueueItem\RedirectItem; +use Drupal\quant\QuantQueueFactory; +use Drupal\quant\Seed; +use Drupal\quant\Utility; +use Drupal\redirect\Entity\Redirect; use Drupal\user\Entity\User; use Drupal\views\Views; use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Drupal\Core\Config\ConfigFactory; -use Drupal\Core\Entity\EntityTypeManager; -use Drupal\Core\Url; -use Drupal\node\Entity\Node; -use Drupal\quant\Seed; -use Drupal\redirect\Entity\Redirect; -use Drupal\quant\QuantQueueFactory; /** * Event subscribers for the quant collection events. @@ -65,8 +66,8 @@ public static function getSubscribedEvents() { */ public function collectEntities(CollectEntitiesEvent $event) { $query = $this->entityTypeManager->getStorage('node')->getQuery(); - // @todo Skip unpublished content if disable_content_drafts is enabled. - $disable_drafts = $this->configFactory->get('quant.settings')->get('disable_content_drafts'); + // @todo Skip unpublished content if configured. + $disable_drafts = !Utility::processDrafts(); $bundles = $event->getFormState()->getValue('entity_node_bundles'); @@ -226,7 +227,7 @@ public function collectFiles(CollectFilesEvent $event) { */ public function collectRoutes(CollectRoutesEvent $event) { // Handle unpublished content based on settings. - $disable_drafts = $this->configFactory->get('quant.settings')->get('disable_content_drafts'); + $disable_drafts = !Utility::processDrafts(); // Collect the site configured routes. $system = $this->configFactory->get('system.site'); diff --git a/src/Form/ConfigForm.php b/src/Form/ConfigForm.php index 17838801..a9d3b76d 100644 --- a/src/Form/ConfigForm.php +++ b/src/Form/ConfigForm.php @@ -50,6 +50,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $this->checkValidationRoute(); + // @todo Rename this key to something more clear. $form['quant_enabled'] = [ '#type' => 'checkbox', '#title' => $this->t('Track content change'), @@ -107,7 +108,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['disable_content_drafts'] = [ '#type' => 'checkbox', '#title' => $this->t('Disable content drafts'), - '#description' => $this->t('Prevent draft content from being sent to Quant.'), + '#description' => $this->t('Prevent draft content from being sent to Quant. Drafts will always been processed when content tracking is disabled to prevent issues with unpublished content.'), '#default_value' => $config->get('disable_content_drafts'), ]; diff --git a/src/Seed.php b/src/Seed.php index 229444ff..7323c77f 100644 --- a/src/Seed.php +++ b/src/Seed.php @@ -8,6 +8,7 @@ use Drupal\node\Entity\Node; use Drupal\quant\Event\QuantEvent; use Drupal\quant\Event\QuantRedirectEvent; +use Drupal\quant\Utility; use GuzzleHttp\Exception\ConnectException; /** @@ -249,13 +250,8 @@ public static function seedTaxonomyTerm($entity, $langcode = NULL) { \Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent("/taxonomy/term/{$tid}", $defaultUrl, 301), QuantRedirectEvent::UPDATE); } - // Handle case where translation is unpublished. + // Unpublish if necessary. $published = $entity->isPublished(); - if ($entity->hasTranslation($langcode)) { - $translation = $entity->getTranslation($langcode); - $published = $translation->isPublished(); - } - if ($published) { \Drupal::service('event_dispatcher')->dispatch(new QuantEvent($markup, $url, $meta, NULL, $entity, $langcode), QuantEvent::OUTPUT); } @@ -333,13 +329,8 @@ public static function seedNode(EntityInterface $entity, $langcode = NULL) { } } - // Handle case where translation is unpublished. + // Unpublish if necessary. $published = $entity->isPublished(); - if ($entity->hasTranslation($langcode)) { - $translation = $entity->getTranslation($langcode); - $published = $translation->isPublished(); - } - if ($published) { \Drupal::service('event_dispatcher')->dispatch(new QuantEvent($markup, $url, $meta, $rid, $entity, $langcode), QuantEvent::OUTPUT); } @@ -452,8 +443,7 @@ public static function headRoute($route, array $headers = []) { // Generate a signed token and use it in the request. This only applies when // drafts are enabled, as we return neutral access otherwise. - $disable_drafts = $config->get('disable_content_drafts'); - if (!$disable_drafts) { + if (Utility::processDrafts()) { $headers['quant-token'] = \Drupal::service('quant.token_manager')->create($route); } @@ -512,8 +502,7 @@ public static function markupFromRoute($route, array $headers = []) { // Generate a signed token and use it in the request. This only applies when // drafts are enabled, as we return neutral access otherwise. - $disable_drafts = $config->get('disable_content_drafts'); - if (!$disable_drafts) { + if (Utility::processDrafts()) { $headers['quant-token'] = \Drupal::service('quant.token_manager')->create($route); } diff --git a/src/TokenManager.php b/src/TokenManager.php index 13ee25a3..1340475e 100644 --- a/src/TokenManager.php +++ b/src/TokenManager.php @@ -156,7 +156,7 @@ public function validate($route = NULL, $strict = TRUE) { throw new TokenValidationDisabledException(); } - if ($this->quantSettings->get('disable_content_drafts')) { + if (!Utility::processDrafts()) { // When content drafts are disabled the token is irrelevant. It may not // even be included in the internal HTTP request. Bypass validation // altogether, as the token is only required for draft access. diff --git a/src/Utility.php b/src/Utility.php index 19ebae44..7181f2ae 100644 --- a/src/Utility.php +++ b/src/Utility.php @@ -117,6 +117,21 @@ public static function inList($item, array $list) { return $found; } + /** + * Determine if drafts should be processed. + * + * @return bool + * TRUE if drafts should be processed and FALSE otherwise. + */ + public static function processDrafts() { + $config = \Drupal::config('quant.settings'); + $disable_drafts = $config->get('disable_content_drafts'); + $track_content = $config->get('quant_enabled'); + // Drafts must always be processed when content tracking is disabled in + // case content is unpublished or it may stay published in Quant. + return !$track_content || !$disable_drafts; + } + /** * Get Quant page info for the given URLs. * From 8e50dd27467ef0324517a31f3487da4a5cc360bc Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Sun, 7 Jan 2024 19:31:09 -0800 Subject: [PATCH 2/4] Updated unpublish seed logic for new content. --- src/Seed.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Seed.php b/src/Seed.php index 7323c77f..2960ada3 100644 --- a/src/Seed.php +++ b/src/Seed.php @@ -250,12 +250,10 @@ public static function seedTaxonomyTerm($entity, $langcode = NULL) { \Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent("/taxonomy/term/{$tid}", $defaultUrl, 301), QuantRedirectEvent::UPDATE); } - // Unpublish if necessary. + // Always add the content in case its new and then unpublish if necessary. $published = $entity->isPublished(); - if ($published) { - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent($markup, $url, $meta, NULL, $entity, $langcode), QuantEvent::OUTPUT); - } - else { + \Drupal::service('event_dispatcher')->dispatch(new QuantEvent($markup, $url, $meta, NULL, $entity, $langcode), QuantEvent::OUTPUT); + if (!$published) { \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); } } @@ -329,12 +327,10 @@ public static function seedNode(EntityInterface $entity, $langcode = NULL) { } } - // Unpublish if necessary. + // Always add the content in case its new and then unpublish if necessary. $published = $entity->isPublished(); + \Drupal::service('event_dispatcher')->dispatch(new QuantEvent($markup, $url, $meta, $rid, $entity, $langcode), QuantEvent::OUTPUT); if ($published) { - \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); } From 6765a868dca665c82a5cccfbf786b3c60b2e4fb5 Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Sun, 7 Jan 2024 19:32:36 -0800 Subject: [PATCH 3/4] Updated seed logging with more information. --- modules/quant_api/src/EventSubscriber/QuantApi.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/quant_api/src/EventSubscriber/QuantApi.php b/modules/quant_api/src/EventSubscriber/QuantApi.php index 9a0c285f..27c46f98 100644 --- a/modules/quant_api/src/EventSubscriber/QuantApi.php +++ b/modules/quant_api/src/EventSubscriber/QuantApi.php @@ -94,7 +94,7 @@ public function onRedirect(QuantRedirectEvent $event) { $res = $this->client->sendRedirect($data); } catch (\Exception $error) { - $this->logger->error($error->getMessage()); + $this->logger->error('Quant onRedirect: ' . $error->getMessage()); return; } @@ -143,7 +143,7 @@ public function onOutput(QuantEvent $event) { $res = $this->client->send($data); } catch (\Exception $error) { - $this->logger->error($error->getMessage()); + $this->logger->error('Quant onOutput: ' . $error->getMessage()); return FALSE; } @@ -297,7 +297,7 @@ public function onMedia(QuantFileEvent $event) { } catch (\Exception $error) { if (strpos("MD5 already matches", $error->getMessage()) !== FALSE) { - $this->logger->error($error->getMessage()); + $this->logger->error('Quant onMedia: ' . $error->getMessage()); } return; } @@ -315,8 +315,8 @@ public function onUnpublish(QuantEvent $event) { } catch (\Exception $error) { // Don't log it if it's a 404, since the content is unpublished. - if (strpos($error->getMessage(), '404 Not Found') === FALSE) { - $this->logger->error($error->getMessage()); + if (!str_contains($error->getMessage(), '404 Not Found') && !str_contains($error->getMessage(), 'Resource is already unpublished')) { + $this->logger->error('Quant onUnpublish: ' . $error->getMessage()); } return; } From 9f42925b1016a62eefb7b05d3fe4118d0abc1ebb Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Sun, 7 Jan 2024 19:49:51 -0800 Subject: [PATCH 4/4] Fixed linting. --- src/Seed.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Seed.php b/src/Seed.php index 2960ada3..1686c57d 100644 --- a/src/Seed.php +++ b/src/Seed.php @@ -8,7 +8,6 @@ use Drupal\node\Entity\Node; use Drupal\quant\Event\QuantEvent; use Drupal\quant\Event\QuantRedirectEvent; -use Drupal\quant\Utility; use GuzzleHttp\Exception\ConnectException; /**