Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround disabled drafts publishing issue. #211

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions modules/quant_api/src/EventSubscriber/QuantApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
21 changes: 11 additions & 10 deletions src/EventSubscriber/CollectionSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion src/Form/ConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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. <strong>Drafts will always been processed when content tracking is disabled to prevent issues with unpublished content.</strong>'),
'#default_value' => $config->get('disable_content_drafts'),
];

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading