Skip to content

Commit

Permalink
WIP media deletion handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
kepol committed Feb 28, 2024
1 parent 445fe2f commit 7537b25
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 19 deletions.
4 changes: 2 additions & 2 deletions modules/quant_search/quant_search.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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'));
}
3 changes: 2 additions & 1 deletion modules/quant_search/src/Form/QuantSearchPageForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
12 changes: 8 additions & 4 deletions quant.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -296,10 +304,6 @@ function _quant_entity_delete_op(EntityInterface $entity) {
case 'taxonomy_term':
Seed::unpublishTaxonomyTerm($entity);
break;

case 'file':
Seed::unpublishFile($entity);
break;
}

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

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

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

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

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 7537b25

Please sign in to comment.