Skip to content

Commit

Permalink
Merge pull request #236 from quantcdn/feature/media-unpublish
Browse files Browse the repository at this point in the history
Improved handling of deleted entities and unpublishing.
  • Loading branch information
kepol authored Jan 29, 2025
2 parents 8a68482 + 2c1c0a5 commit 89d1088
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 47 deletions.
4 changes: 4 additions & 0 deletions modules/quant_api/src/EventSubscriber/QuantApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public function onRedirect(QuantRedirectEvent $event) {
$dest = $event->getDestinationUrl();
$statusCode = $event->getStatusCode();

if ($source == $dest) {
return;
}

$data = [
'url' => $source,
'redirect_url' => $dest,
Expand Down
4 changes: 2 additions & 2 deletions modules/quant_search/quant_search.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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 +99,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');
}
4 changes: 2 additions & 2 deletions modules/quant_search/src/Form/QuantSearchPageForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeManagerInterface;
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 +531,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
62 changes: 36 additions & 26 deletions quant.module
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,19 @@ function quant_entity_predelete(EntityInterface $entity) {
// This needs to not be a shutdown hook and also needs to be on
// the predelete hook as the path alias is removed in the entity
// delete clean-up.
_quant_entity_delete_op($entity);
_quant_unpublish_entity($entity);

// Handle any translations.
if (method_exists($entity, 'isTranslatable') && $entity->isTranslatable()) {
foreach ($entity->getTranslationLanguages() as $language) {
if ($language->getId() != $entity->language()->getId()) {
$translation = $entity->getTranslation($language->getId());

_quant_unpublish_entity($translation);
}
}
}

}

/**
Expand Down Expand Up @@ -231,20 +243,6 @@ function quant_redirect_presave($redirect) {
Seed::seedRedirect($redirect);
}

/**
* Implements hook_redirect_delete().
*/
function quant_redirect_delete($redirect) {
$quant_enabled = \Drupal::config('quant.settings')->get('quant_enabled');
$quant_redirect_enabled = \Drupal::config('quant.settings')->get('quant_enabled_redirects');

if (!$quant_enabled || !$quant_redirect_enabled) {
return;
}

Seed::deleteRedirect($redirect);
}

/**
* Entity update operation hook.
*
Expand Down Expand Up @@ -273,17 +271,17 @@ function _quant_entity_update_op(EntityInterface $entity) {
}

/**
* Entity delete operation hook.
* Unpublish entity.
*
* Used to trigger an unpublish from the Quant API.
*
* @param Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* Used to trigger an unpublish from the Quant API.
*
* @todo Entity support.
*/
function _quant_entity_delete_op(EntityInterface $entity) {
function _quant_unpublish_entity(EntityInterface $entity) {

// @todo Try to handle entities more generically.
// @todo Check tracking is enabled for entity first.
switch ($entity->getEntityTypeId()) {
case 'node':
Seed::unpublishNode($entity);
Expand All @@ -300,22 +298,34 @@ function _quant_entity_delete_op(EntityInterface $entity) {
case 'file':
Seed::unpublishFile($entity);
break;

case 'media':
Seed::unpublishMedia($entity);
break;

case 'view':
Seed::unpublishView($entity);
break;

case 'redirect':
Seed::unpublishRedirect($entity);
break;
}

}

/**
* Implements hook_ENTITY_TYPE_translation_delete().
* Implements hook_entity_translation_delete().
*/
function quant_node_translation_delete($entity) {
function quant_entity_translation_delete($entity) {
$quant_enabled = \Drupal::config('quant.settings')->get('quant_enabled');
$quant_node_enabled = \Drupal::config('quant.settings')->get('quant_enabled_nodes');

if (!$quant_enabled || !$quant_node_enabled) {
if (!$quant_enabled) {
return;
}

Seed::unpublishNode($entity);
_quant_unpublish_entity($entity);

}

/**
Expand Down
Loading

0 comments on commit 89d1088

Please sign in to comment.