setTransitionWeight example

protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
    // This form can only set the workflow's ID, label and the weights for each     // state.     /** @var \Drupal\workflows\WorkflowInterface $entity */
    $values = $form_state->getValues();
    $entity->set('label', $values['label']);
    $entity->set('id', $values['id']);
    foreach ($values['states'] as $state_id => $state_values) {
      $entity->getTypePlugin()->setStateWeight($state_id$state_values['weight']);
    }
    foreach ($values['transitions'] as $transition_id => $transition_values) {
      $entity->getTypePlugin()->setTransitionWeight($transition_id$transition_values['weight']);
    }
  }

}
// Transitions are stored in alphabetical key order in configuration.     $this->assertEquals(['a_a', 'a_b']array_keys($workflow->getTypePlugin()->getConfiguration()['transitions']));

    // Ensure we're returning transition objects.     $this->assertInstanceOf(Transition::class$workflow->getTypePlugin()->getTransitions()['a_a']);

    // Passing in no IDs returns all transitions.     $this->assertEquals(['a_b', 'a_a']array_keys($workflow->getTypePlugin()->getTransitions()));

    // The order of states is by weight.     $workflow->getTypePlugin()->setTransitionWeight('a_a', -1);
    $this->assertEquals(['a_a', 'a_b']array_keys($workflow->getTypePlugin()->getTransitions()));

    // If all weights are equal it will fallback to labels.     $workflow->getTypePlugin()->setTransitionWeight('a_a', 0);
    $this->assertEquals(['a_a', 'a_b']array_keys($workflow->getTypePlugin()->getTransitions()));
    $workflow->getTypePlugin()->setTransitionLabel('a_b', 'A B');
    $this->assertEquals(['a_b', 'a_a']array_keys($workflow->getTypePlugin()->getTransitions()));

    // You can limit the states returned by passing in states IDs.     $this->assertEquals(['a_a']array_keys($workflow->getTypePlugin()->getTransitions(['a_a'])));

    
Home | Imprint | This part of the site doesn't use cookies.