Skip to content

Commit

Permalink
Do not have a fatal error on a missing action (#1014)
Browse files Browse the repository at this point in the history
  • Loading branch information
AronNovak authored Apr 24, 2024
1 parent 54206de commit c807695
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/PresetReaction/PresetReaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand All @@ -21,12 +22,20 @@ class PresetReaction extends ContextReactionPluginBase implements ContainerFacto
*/
protected $actionStorage;

/**
* The logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;

/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $action_storage) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $action_storage, LoggerInterface $logger) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->actionStorage = $action_storage;
$this->logger = $logger;
}

/**
Expand All @@ -37,7 +46,8 @@ public static function create(ContainerInterface $container, array $configuratio
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')->getStorage('action')
$container->get('entity_type.manager')->getStorage('action'),
$container->get('logger.factory')->get('islandora')
);
}

Expand All @@ -56,7 +66,20 @@ public function execute(EntityInterface $entity = NULL) {
$action_ids = $config['actions'];
foreach ($action_ids as $action_id) {
$action = $this->actionStorage->load($action_id);
$action->execute([$entity]);
if (empty($action)) {
$this->logger->warning('Action "@action" not found.', ['@action' => $action_id]);
continue;
}
try {
$action->execute([$entity]);
}
catch (\Exception $e) {
$this->logger->error('Error executing action "@action" on entity "@entity": @message', [
'@action' => $action->label(),
'@entity' => $entity->label(),
'@message' => $e->getMessage(),
]);
}
}
}

Expand Down

0 comments on commit c807695

Please sign in to comment.