Don't force the state and workflow on new registrations

Created on 31 December 2023, 6 months ago
Updated 14 January 2024, 6 months ago

Problem/Motivation

Currently the code in the registration entity forces the workflow and state to use default values, ignoring what is actually set on the entity.

This makes it very hard for developers to engineer the creation of new registrations with custom workflows or non-default state. For example, you can't make the default state be one thing on one context and another thing in a different context. Nor can you allow editors to choose the state of a newly created registration.

Furthermore, it leads to unexpected behaviors because the code like that in Registration::preSave() that uses getState() or getWorkflow() will behave in ways that are incompatible with any workflow or state that has been explicitly set on the entity before saving it for the first time.

The current code is:

 public function getWorkflow(): WorkflowInterface {
    if ($this->isNew() || $this->get('workflow')->isEmpty()) {
      return $this->getType()->getWorkflow();
    }
    else {
      return $this->workflow->entity;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getState(): StateInterface {
    $workflow = $this->getWorkflow();
    if ($this->isNew() || $this->get('state')->isEmpty()) {
      return $workflow->getTypePlugin()->getState($this->getType()->getDefaultState());
    }
    else {
      return $workflow->getTypePlugin()->getState($this->get('state')->first()->value);
    }
  }

Proposed resolution

Instead we should do something like this:

/**
 * {@inheritdoc}
 */
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
  parent::preCreate($storage_controller, $values);
  $values['workflow'] = $this->getType()->getWorkflow()->id();
}

public function getWorkflow(): WorkflowInterface {
    if ($this->get('workflow')->isEmpty()) {
      return $this->getType()->getWorkflow();
    }
    else {
      return $this->workflow->entity;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getState(): StateInterface {
    $workflow = $this->getWorkflow();
    if ($this->get('state')->isEmpty()) {
      return $workflow->getTypePlugin()->getState($this->getType()->getDefaultState());
    }
    else {
      return $workflow->getTypePlugin()->getState($this->get('state')->first()->value);
    }
  }

Remaining tasks

User interface changes

None.

API changes

Possible change of behavior of newly created registrations in some edge cases.

Data model changes

None.

πŸ› Bug report
Status

Fixed

Version

3.1

Component

Registration Core

Created by

πŸ‡¬πŸ‡§United Kingdom jonathanshaw Stroud, UK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024