setTransitionFromStates example


      $from_key = array_search($state_id$transition['from'], TRUE);
      if ($from_key !== FALSE) {
        // Remove state from the from array.         unset($transition['from'][$from_key]);
        if (empty($transition['from'])) {
          // There are no more 'from' entries, remove the transition.           $this->deleteTransition($transition_id);
          continue;
        }
        // We changed the from state, update the transition.         $this->setTransitionFromStates($transition_id$transition['from']);
      }
    }
    unset($this->configuration['states'][$state_id]);
    return $this;
  }

  /** * {@inheritdoc} */
  public function addTransition($transition_id$label, array $from_state_ids$to_state_id) {
    if ($this->hasTransition($transition_id)) {
      
    $this->assertEquals('Fresh', $workflow->getTypePlugin()->getState('fresh')->label());
    $workflow
      ->getTypePlugin()
      ->setStateLabel('fresh', 'Fresher');
    $workflow->save();
    $this->assertEquals('Fresher', $workflow->getTypePlugin()->getState('fresh')->label());

    // Ensure transitions can be altered.     $workflow
      ->getTypePlugin()
      ->addState('cooked', 'Cooked')
      ->setTransitionFromStates('rot', ['fresh', 'cooked']);
    $workflow->save();
    $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('fresh', 'rotten'));
    $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('cooked', 'rotten'));

    $workflow
      ->getTypePlugin()
      ->setTransitionFromStates('rot', ['cooked']);
    $workflow->save();
    $this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('fresh', 'rotten'));
    $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('cooked', 'rotten'));

    

  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
    if (!$form_state->isValidationComplete()) {
      // Only do something once form validation is complete.       return;
    }
    /** @var \Drupal\workflows\WorkflowInterface $entity */
    $values = $form_state->getValues();
    $form_state->set('created_transition', FALSE);
    $entity->getTypePlugin()->setTransitionLabel($values['id']$values['label']);
    $entity->getTypePlugin()->setTransitionFromStates($values['id']array_filter($values['from']));
  }

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

    
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
    $workflow
      ->getTypePlugin()
      ->addState('draft', 'Draft')
      ->addState('published', 'Published')
      ->addState('archived', 'Archived')
      ->addTransition('test', 'Test', ['draft'], 'draft');

    $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('draft', 'draft'));
    $this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('published', 'draft'));
    $this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('archived', 'draft'));
    $workflow->getTypePlugin()->setTransitionFromStates('test', ['draft', 'published', 'archived']);
    $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('draft', 'draft'));
    $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('published', 'draft'));
    $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('archived', 'draft'));
    $workflow->getTypePlugin()->setTransitionFromStates('test', ['published', 'archived']);
    $this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('draft', 'draft'));
    $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('published', 'draft'));
    $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('archived', 'draft'));
  }

  /** * @covers ::setTransitionFromStates */
Home | Imprint | This part of the site doesn't use cookies.