Workflow grants not updated on state transition of custom non-Node entity with Workflow BaseField

Created on 11 July 2018, over 6 years ago
Updated 15 March 2024, 11 months ago

I have created workflow field on custom defined entity like below:

    $fields['my_entity'] = BaseFieldDefinition::create('workflow')
      ->setLabel(t('My entity'))
      ->setDescription(t('The my_entity workflow.'))
      ->setRevisionable(TRUE)
      ->setDefaultValue('my_entity_workflow_waiting')
      ->setSetting('target_type', 'workflow_transition')
      ->setSetting('workflow_type', 'my_entity_workflow')
      ->setSetting('wid', 'my_entity_workflow')
      ->setTranslatable(FALSE)
      ->setDisplayOptions('form', [
        'type' => 'workflow_default',
        'weight' => 0,
        ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

When user change transition local tasks still show Edit, Delete links despite workflow access and my_entityAccessControlHandler return not allowed edit or delete in the new state.

Solution:
Implement hook entity update like below and invalidate the entity cache tags so that local tasks links respect access.

/*
* Implements hook_entity_update().
* Invalidate cache tags to that entity so that local tasks rebuilt.
*/
function my_entity_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
  if($entity->getEntityTypeId() == 'my_entity'){

    if($entity->get('my_entity_workflow')->value == 'my_entity_workflow_processing'){
      $entity->getCacheTagsToInvalidate();
    }
  }
}

I don't know whether it's problem with my definition of workflow field or drupal itself didn't invalidate cache tags.

🐛 Bug report
Status

Fixed

Version

1.7

Component

Code

Created by

🇪🇬Egypt mhmd Riydah

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇱Netherlands johnv

    Above patch #4 is implemented in workflow_access module. But you do not mention that. It must be in the general hook_entity_update.

    • johnv committed 25919411 on 8.x-1.x
      Issue #2985022 by johnv, mhmd: Workflow grants not updated on state...
    • johnv committed 5633aef6 on 8.x-1.x
      Issue #2985022 by johnv, mhmd: Workflow grants not updated on state...
  • Status changed to Fixed 12 months ago
  • 🇳🇱Netherlands johnv

    Sorry for the mess.
    I first wanted to hide it deep in WorkflowManager, but decided not to.

    diff --git a/workflow.entity.inc b/workflow.entity.inc
    index f3ea4f3..c97d4dc 100644
    --- a/workflow.entity.inc
    +++ b/workflow.entity.inc
    @@ -69,6 +69,14 @@ function workflow_entity_insert(EntityInterface $entity) {
      */
     function workflow_entity_update(EntityInterface $entity) {
       _workflow_execute_transitions($entity);
    +
    +  // Invalidate cache tags for entity so that local tasks rebuild,
    +  // when Workflow is a BaseField.
    +  $field_names = workflow_get_workflow_field_names($entity);
    +  if ($field_names) {
    +    $entity->getCacheTagsToInvalidate();
    +  }
    +
     }
     
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024