How to programmatically change Workflow State of translated node

Created on 3 December 2018, over 5 years ago
Updated 26 March 2024, 3 months ago

I have a content node that has several translated nodes attached to it. My goal is to set the workflow transition state of translated nodes according to the state of the "original" node.
The problem is, that the "original" node properly changes its workflow transition state, but the translated nodes are untouched.
I noticed, that I get several Warnings 'Transition is executed twice in a call. The second call for node 1 is not executed.' when trying to update the workflow state of all of the nodes translations. That could explain that the state of the translations doesn't update.
But how can the workflow state of the translations be synced to the main (in this case english) one?

I think this is a bug

Code to reproduce the problem (called in a form_submit handler):

$entity= $form_state->getFormObject()->getEntity();
$user = User::load(\Drupal::currentUser()->id());
$fieldName = 'field_workflow_state';
$formToState = $form_state->getValue($fieldName)[0]['workflow_transition']->getToState()->id();
$existingTranslations = array_keys($entity->getTranslationLanguages());
$currentLanguage = \Drupal::languageManager()->getCurrentLanguage()->getId();
foreach ($existingTranslations as $translationAbbreviation) {
  if ($currentLanguage !== $translationAbbreviation) {
    $translatedEntity = $entity->getTranslation($translationAbbreviation);
    $currentSid = $translatedEntity->$fieldName->value;
    $transition = WorkflowTransition::create([$currentSid, 'field_name' => $fieldName]);
    $transition->setTargetEntity($translatedEntity);
    $comment = $form[$fieldName]['widget'][0]['comment']['#value'];
    $transition->setValues($formToState, $user->id(), \Drupal::time()->getRequestTime(), $comment, true);
    $transitionId = $transition->execute(true);
    $translatedEntity->$fieldName->value = $transitionId;
    $translatedEntity->save();
  }
}

I also tried just using this:

$translatedEntity = $application->getTranslation($translationAbbreviation);
$translatedEntity->set($fieldAppRevState, $appFormToState);
$translatedEntity->save();

but the problem persists.

πŸ’¬ Support request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany jl_cs

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.

Production build 0.69.0 2024