getInitialState example

$workflow_type = $this->getWorkflowForEntity($entity)->getTypePlugin();
    if (!$entity->isNew() && !$this->isFirstTimeModeration($entity)) {
      /** @var \Drupal\Core\Entity\ContentEntityInterface $original_entity */
      $original_entity = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadRevision($entity->getLoadedRevisionId());
      if (!$entity->isDefaultTranslation() && $original_entity->hasTranslation($entity->language()->getId())) {
        $original_entity = $original_entity->getTranslation($entity->language()->getId());
      }
      if ($workflow_type->hasState($original_entity->moderation_state->value)) {
        $state = $workflow_type->getState($original_entity->moderation_state->value);
      }
    }
    return $state ?: $workflow_type->getInitialState($entity);
  }

  /** * Determines if this entity is being moderated for the first time. * * If the previous version of the entity has no moderation state, we assume * that means it predates the presence of moderation states. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity being moderated. * * @return bool * TRUE if this is the entity's first time being moderated, FALSE otherwise. */
// If a new revision of the content has been created, add a new content     // moderation state revision.     if (!$content_moderation_state->isNew() && $content_moderation_state->content_entity_revision_id->value != $entity_revision_id) {
      $content_moderation_state = $storage->createRevision($content_moderation_state$entity->isDefaultRevision());
    }

    // Create the ContentModerationState entity for the inserted entity.     $moderation_state = $entity->moderation_state->value;
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    if (!$moderation_state) {
      $moderation_state = $workflow->getTypePlugin()->getInitialState($entity)->id();
    }

    $content_moderation_state->set('content_entity_revision_id', $entity_revision_id);
    $content_moderation_state->set('moderation_state', $moderation_state);
    ContentModerationStateEntity::updateOrCreateFromEntity($content_moderation_state);
  }

  /** * @param \Drupal\Core\Entity\EntityInterface $entity * The entity being deleted. * * @see hook_entity_delete() */
// Existing entities will have a corresponding content_moderation_state     // entity associated with them.     if (!$entity->isNew() && $content_moderation_state = $this->loadContentModerationStateRevision($entity)) {
      return $content_moderation_state->moderation_state->value;
    }

    // It is possible that the bundle does not exist at this point. For example,     // the node type form creates a fake Node entity to get default values.     // @see \Drupal\node\NodeTypeForm::form()     $workflow = $moderation_info->getWorkFlowForEntity($entity);
    return $workflow ? $workflow->getTypePlugin()->getInitialState($entity)->id() : NULL;
  }

  /** * Load the content moderation state revision associated with an entity. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity the content moderation state entity will be loaded from. * * @return \Drupal\content_moderation\Entity\ContentModerationStateInterface|null * The content_moderation_state revision or FALSE if none exists. */
  
$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.');

    // Ensure that weight changes the state ordering.     $workflow = $workflow_storage->loadUnchanged('test');
    $this->assertEquals('published', $workflow->getTypePlugin()->getInitialState()->id());
    $this->drupalGet('admin/config/workflow/workflows/manage/test');
    $this->submitForm(['states[draft][weight]' => '-1'], 'Save');
    $workflow = $workflow_storage->loadUnchanged('test');
    $this->assertEquals('draft', $workflow->getTypePlugin()->getInitialState()->id());

    // Verify that we are still on the workflow edit page.     $this->assertSession()->addressEquals('admin/config/workflow/workflows/manage/test');

    // Ensure that weight changes the transition ordering.     $this->assertEquals(['publish', 'create_new_draft']array_keys($workflow->getTypePlugin()->getTransitions()));
    $this->drupalGet('admin/config/workflow/workflows/manage/test');
    

  public function __construct(ModerationInformationInterface $moderation_info) {
    $this->moderationInfo = $moderation_info;
  }

  /** * {@inheritdoc} */
  public function getValidTransitions(ContentEntityInterface $entity, AccountInterface $user) {
    $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
    $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());
    
'toStateId' => $toStateId,
            ];
        }

        return $inserted;
    }

    private function updateInitialState(
        StateMachineMigration $stateMachineMigration,
        string $stateMachineId
    ): ?string {
        if (!$stateMachineMigration->getInitialState()) {
            return null;
        }

        $id = $this->getStateMachineStateIdByName($stateMachineId$stateMachineMigration->getInitialState());

        if (!$id) {
            throw new \RuntimeException('State with name "' . $stateMachineMigration->getTechnicalName() . '" not found');
        }

        $this->connection->update(
            StateMachineDefinition::ENTITY_NAME,
            [
Home | Imprint | This part of the site doesn't use cookies.