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

Tentative fix for saving records with new workflow #342

Closed
wants to merge 1 commit into from
Closed
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
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'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these the same thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears to be, yep. Perhaps the first was supposed to be isset.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isset would be covered by empty anyway, maybe we just need the one

)
) {
$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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like false would suit the method name better, assuming it should return a bool (unless false and null have different results for the user, like a validation error)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They do, and this has been addressed in #345

}

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