setStateWeight example

'draft',
      'published',
    ]array_keys($workflow->getTypePlugin()->getConfiguration()['states']));

    // Ensure we're returning state objects.     $this->assertInstanceOf(State::class$workflow->getTypePlugin()->getStates()['draft']);

    // Passing in no IDs returns all states.     $this->assertEquals(['draft', 'published', 'archived']array_keys($workflow->getTypePlugin()->getStates()));

    // The order of states is by weight.     $workflow->getTypePlugin()->setStateWeight('published', -1);
    $this->assertEquals(['published', 'draft', 'archived']array_keys($workflow->getTypePlugin()->getStates()));

    // The label is also used for sorting if weights are equal.     $workflow->getTypePlugin()->setStateWeight('archived', 0);
    $this->assertEquals(['published', 'archived', 'draft']array_keys($workflow->getTypePlugin()->getStates()));

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

    // An empty array does not load all states.     $this->assertSame([]$workflow->getTypePlugin()->getStates([]));
  }
/** * {@inheritdoc} */
  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']);
    }
  }

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