Skip to content

Commit 624c172

Browse files
Chris Pennychrispenny
authored andcommitted
Improve extensibility for external modules
1 parent e051309 commit 624c172

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,12 @@
4242
}
4343
},
4444
"prefer-stable": true,
45-
"minimum-stability": "dev"
45+
"minimum-stability": "dev",
46+
"config": {
47+
"allow-plugins": {
48+
"composer/installers": true,
49+
"silverstripe/vendor-plugin": true,
50+
"silverstripe/recipe-plugin": true
51+
}
52+
}
4653
}

src/Extension/EmbargoExpiryCMSMainExtension.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Terraformers\EmbargoExpiry\Extension;
44

5+
use Exception;
56
use SilverStripe\CMS\Controllers\CMSMain;
67
use SilverStripe\Control\HTTPResponse_Exception;
78
use SilverStripe\Core\Extension;
@@ -108,8 +109,19 @@ protected function removeEmbargoOrExpiry(string $className, int $id, string $dat
108109
throw new AccessDeniedException('You do not have permission to remove embargo and expiry dates.');
109110
}
110111

111-
// Writing the record with no embargo set will automatically remove the queued jobs.
112-
$record->{$dateField} = null;
112+
// Clear the appropriate Job and field
113+
switch ($dateField) {
114+
case 'PublishOnDate':
115+
$record->clearPublishJob();
116+
117+
break;
118+
case 'UnPublishOnDate':
119+
$record->clearUnPublishJob();
120+
121+
break;
122+
default:
123+
throw new Exception('Invalid action submitted');
124+
}
113125

114126
$record->write();
115127
}

src/Extension/EmbargoExpiryExtension.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Terraformers\EmbargoExpiry\Extension;
44

5+
use DateTimeImmutable;
56
use SilverStripe\Core\Injector\Injector;
67
use SilverStripe\Forms\DatetimeField;
78
use SilverStripe\Forms\FieldList;
@@ -22,7 +23,6 @@
2223
use Symbiote\QueuedJobs\Services\QueuedJobService;
2324
use Terraformers\EmbargoExpiry\Job\PublishTargetJob;
2425
use Terraformers\EmbargoExpiry\Job\UnPublishTargetJob;
25-
use DateTimeImmutable;
2626

2727
/**
2828
* Class WorkflowEmbargoExpiryExtension
@@ -202,7 +202,16 @@ public function onBeforeWrite(): void
202202
}
203203

204204
// Jobs can only be queued for records that already exist
205-
if (!$this->owner->ID) {
205+
if (!$this->owner->isInDB()) {
206+
return;
207+
}
208+
209+
// We allow other extensions/modules to prevent Jobs from being queued (only temporarily though, we hope). EG:
210+
// The Advanced Workflow module will prevent Jobs being queued during write() operations if a Workflow is set,
211+
// and will later allow them during an approval step
212+
$extensionResults = $this->owner->invokeWithExtensions('preventEmbargoExpiryQueueJobs');
213+
214+
if (in_array(true, $extensionResults)) {
206215
return;
207216
}
208217

@@ -496,6 +505,7 @@ public function objectRequiresPublishJob(): bool
496505
// You might have some additional requirements for allowing a PublishJob to be created.
497506
/** @var array|bool[] $canHavePublishJob */
498507
$canHavePublishJob = $this->owner->invokeWithExtensions('publishJobCanBeQueued');
508+
499509
// One or more extensions said that this Object cannot have a PublishJob.
500510
if (in_array(false, $canHavePublishJob)) {
501511
return false;

src/Extension/EmbargoExpiryGridFieldItemRequestExtension.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Terraformers\EmbargoExpiry\Extension;
44

5+
use Exception;
56
use SilverStripe\Control\Controller;
67
use SilverStripe\Control\HTTPResponse;
78
use SilverStripe\Control\HTTPResponse_Exception;
@@ -124,8 +125,19 @@ public function removeEmbargoOrExpiry(string $dateField): void
124125
throw new AccessDeniedException('You do not have permission to remove embargo and expiry dates.');
125126
}
126127

127-
// Writing the record with no embargo set will automatically remove the queued jobs.
128-
$record->{$dateField} = null;
128+
// Clear the appropriate Job and field
129+
switch ($dateField) {
130+
case 'PublishOnDate':
131+
$record->clearPublishJob();
132+
133+
break;
134+
case 'UnPublishOnDate':
135+
$record->clearUnPublishJob();
136+
137+
break;
138+
default:
139+
throw new Exception('Invalid action submitted');
140+
}
129141

130142
$record->write();
131143
}

0 commit comments

Comments
 (0)