diff --git a/src/Entity/Controller/JobItemAccessControlHandler.php b/src/Entity/Controller/JobItemAccessControlHandler.php index f6a5d84..4fec098 100644 --- a/src/Entity/Controller/JobItemAccessControlHandler.php +++ b/src/Entity/Controller/JobItemAccessControlHandler.php @@ -25,6 +25,9 @@ class JobItemAccessControlHandler extends JobAccessControlHandler { if ($operation === 'delete') { return AccessResult::allowedIf($entity->isInactive()); } + else if ($operation == 'abort') { + return AccessResult::allowedIf($entity->isActive() || $entity->isNeedsReview()); + } else if ($entity->getJob()) { return $entity->getJob()->access($operation, $account, TRUE); } diff --git a/src/Form/JobItemAbortForm.php b/src/Form/JobItemAbortForm.php index cee5e5a..53c543e 100644 --- a/src/Form/JobItemAbortForm.php +++ b/src/Form/JobItemAbortForm.php @@ -9,6 +9,7 @@ namespace Drupal\tmgmt\Form; use Drupal\Core\Entity\ContentEntityConfirmFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\tmgmt\TMGMTException; /** * Provides a form for deleting a node. @@ -19,7 +20,7 @@ class JobItemAbortForm extends ContentEntityConfirmFormBase { * {@inheritdoc} */ public function getQuestion() { - return $this->t('Abort this job item?', array('%title' => $this->entity->label())); + return $this->t('Are you sure you want to abort the job item %title?', array('%title' => $this->entity->label())); } /** @@ -42,19 +43,26 @@ class JobItemAbortForm extends ContentEntityConfirmFormBase { public function submitForm(array &$form, FormStateInterface $form_state) { /** @var \Drupal\tmgmt\Entity\JobItem $entity */ $entity = $this->entity; - if (!$entity->abortTranslation()) { - foreach ($entity->getMessagesSince() as $message) { - /** @var \Drupal\tmgmt\MessageInterface $message */ - if ($message->getType() == 'debug') { - continue; - } - if ($text = $message->getMessage()) { - // We want to persist also the type therefore we will set the - // messages directly and not return them. - drupal_set_message($text, $message->getType()); + try { + if (!$entity->abortTranslation()) { + foreach ($entity->getMessagesSince() as $message) { + /** @var \Drupal\tmgmt\MessageInterface $message */ + if ($message->getType() == 'debug') { + continue; + } + if ($text = $message->getMessage()) { + // We want to persist also the type therefore we will set the + // messages directly and not return them. + drupal_set_message($text, $message->getType()); + } } } } + catch(TMGMTException $e) { + drupal_set_message(t('Job item cannot be aborted: %error.', array( + '%error' => $e->getMessage(), + )), 'error'); + } $form_state->setRedirectUrl($this->entity->toUrl()); } diff --git a/src/Tests/TMGMTUiTest.php b/src/Tests/TMGMTUiTest.php index 1ae32bb..c27c071 100644 --- a/src/Tests/TMGMTUiTest.php +++ b/src/Tests/TMGMTUiTest.php @@ -786,7 +786,7 @@ class TMGMTUiTest extends EntityTestBase { $this->assertLink('Abort'); $this->clickLink('Abort'); - $this->assertText(t('Abort this job item?')); + $this->assertText(t('Are you sure you want to abort the job item test_source:test:1?')); // Check if cancel button is present or not. $this->assertLink('Cancel'); diff --git a/tmgmt.routing.yml b/tmgmt.routing.yml index fa8f343..2595c1a 100644 --- a/tmgmt.routing.yml +++ b/tmgmt.routing.yml @@ -101,7 +101,7 @@ entity.tmgmt_job_item.abort_form: defaults: _entity_form: tmgmt_job_item.abort requirements: - _entity_access: tmgmt_job_item.update + _entity_access: tmgmt_job_item.abort tmgmt.source_overview: path: '/admin/tmgmt/sources/{plugin}/{item_type}'