getRequiredStates example


  protected function checkAccess(EntityInterface $entity$operation, AccountInterface $account) {
    /** @var \Drupal\workflows\Entity\Workflow $entity */
    $workflow_type = $entity->getTypePlugin();
    if (str_starts_with($operation, 'delete-state')) {
      [$state_id] = explode(':', $operation, 2);
      // Deleting a state is editing a workflow, but also we should forbid       // access if there is only one state.       return AccessResult::allowedIf(count($entity->getTypePlugin()->getStates()) > 1)
        ->andIf(parent::checkAccess($entity, 'edit', $account))
        ->andIf(AccessResult::allowedIf(!in_array($state_id$workflow_type->getRequiredStates(), TRUE)))
        ->addCacheableDependency($entity);
    }

    return parent::checkAccess($entity$operation$account);
  }

  /** * {@inheritdoc} */
  protected function checkCreateAccess(AccountInterface $account, array $context$entity_bundle = NULL) {
    $workflow_types_count = count($this->workflowTypeManager->getDefinitions());
    

class ContentModerationStateForm extends WorkflowTypeStateFormBase {

  /** * {@inheritdoc} */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state, StateInterface $state = NULL) {
    /** @var \Drupal\content_moderation\ContentModerationState $state */
    $state = $form_state->get('state');
    $is_required_state = isset($state) ? in_array($state->id()$this->workflowType->getRequiredStates(), TRUE) : FALSE;

    $form = [];
    $form['published'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Published'),
      '#description' => $this->t('When content reaches this state it should be published.'),
      '#default_value' => isset($state) ? $state->isPublishedState() : FALSE,
      '#disabled' => $is_required_state,
    ];

    $form['default_revision'] = [
      
/** * @covers ::getRequiredStates * @covers ::__construct */
  public function testGetRequiredStates() {
    $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() {
    

  protected $pluginCollection;

  /** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    $workflow_type = $this->getTypePlugin();
    $missing_states = array_diff($workflow_type->getRequiredStates()array_keys($this->getTypePlugin()->getStates()));
    if (!empty($missing_states)) {
      throw new RequiredStateMissingException(sprintf("Workflow type '{$workflow_type->label()}' requires states with the ID '%s' in workflow '{$this->id()}'", implode("', '", $missing_states)));
    }
    parent::preSave($storage);
  }

  /** * {@inheritdoc} */
  public function getTypePlugin() {
    return $this->getPluginCollection()->get($this->type);
  }
Home | Imprint | This part of the site doesn't use cookies.