Workspaces' entity presave hook omits entities provided by the workspaces module. When trying to combine it with the content_moderation module, I found out that the content_moderation_state entity should be omitted as well. @amateescu suggested that this is probably true for all internal entity types.

In this case, all workspaces get their own revision of an entities moderation state, which means they can have different moderation states per workspace. This is exactly the behaviour my current use case needs, but I'm not sure this is intended.

Comments

pmelab created an issue. See original summary.

pmelab’s picture

StatusFileSize
new939 bytes
plach’s picture

Status: Active » Needs review

Needs review?

amateescu’s picture

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

Working on an updated patch.

amateescu’s picture

Title: Omit workspaces entity presave hook for internal entities » Omit workspaces entity presave and predelete hooks for internal entities
Assigned: amateescu » Unassigned
Status: Needs work » Needs review
Issue tags: +Workflow Initiative
StatusFileSize
new9.32 KB
+++ b/core/modules/workspaces/src/EntityOperations.php
@@ -116,9 +116,9 @@ public function entityPreload(array $ids, $entity_type_id) {
-    if ($entity_type->getProvider() === 'workspaces' || $this->workspaceManager->getActiveWorkspace()->isDefaultWorkspace()) {
...
+    if ($entity_type->isInternal() || $this->workspaceManager->getActiveWorkspace()->isDefaultWorkspace()) {

This change makes the workspace entity type (which is not internal) unable to be changed in a non-default workspace, as the test fails show :)

Also, we need to skip the pre-delete hook for internal entity types as well, which shows that this code can be a little confusing. Let's introduce a dedicated method for the checks in pre-save and pre-delete, so we don't get into the same problem again.

Reworked the test coverage because a phpunit test method should set an expected exception only once.

pmelab’s picture

Status: Needs review » Reviewed & tested by the community

Reviewed the patch and tested with our current use case (involving quite a lot of different internal entities). All works as expected.

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

larowlan’s picture

+++ b/core/modules/workspaces/src/WorkspaceManager.php
@@ -254,6 +254,19 @@ public function shouldAlterOperations(EntityTypeInterface $entity_type) {
+  public function shouldSkipPreOperations(EntityTypeInterface $entity_type) {

Does workspaces have a notion of handlers? This feels like something we might want the entity providers to have a say in?

E.g. content moderation has handlers with a default and then node has its own with some extra sauce

amateescu’s picture

@larowlan, nope, we didn't find the need to have per-entity type customizations so far.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/workspaces/src/WorkspaceManager.php
@@ -254,6 +254,19 @@ public function shouldAlterOperations(EntityTypeInterface $entity_type) {
+  /**
+   * {@inheritdoc}
+   */
+  public function shouldSkipPreOperations(EntityTypeInterface $entity_type) {

Out-of-interest how come this is on WorkspaceManager and not a protected method on \Drupal\workspaces\EntityOperations? It seems odd for this implementation detail to be in the Workspace manager.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new8.69 KB
new4.45 KB

@alexpott, very good point :) The only reason I put that helper method on the workspace manager is to make it easier to override if anyone needs some custom behavior. But after your question, I realized that we instantiate the entity operations class with the class resolver service, which means that developers can provide a service with that class name and the override ability is not lost.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/workspaces/src/EntityOperations.php
@@ -340,4 +343,24 @@ public static function entityFormEntityBuild($entity_type_id, RevisionableInterf
+    // - the entity type is internal, which means that it should not affect
+    //   anything in the default (Live) workspace;

I've read this a few times and I'm just not sure we can make this assumption. Here are docs for isInternal()

   * Indicates whether the entity data is internal.
   *
   * This can be used in a scenario when it is not desirable to expose data of
   * this entity type to an external system.
   *
   * The implications of this method are left to the discretion of the caller.
   * For example, a module providing an HTTP API may not expose entities of
   * this type or a custom entity reference field settings form may deprioritize
   * entities of this type in a select list.

I'm not sure that

which means that it should not affect anything in the default (Live) workspace;

is really an assumption that can be made. Or if it can be then we really need to explain why.

amateescu’s picture

Status: Needs work » Reviewed & tested by the community

Discussed this issue with @alexpott at Dev Days Transylvania, and, after reviewing all the usages of internal in core and contrib, we agreed to move forward with the current patch and discuss improving the documentation of that property in a follow-up issue: #3061252: Improve the documentation of the 'internal' property of entity types

amateescu’s picture

amateescu’s picture

Issue tags: +DevDaysTransylvania
alexpott’s picture

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

Committed 76fa7e9 and pushed to 8.8.x. Thanks!

I'm going to commit #11 to 8.7.x once it has had a test run.

  • alexpott committed 76fa7e9 on 8.8.x
    Issue #3027598 by amateescu, pmelab, alexpott, larowlan: Omit workspaces...
amateescu’s picture

StatusFileSize
new8.78 KB

I just checked and #11 doesn't apply anymore to 8.7.x either, so here's a reroll for that.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 37e4d4b and pushed to 8.7.x. Thanks!

  • alexpott committed 37e4d4b on 8.7.x
    Issue #3027598 by amateescu, pmelab, alexpott, larowlan: Omit workspaces...

Status: Fixed » Closed (fixed)

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