Problem/Motivation

Creating an entity that has a reaction field fails validation unless it goes through the widget:

field_reaction.0.status: This value should not be null.

You hit this whenever the entity is created in code with Entity::create(). The form is fine because the widget submits a status.

What is going on: status is a required property and isEmpty() always returns FALSE, so the field only works if something guarantees a default value. Nothing does. A field added through the UI has an empty default value, core appends one blank item to it on create, the item never reports empty, and the required status fails.

Core's comment field has the same required status and the same isEmpty(), and it guarantees the value in two places: comment_field_config_create() writes a default value with status open onto every new comment field instance, and CommentFieldItemList::get() builds item 0 from that default for an entity that has nothing stored.

Steps to reproduce

  1. Add a reaction field to a content type through the UI. Leave the default value empty.
  2. $node = Node::create(['type' => 'article', 'title' => 'Hello']);
    var_dump($node->get('field_reaction')->status); // NULL
    var_dump($node->validate()->count()); // 1

Proposed resolution

Do what core comment does.

  • hook_field_config_create(): a new reaction field instance gets a default value of status open unless one was configured.
  • A list_class, VotingApiReactionFieldItemList, that reads item 0 from the field's default value when nothing is stored, and applies that default on create. A field instance created before this change has no default configured; the list falls back to open for it, so existing sites need no config update.

A kernel test creates a field with no default value and an entity in code, and checks the status is open and validation passes. Without the fix the status is NULL.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

loze created an issue. See original summary.

loze’s picture

Status: Active » Needs review

!16 does what core's comment field does for the same situation.

votingapi_reaction_field_config_create() gives every new reaction field instance a default value of status open, unless one was configured, and skips config sync. VotingApiReactionFieldItemList is now the field type's list_class: it reads item 0 from that default when an entity has nothing stored, and applies it on create. An instance created before this change has no default configured, so the list falls back to open for it and existing sites need no config update.

DefaultStatusTest creates a field with no default value and an entity with EntityTest::create(), then checks the status is open and validation passes. On 1.x without the fix the status is NULL and validation fails. With the fix it passes, and phpcs is clean on the four files.

Some of this work was done with help from an AI coding agent. I have gone through it and reviewed it personally.

elaman made their first commit to this issue’s fork.

elaman’s picture

Status: Needs review » Fixed

Thanks for the fix! I reproduced the validation error when creating an entity programmatically and confirmed that the MR resolves it.

I also added tests for existing fields without a configured default and fields configured with a closed default. The targeted tests pass on Drupal 10.6 and 11.4.

Merging and closing as fixed.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.