Browse Source

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

pull/1016/head 2.12.0
Aron Novak 8 months ago committed by GitHub
parent
commit
c80769580c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 27
      src/PresetReaction/PresetReaction.php

27
src/PresetReaction/PresetReaction.php

@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
@ -21,12 +22,20 @@ class PresetReaction extends ContextReactionPluginBase implements ContainerFacto
*/ */
protected $actionStorage; protected $actionStorage;
/**
* The logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/** /**
* {@inheritdoc} * {@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); parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->actionStorage = $action_storage; $this->actionStorage = $action_storage;
$this->logger = $logger;
} }
/** /**
@ -37,7 +46,8 @@ class PresetReaction extends ContextReactionPluginBase implements ContainerFacto
$configuration, $configuration,
$plugin_id, $plugin_id,
$plugin_definition, $plugin_definition,
$container->get('entity_type.manager')->getStorage('action') $container->get('entity_type.manager')->getStorage('action'),
$container->get('logger.factory')->get('islandora')
); );
} }
@ -56,8 +66,21 @@ class PresetReaction extends ContextReactionPluginBase implements ContainerFacto
$action_ids = $config['actions']; $action_ids = $config['actions'];
foreach ($action_ids as $action_id) { foreach ($action_ids as $action_id) {
$action = $this->actionStorage->load($action_id); $action = $this->actionStorage->load($action_id);
if (empty($action)) {
$this->logger->warning('Action "@action" not found.', ['@action' => $action_id]);
continue;
}
try {
$action->execute([$entity]); $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(),
]);
}
}
} }
/** /**

Loading…
Cancel
Save