hasTransitionFromStateToState example


  public function setTransitionFromStates($transition_id, array $from_state_ids) {
    if (!$this->hasTransition($transition_id)) {
      throw new \InvalidArgumentException("The transition '$transition_id' does not exist in workflow.");
    }

    // Ensure that the states exist.     foreach ($from_state_ids as $from_state_id) {
      if (!$this->hasState($from_state_id)) {
        throw new \InvalidArgumentException("The state '$from_state_id' does not exist in workflow.");
      }
      if ($this->hasTransitionFromStateToState($from_state_id$this->configuration['transitions'][$transition_id]['to'])) {
        $existing_transition_id = $this->getTransitionIdFromStateToState($from_state_id$this->configuration['transitions'][$transition_id]['to']);
        if ($transition_id !== $existing_transition_id) {
          throw new \InvalidArgumentException("The '$existing_transition_id' transition already allows '$from_state_id' to '{$this->configuration['transitions'][$transition_id]['to']}' transitions in workflow.");
        }
      }
    }

    // Preserve the order of the state IDs in the from value and don't save any     // keys.     $from_state_ids = array_values($from_state_ids);
    sort($from_state_ids);
    
$workflow = Workflow::create([
      'id' => 'test',
      'type' => 'workflow_type_required_state_test',
    ]);
    $workflow->save();
    $this->assertEquals(['fresh', 'rotten']$workflow->getTypePlugin()
      ->getRequiredStates());

    // Ensure that the workflow has the default configuration.     $this->assertTrue($workflow->getTypePlugin()->hasState('rotten'));
    $this->assertTrue($workflow->getTypePlugin()->hasState('fresh'));
    $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('fresh', 'rotten'));
  }

  /** * @covers \Drupal\workflows\Entity\Workflow::preSave */
  public function testDeleteRequiredStateAPI() {
    $workflow = Workflow::create([
      'id' => 'test',
      'type' => 'workflow_type_required_state_test',
    ]);
    $workflow->save();
    
/** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    /** @var \Drupal\workflows\WorkflowInterface $workflow */
    $workflow = $this->getEntity();
    $workflow_type = $workflow->getTypePlugin();

    $values = $form_state->getValues();
    foreach (array_filter($values['from']) as $from_state_id) {
      if ($workflow->getTypePlugin()->hasTransitionFromStateToState($from_state_id$values['to'])) {
        $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);
      $this->pluginFormFactory
        ->createInstance($workflow_type, TransitionInterface::PLUGIN_FORM_KEY)
        

  public function testAddAndHasState() {
    $workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
    $this->assertFalse($workflow->getTypePlugin()->hasState('draft'));

    // By default states are ordered in the order added.     $workflow->getTypePlugin()->addState('draft', 'Draft');
    $this->assertTrue($workflow->getTypePlugin()->hasState('draft'));
    $this->assertFalse($workflow->getTypePlugin()->hasState('published'));
    $this->assertEquals(0, $workflow->getTypePlugin()->getState('draft')->weight());
    // Adding a state does not set up a transition to itself.     $this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('draft', 'draft'));

    // New states are added with a new weight 1 more than the current highest     // weight.     $workflow->getTypePlugin()->addState('published', 'Published');
    $this->assertEquals(1, $workflow->getTypePlugin()->getState('published')->weight());
  }

  /** * @covers ::addState */
  public function testAddStateException() {
    
// Create a new transition.     $this->submitForm(['id' => 'save_and_publish', 'label' => 'Save and publish', 'from[published]' => 'published', 'to' => 'published'], 'Save');
    $this->assertSession()->pageTextContains('Created Save and publish transition.');
    // Edit the new transition and try to add an existing transition.     $this->clickLink('Edit', 4);
    $this->submitForm(['from[draft]' => 'draft'], 'Save');
    $this->assertSession()->pageTextContains('The transition from Draft to Live already exists.');

    // Delete the transition.     $workflow = $workflow_storage->loadUnchanged('test');
    $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('published', 'published'), 'Can transition from published to published');
    $this->clickLink('Delete');
    $this->assertSession()->pageTextContains('Are you sure you want to delete Save and publish from Test?');
    $this->submitForm([], 'Delete');
    $workflow = $workflow_storage->loadUnchanged('test');
    $this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('published', 'published'), 'Cannot transition from published to published');

    // Try creating a duplicate state.     $this->drupalGet('admin/config/workflow/workflows/manage/test');
    $this->clickLink('Add a new state');
    $this->submitForm(['label' => 'Draft', 'id' => 'draft'], 'Save');
    $this->assertSession()->pageTextContains('The machine-readable name is already in use. It must be unique.');

    
/** * {@inheritdoc} */
  public function weight() {
    return $this->weight;
  }

  /** * {@inheritdoc} */
  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 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)) {
      
Home | Imprint | This part of the site doesn't use cookies.