getOriginalState example


    return parent::form($items$form$form_state$get_delta);
  }

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $original_entity = $items->getEntity();

    $default = $this->moderationInformation->getOriginalState($entity);

    // If the entity already exists, grab the most recent revision and load it.     // The moderation state of the saved revision will be used to display the     // current state as well determine the appropriate transitions.     if (!$entity->isNew()) {
      /** @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());
      }
    }
    


  /** * @covers ::getOriginalState */
  public function testGetOriginalState() {
    $entity = EntityTestMulRevPub::create([
      'moderation_state' => 'published',
    ]);
    $entity->save();
    $entity->moderation_state = 'foo';
    $this->assertEquals('published', $this->moderationInformation->getOriginalState($entity)->id());
  }

  /** * @covers ::getOriginalState */
  public function testGetOriginalStateMultilingual() {
    $entity = EntityTestMulRevPub::create([
      'moderation_state' => 'draft',
    ]);
    $entity->save();

    
if (!$workflow->getTypePlugin()->hasState($entity->moderation_state->value)) {
      // If the state we are transitioning to doesn't exist, we can't validate       // the transitions for this entity further.       $this->context->addViolation($constraint->invalidStateMessage, [
        '%state' => $entity->moderation_state->value,
        '%workflow' => $workflow->label(),
      ]);
      return;
    }

    $new_state = $workflow->getTypePlugin()->getState($entity->moderation_state->value);
    $original_state = $this->moderationInformation->getOriginalState($entity);

    // If a new state is being set and there is an existing state, validate     // there is a valid transition between them.     if (!$original_state->canTransitionTo($new_state->id())) {
      $this->context->addViolation($constraint->message, [
        '%from' => $original_state->label(),
        '%to' => $new_state->label(),
      ]);
    }
    else {
      // If we're sure the transition exists, make sure the user has permission
Home | Imprint | This part of the site doesn't use cookies.