From 7537b258fa392b9997466e3eac064ba133342fb5 Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Tue, 27 Feb 2024 19:30:36 -0800 Subject: [PATCH] WIP media deletion handling. --- modules/quant_search/quant_search.module | 4 +- .../src/Form/QuantSearchPageForm.php | 3 +- quant.module | 12 ++-- src/Seed.php | 69 +++++++++++++++---- src/Utility.php | 10 +++ 5 files changed, 79 insertions(+), 19 deletions(-) diff --git a/modules/quant_search/quant_search.module b/modules/quant_search/quant_search.module index 35456252..244bcbf5 100644 --- a/modules/quant_search/quant_search.module +++ b/modules/quant_search/quant_search.module @@ -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; /** @@ -98,6 +99,5 @@ function quant_search_run_index($nids, $languages, array &$context) { * Implements hook_ENTITY_TYPE_delete(). */ 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($entity->get('route')); } diff --git a/modules/quant_search/src/Form/QuantSearchPageForm.php b/modules/quant_search/src/Form/QuantSearchPageForm.php index f4dc1ce7..38178dde 100644 --- a/modules/quant_search/src/Form/QuantSearchPageForm.php +++ b/modules/quant_search/src/Form/QuantSearchPageForm.php @@ -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; @@ -524,7 +525,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); } $form_state->setRedirect('entity.quant_search_page.collection'); diff --git a/quant.module b/quant.module index 04612dba..e285296b 100644 --- a/quant.module +++ b/quant.module @@ -285,6 +285,14 @@ function _quant_entity_update_op(EntityInterface $entity) { function _quant_entity_delete_op(EntityInterface $entity) { switch ($entity->getEntityTypeId()) { + case 'file': + Seed::unpublishFile($entity); + break; + + case 'media': + Seed::unpublishMedia($entity); + break; + case 'node': Seed::unpublishNode($entity); break; @@ -296,10 +304,6 @@ function _quant_entity_delete_op(EntityInterface $entity) { case 'taxonomy_term': Seed::unpublishTaxonomyTerm($entity); break; - - case 'file': - Seed::unpublishFile($entity); - break; } } diff --git a/src/Seed.php b/src/Seed.php index 6ea13498..203d20ac 100644 --- a/src/Seed.php +++ b/src/Seed.php @@ -96,7 +96,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); } } @@ -202,7 +202,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']); } } @@ -218,7 +218,7 @@ 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); return; } @@ -242,7 +242,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); } // Handle internal path redirects. @@ -347,7 +347,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); } // Handle internal path redirects. @@ -369,13 +369,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('/'); } // Handle internal path redirects. self::handleInternalPathRedirects($entity, $langcode, $url); - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($url); } /** @@ -393,7 +393,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); } /** @@ -403,20 +403,65 @@ public static function unpublishTaxonomyTerm(EntityInterface $entity) { * The file entity. */ public static function unpublishFile(EntityInterface $entity) { + Utility::unpublishUrl($entity->createFileUrl()); + } + + /** + * Unpublish the media from Quant. + * + * @param Drupal\Core\Entity\EntityInterface $entity + * The file entity. + */ + public static function unpublishMedia(EntityInterface $entity) { + // @fixme $url = $entity->createFileUrl(); - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); + Utility::unpublishUrl($entity->createFileUrl()); + + // Get the file for the media type. + $type = $entity->bundle(); + + switch ($type) { + case 'audio': + $field_name = 'field_media_audio_file'; + break; + + case 'document': + $field_name = 'field_media_document'; + break; + + case 'image': + $field_name = 'field_media_image'; + break; + + case 'video': + $field_name = 'field_media_video_file'; + break; + + default: + return; + } + + // Check data exists. + if (!$media->hasField($field_name) || $media->get($field_name)->isEmpty()) { + return; + } + + // Get the file entity for the media. + $file = $media->get($field_name)->entity; + if ($file) { + Utility::unpublishUrl($file->createFileUrl()); + } + } /** * Unpublish path alias via API request. */ 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); } /** diff --git a/src/Utility.php b/src/Utility.php index b4895ed6..f36f112c 100644 --- a/src/Utility.php +++ b/src/Utility.php @@ -138,6 +138,16 @@ public static function isExternalUrl(string $url) : bool { return FALSE; } + /** + * Unpublish URL in Quant. + * + * @param string $url + * The URL. + */ + public static function unpublishUrl(string $url) { + \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); + } + /** * Checks if an item is in a list which may use regular expressions. *