Skip to content

Commit

Permalink
Merge pull request #920 from rosiel/infinite-derivs
Browse files Browse the repository at this point in the history
Avoid Infinite Derivatives.
  • Loading branch information
nigelgbanks authored Dec 14, 2022
2 parents f63dce6 + 665abfb commit db31d14
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/EventGenerator/EmitEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\islandora\Event\StompHeaderEvent;
use Drupal\islandora\Event\StompHeaderEventException;
use Drupal\islandora\Exception\IslandoraDerivativeException;
use Stomp\Exception\StompException;
use Stomp\StatefulStomp;
use Stomp\Transport\Message;
Expand Down Expand Up @@ -168,6 +169,10 @@ public function execute($entity = NULL) {
$event->getHeaders()->all()
);
}
catch (IslandoraDerivativeException $e) {
$this->logger->info($e->getMessage());
return;
}
catch (StompHeaderEventException $e) {
$this->logger->error($e->getMessage());
$this->messenger->addError($e->getMessage());
Expand Down
11 changes: 11 additions & 0 deletions src/Exception/IslandoraDerivativeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Drupal\islandora\Exception;

/**
* Islandora exceptions.
*
* @package islandora
*/
class IslandoraDerivativeException extends \RuntimeException {
}
8 changes: 8 additions & 0 deletions src/Plugin/Action/AbstractGenerateDerivative.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\islandora\Exception\IslandoraDerivativeException;

/**
* Emits a Node event.
Expand Down Expand Up @@ -60,6 +61,13 @@ protected function generateData(EntityInterface $entity) {
throw new \RuntimeException("Could not locate taxonomy term with uri: " . $this->configuration['derivative_term_uri'], 500);
}

// See if there is a destination media already set, and abort if it's the
// same as the source media. Dont cause an error, just don't continue.
$derivative_media = $this->utils->getMediaWithTerm($entity, $derivative_term);
if (!is_null($derivative_media) && $derivative_media->id() == $source_media->id()) {
throw new IslandoraDerivativeException("Halting derivative, as source and target media are the same. Derivative term: [" . $this->configuration['derivative_term_uri'] . "] Source term: [" . $this->configuration['source_term_uri'] . "] Node id: [" . $entity->id() . "].", 500);
}

$route_params = [
'node' => $entity->id(),
'media_type' => $this->configuration['destination_media_type'],
Expand Down

0 comments on commit db31d14

Please sign in to comment.