getTransitionFromStateToState example

$current_state = $entity->moderation_state->value ? $workflow->getTypePlugin()->getState($entity->moderation_state->value) : $workflow->getTypePlugin()->getInitialState($entity);

    return array_filter($current_state->getTransitions()function DTransition $transition) use ($workflow$user) {
      return $user->hasPermission('use ' . $workflow->id() . ' transition ' . $transition->id());
    });
  }

  /** * {@inheritdoc} */
  public function isTransitionValid(WorkflowInterface $workflow, StateInterface $original_state, StateInterface $new_state, AccountInterface $user, ContentEntityInterface $entity) {
    $transition = $workflow->getTypePlugin()->getTransitionFromStateToState($original_state->id()$new_state->id());
    return $user->hasPermission('use ' . $workflow->id() . ' transition ' . $transition->id());
  }

}
public function canTransitionTo($to_state_id) {
    return $this->workflow->hasTransitionFromStateToState($this->id, $to_state_id);
  }

  /** * {@inheritdoc} */
  public function getTransitionTo($to_state_id) {
    if (!$this->canTransitionTo($to_state_id)) {
      throw new \InvalidArgumentException("Can not transition to '$to_state_id' state");
    }
    return $this->workflow->getTransitionFromStateToState($this->id()$to_state_id);
  }

  /** * {@inheritdoc} */
  public function getTransitions() {
    return $this->workflow->getTransitionsForState($this->id);
  }

  /** * Helper method to convert a State value object to a label. * * @param \Drupal\workflows\StateInterface $state * The state. * * @return string * The label of the state. */
    $workflow
      ->getTypePlugin()
      ->addState('draft', 'Draft')
      ->addState('published', 'Published')
      ->addState('archived', 'Archived')
      ->addTransition('create_new_draft', 'Create new draft', ['archived', 'draft'], 'draft')
      ->addTransition('publish', 'Publish', ['draft', 'published'], 'published')
      ->addTransition('archive', 'Archive', ['published'], 'archived');

    $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('draft', 'published'));
    $this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('archived', 'archived'));
    $transition = $workflow->getTypePlugin()->getTransitionFromStateToState('published', 'archived');
    $this->assertEquals('Archive', $transition->label());
  }

  /** * @covers ::getTransitionFromStateToState */
  public function testGetTransitionFromStateToStateException() {
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage("The transition from 'archived' to 'archived' does not exist in workflow.");
    $workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
    // By default states are ordered in the order added.

  public function validateForm(array &$form, FormStateInterface $form_state) {
    /** @var \Drupal\workflows\WorkflowInterface $workflow */
    $workflow = $this->getEntity();
    $workflow_type = $workflow->getTypePlugin();
    $transition = $workflow_type->getTransition($this->transitionId);

    $values = $form_state->getValues();
    foreach (array_filter($values['from']) as $from_state_id) {
      if ($workflow_type->hasTransitionFromStateToState($from_state_id$values['to'])) {
        $existing_transition = $workflow_type->getTransitionFromStateToState($from_state_id$values['to']);
        if ($existing_transition->id() !== $values['id']) {
          $form_state->setErrorByName('from][' . $from_state_id$this->t('The transition from %from to %to already exists.', [
            '%from' => $workflow->getTypePlugin()->getState($from_state_id)->label(),
            '%to' => $workflow->getTypePlugin()->getState($values['to'])->label(),
          ]));
        }
      }
    }

    if ($workflow_type->hasFormClass(TransitionInterface::PLUGIN_FORM_KEY)) {
      $subform_state = SubformState::createForSubform($form['type_settings']$form$form_state);
      
Home | Imprint | This part of the site doesn't use cookies.