Skip to content

Commit

Permalink
Tentative fix for saving records with new workflow
Browse files Browse the repository at this point in the history
Still breaks on subsequent save & publish, but the first one works
  • Loading branch information
Damian Mooyman committed Jan 26, 2018
1 parent 7c484c2 commit 7a0c3c1
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Extensions/WorkflowApplicable.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ class WorkflowApplicable extends DataExtension
'workflowService' => '%$' . WorkflowService::class,
];

/**
* Temporary record of items having workflow applied / removed to it.
* These records should not have canPublish() = false immediately applied until refreshed.
*
* @var array
*/
protected static $itemsChangingWorkflow = [];

/**
*
* Used to flag to this extension if there's a WorkflowPublishTargetJob running.
Expand Down Expand Up @@ -319,6 +327,19 @@ public function LinkToPendingItems()
*/
public function onAfterWrite()
{
// Record changes to the workflow on this record
$changed = $this->owner->getChangedFields(['WorkflowDefinitionID']);
if (isset($changed['WorkflowDefinitionID']) &&
(
empty($changed['WorkflowDefinitionID']['before'])
|| empty($changed['WorkflowDefinitionID']['before'])
)
) {
$key = $this->owner->baseClass() . '#' . $this->owner->ID;
static::$itemsChangingWorkflow[$key] = true;
}

// Get workflow instance
$instance = $this->getWorkflowInstance();
if ($instance && $instance->CurrentActionID) {
$action = $instance->CurrentAction()->BaseAction()->targetUpdated($instance);
Expand Down Expand Up @@ -382,6 +403,12 @@ public function RecentWorkflowComment($limit = 10)
*/
public function canPublish()
{
// Ignore canPublish() if in the middle of having workflow added / removed
$key = $this->owner->baseClass() . '#' . $this->owner->ID;
if (isset(static::$itemsChangingWorkflow[$key])) {
return null;
}

// Override any default behaviour, to allow queuedjobs to complete
if ($this->isPublishJobRunning()) {
return true;
Expand Down

0 comments on commit 7a0c3c1

Please sign in to comment.