3

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.

My code so far (called in a form_submit handler):

$entity= \Drupal::routeMatch()->getParameter('node');
$user = User::load(\Drupal::currentUser()->id());
$formToState = $form_state->getValue('field_workflow_state')[0]['workflow_transition']->getToState()->id();
$existingTranslations = array_keys($entity->getTranslationLanguages());
$currentLanguage = \Drupal::languageManager()->getCurrentLanguage()->getId();
$fieldName = 'field_workflow_state';
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['field_workflow_state']['widget'][0]['comment']['#value'];
    $transition->setValues($formToState, $user->id(), \Drupal::time()->getRequestTime(), $comment, true);
    $transitionId = $transition->execute(true);
    $translatedEntity->$fieldName->value = $transitionId;
    $translatedEntity->save();
  }
}

The transitions seem to be executed correctly, but if I check, they are still in the old state. Can someone point me to where the problem is?

Edit: 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?

  • where you put your code? Node form alter, form transition ... ? – Jonh Nov 30 '18 at 8:54
  • "(called in a form_submit handler)" – plocks Nov 30 '18 at 8:54
  • Why are you loading the node entity from the request and not from the $form_state? – Stefanos Petrakis Nov 30 '18 at 21:04
  • e.g. $entity = $form_state->getFormObject()->getEntity(); – Stefanos Petrakis Nov 30 '18 at 21:14
  • 1
    Shouldn't play a part in your current situation @plocks ; it's just better to rely on the actual form state instead of picking entities from the route. – Stefanos Petrakis Dec 1 '18 at 23:48

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.