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; } 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 ec1b793d..1686c57d 100644 --- a/src/Seed.php +++ b/src/Seed.php @@ -249,12 +249,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); } } @@ -328,12 +326,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); } @@ -442,8 +438,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); } @@ -502,8 +497,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 3124123f..dc1fa77c 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. *