Problem/Motivation

When adding a new unpublished content entity in a workspace, the workspace revision metadata field on the entity is not set, resulting in the content being attributed to 'Live'. The content is still attached to the workspace in a workspace_association table row.

As things stand if a user is in a workspace and creates a piece of content it does not appear in that workspace when using views that filter by workspace.

SELECT * FROM node_revision WHERE nid = 1; SELECT * FROM workspace_association WHERE target_entity_id = 1;
nid         vid         langcode    revision_uid  revision_timestamp  revision_log  revision_default  workspace 
----------  ----------  ----------  ------------  ------------------  ------------  ----------------  ----------
1           1           en          1             1587501174                        1                           
workspace   target_entity_type_id  target_entity_id  target_entity_revision_id
----------  ---------------------  ----------------  -------------------------
stage       node                   1                 1                        

This workspace field is set when

  • A new revision is created on an existing entity
  • A new published entity is created

Proposed resolution

Track the initial revisions created in a workspace, for both published and unpublished entities.

Comments

Alexj12 created an issue. See original summary.

alexj12’s picture

Status: Active » Needs review
Issue tags: +Workflow Initiative
StatusFileSize
new1.05 KB

Status: Needs review » Needs work

The last submitted patch, 2: 3129762-2.patch, failed testing. View results

msuthars’s picture

Assigned: Unassigned » msuthars
alexj12’s picture

This failing test appears to indicate the revisions are not expected to be associated with the workspace. This seems somewhat illogical, if you create a new piece of content in a workspace you would expect at least one revision to be within the workspace even if it is unnpublished.

There is also a mismatch with this functionality, the workspace association suggests the revision is in the workspace but the workspace field does not.

Just need some steer as to what the preferred functionality should be.

    // Create a workspace with a very small number of associated node revisions.
    $workspace_1 = Workspace::create([
      'id' => 'gibbon',
      'label' => 'Gibbon',
    ]);
    $workspace_1->save();
    $this->workspaceManager->setActiveWorkspace($workspace_1);

    $workspace_1_node_1 = $this->createNode(['status' => FALSE]);
    $workspace_1_node_2 = $this->createNode(['status' => FALSE]);

    // The 'live' workspace should have 2 revisions now. The initial revision
    // for each node.
    $live_revisions = $this->getUnassociatedRevisions('node');
    $this->assertCount(2, $live_revisions);
msuthars’s picture

@Alexj12 Yes because of this functionality when nodes are created as unpublished then it is not creating any revision in the live for those nodes.

msuthars’s picture

StatusFileSize
new4.68 KB

I just remove the assetCount for live revision, because after adding this functionality there will no live revision will created.

msuthars’s picture

Assigned: msuthars » Unassigned
Status: Needs work » Needs review

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

amateescu’s picture

Assigned: Unassigned » amateescu
Status: Needs review » Needs work
Issue tags: -Workflow Initiative

This problem was also discovered in #3092247: Prevent content from being deleted when there is an active workspace. I will bring over the relevant changes from that issue to this one because the fix is a bit more involved than the current patch.

amateescu’s picture

Version: 9.5.x-dev » 10.1.x-dev
Priority: Normal » Major
StatusFileSize
new6.49 KB
new8.7 KB

Let's start with explicit test coverage for this bug, and the fixes required for it.

The second patch will fail _other_ tests because we need to update that test coverage to account for the change we're introducing: when creating a new entity in a workspace, either published or unpublished, the initial revision will now be considered as part of the workspace.

Leaving at needs work for now, will followup with the complete patch.

adriancid’s picture

StatusFileSize
new8.75 KB
new2.49 KB

I had these errors while testing the patch, so I added a validation to check for the existence of the element in the array:

  • Warning: Undefined array key "test_delete_workspace" in Drupal\workspaces\WorkspaceAssociation->getAssociatedRevisions() (line 179 of /var/www/html/web/core/modules/workspaces/src/WorkspaceAssociation.php) 
  • Warning: Trying to access array offset on value of type null in Drupal\workspaces\WorkspaceAssociation->getAssociatedRevisions() (line 179 of /var/www/html/web/core/modules/workspaces/src/WorkspaceAssociation.php) 
  • TypeError: array_merge(): Argument #2 must be of type array, null given in array_merge() (line 179 of /var/www/html/web/core/modules/workspaces/src/WorkspaceAssociation.php)
adriancid’s picture

StatusFileSize
new8.75 KB
new1.48 KB

Fixing a problem with undefined $workspace_candidates.

amateescu’s picture

StatusFileSize
new6.37 KB

The test-only patch from #15 was flawed, it didn't take into account editing an existing entity in a workspace. Reworked the test coverage to cover all cases, and this shows that we can't check for "initial revisions" (of the entities that are created in a workspace) at the moment.

amateescu’s picture

StatusFileSize
new11.92 KB
new6.25 KB

These are the fixes needed for the test-only patch from #18. As mentioned in #15, some other tests will fail because workspaces are now tracking initial default revisions, and I will follow up with a complete patch.

amateescu’s picture

Title: Creating an unpublished entity in non-default workspace does not set the workspace field on the revision » Creating an unpublished entity in a workspace does not set the workspace field on the revision
Assigned: amateescu » Unassigned
Issue summary: View changes
Status: Needs work » Needs review
Issue tags: +WI critical
StatusFileSize
new24.64 KB
new12.72 KB

Here are the changes needed to fix the existing test coverage. This is fully ready for reviews now.

adriancid’s picture

Status: Needs review » Reviewed & tested by the community

We are using this patch in a production site and is working as expected. I know usually it needs some screenshots but I can confirm is working.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

There's some API changes here (new arguments to plugin constructors, new methods).
Whilst we don't have to provide a BC layer, it would be nice to add a change notice, so tagged for that.

We've not had a test-run that shows the new tests fail yet (they didn't pass the custom checks so tests didn't run) - so can we get a red/green set of patches - ie one with just the tests, and one with tests and fixes.

Couple of minor observations

  1. +++ b/core/modules/workspaces/src/WorkspaceAssociation.php
    @@ -192,6 +200,42 @@ public function getAssociatedRevisions($workspace_id, $entity_type_id, $entity_i
    +  public function getAssociatedInitialRevisions($workspace_id, $entity_type_id, $entity_ids = NULL) {
    
    +++ b/core/modules/workspaces/src/WorkspaceAssociationInterface.php
    @@ -74,6 +74,23 @@ public function getTrackedEntities($workspace_id, $entity_type_id = NULL, $entit
    +  public function getAssociatedInitialRevisions($workspace_id, $entity_type_id, $entity_ids = NULL);
    

    Can we get typehints here for new code

  2. +++ b/core/modules/workspaces/src/WorkspaceManager.php
    @@ -316,31 +316,60 @@ public function purgeDeletedWorkspacesBatch() {
    +          $associated_entity_storage->delete([$associated_entity_storage->load($initial_revision_ids[$revision_id])]);
    

    should we try to do this in bulk outside the foreach loop?

amateescu’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs change record
StatusFileSize
new6.37 KB
new1022 bytes
new24.68 KB
new1.89 KB

Good point about the CR, wrote one here: https://www.drupal.org/node/3352703

Attaching the test-only patch from #18 with the custom checks fixed, as well as the complete one from #20 with the point from #22.1 fixed.

I thought a lot about #22.2 while working on this patch and eventually settled on the argument that this is "just" cleanup code that usually runs on cron, and that it's better to be correct and easy to read rather than fast. Loading multiple default revisions would only save a few cache gets anyway :)

The last submitted patch, 23: 3129762-23-test-only.patch, failed testing. View results

  • catch committed cd12944d on 10.1.x
    Issue #3129762 by amateescu, adriancid, msuthars, Alexj12: Creating an...
catch’s picture

Status: Reviewed & tested by the community » Fixed

This is tricky but the changes look solid and agreed the CR is plenty for the changes.

Committed/pushed to 10.1.x, thanks!

quietone’s picture

I published the change record

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.