How to programmatically create a Workflow Transition

Created on 7 November 2017, about 7 years ago
Updated 7 March 2024, 10 months ago

I am creating workflow transition programmatically for node by below code.

$node = node_load($nid);
$transition = new \Drupal\workflow\Entity\WorkflowTransition();
$transition->setTargetEntity($node);
$transition->setValues($new_sid,1,REQUEST_TIME,'',FALSE);
$transition->execute(TRUE); 

when I run this code. I am getting error as:

Wrong call to constructor Workflow*Transition( to $newsid)
You tried to set a Workflow State, but the entity is not relevant. Please contact your system administrator.

I have debugged the code. I found that "from_sid" is not getting set.

In D7, $old_sid argument will be in "setValues()". In Drupal 8, that argument is removed.

Can anyone help me, how should I create workflow transition for node programmatically?

Thanks,
Saranya.P

💬 Support request
Status

Active

Version

1.0

Component

Code

Created by

🇮🇳India saranya ashokkumar

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.

  • 🇺🇸United States todea

    Do these transitions still work? I was running into issues integrating this into either hook_entity_update() or hook_ENTITY_TYPE_update()

  • 🇳🇱Netherlands johnv

    Check workflow.module file.
    Transitions are saved upon presave()

  • 🇺🇸United States todea

    Thank you. I am now getting the following error when using the presave hook.

    Drupal\Core\Entity\EntityStorageException: Value is not a valid entity. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

  • 🇳🇱Netherlands johnv

    Can you share your code and the call stack?

  • 🇺🇸United States todea

    Here is the code. I have a ton of use statements in there due to earlier testing. Error output is below the code.

    <?php
    
    /**
     * @file
     * Contains mymodule.module..
     */
    
     use Drupal\Core\Entity\EntityInterface;
     use Drupal\Core\Entity\EntityTypeInterface;
     use Drupal\Core\Field\Entity\BaseFieldOverride;
     use Drupal\Core\Routing\RouteMatchInterface;
     use Drupal\gin\GinSettings;
     use Drupal\Core\Form\FormStateInterface;
     use Drupal\node\Entity\Node;
     use Drupal\node\NodeInterface;
     use Drupal\block\Entity\Block;
     use Drupal\Core\Access\AccessResult;
     use Drupal\Core\Session\AccountInterface;
     use Drupal\views\ViewExecutable;
     use Drupal\Core\Url;
     
     use Drupal\workflow\Entity\Workflow;
     use Drupal\workflow\Entity\WorkflowManager;
     use Drupal\workflow\Entity\WorkflowScheduledTransition;
     use Drupal\workflow\Entity\WorkflowConfigTransition;
     use Drupal\workflow\Entity\WorkflowState;
     use Drupal\workflow\Entity\WorkflowTransitionInterface;
     use Drupal\workflow\Entity\WorkflowTransition;
     
     
     // ####################################################
     // WORKFLOW
     // ####################################################
    
     
    function mymodule_entity_presave(EntityInterface $entity) {
    
    if ((!empty($entity)) && ($entity->bundle() == 'event')) {
    
      if (!$entity->isNew()) {
    
          dpm($entity);
    
          $field_name = 'field_event_workflow';
          $next_state = "event_reporting_complete";
          //$entity = \Drupal::entityManager()->getStorage($entity_type)->load($entity_id);
          //$entity = \Drupal::EntityTypeManager()->getStorage($entity->getEntityTypeId())->load($entity->id());
    
          $current_user = \Drupal::currentUser();
          $current_sid = $entity->$field_name->value;
    
          dpm($current_user);
          dpm($current_sid);
    
          if (!$entity) {
                dpm('not entity');
          } else {
                dpm('is entity');
          }
    
          $entity_id = $entity->id();
          $entity_type = $entity->getEntityTypeId();
    
          $transition = WorkflowTransition::create([$current_sid, 'field_name' => $field_name]);
          $transition->setTargetEntity($entity);
          $transition->setValues($next_state, $current_user, \Drupal::time()->getRequestTime(), '', TRUE);
          $transition->force($force);
    
      } 
    
    }
    }

    Error:

    The website encountered an unexpected error. Try again later.

    Drupal\Core\Entity\EntityStorageException: Value is not a valid entity. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
    Drupal\Core\Field\FieldItemBase->writePropertyValue('entity', Object) (Line: 131)
    Drupal\Core\TypedData\Plugin\DataType\Map->set('entity', Object, ) (Line: 231)
    Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem->setValue(Object, ) (Line: 72)
    Drupal\Core\TypedData\Plugin\DataType\ItemList->setValue(Array, 1) (Line: 107)
    Drupal\Core\Field\FieldItemList->setValue(Array, 1) (Line: 657)
    Drupal\Core\Entity\ContentEntityBase->set('uid', Object) (Line: 894)
    Drupal\workflow\Entity\WorkflowTransition->setOwnerId(Object) (Line: 185)
    Drupal\workflow\Entity\WorkflowTransition->setValues('event_reporting_expired', Object, 1707331982, '', 1) (Line: 127)
    bpl_config_entity_presave(Object)
    call_user_func_array(Object, Array) (Line: 409)
    Drupal\Core\Extension\ModuleHandler->Drupal\Core\Extension\{closure}(Object, 'bpl_config') (Line: 388)
    Drupal\Core\Extension\ModuleHandler->invokeAllWith('entity_presave', Object) (Line: 408)
    Drupal\Core\Extension\ModuleHandler->invokeAll('entity_presave', Array) (Line: 217)
    Drupal\Core\Entity\EntityStorageBase->invokeHook('presave', Object) (Line: 900)
    Drupal\Core\Entity\ContentEntityStorageBase->invokeHook('presave', Object) (Line: 529)
    Drupal\Core\Entity\EntityStorageBase->doPreSave(Object) (Line: 753)
    Drupal\Core\Entity\ContentEntityStorageBase->doPreSave(Object) (Line: 483)
    Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 806)
    Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 352)
    Drupal\Core\Entity\EntityBase->save() (Line: 270)
    Drupal\node\NodeForm->save(Array, Object)
    call_user_func_array(Array, Array) (Line: 129)
    Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 67)
    Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 597)
    Drupal\Core\Form\FormBuilder->processForm('node_event_edit_form', Array, Object) (Line: 325)
    Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
    Drupal\Core\Controller\FormController->getContentResult(Object, Object) (Line: 39)
    Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult(Object, Object)
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 627)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 121)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
    Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 38)
    Drupal\tracer\StackMiddleware\TracesMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
    Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

  • Status changed to Active 10 months ago
Production build 0.71.5 2024