Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not have a fatal error on a missing action #1014

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading