Entity State condition broken due to improper field check

Created on 10 January 2025, 11 days ago

Problem/Motivation

When using the State Machine: Entity State condition with a state field on an entity, the result is always FALSE.

Steps to reproduce

Create a state machine, and add the field to a content type. Set it to a specific value.

Create a model using this condition, and try to get it to work. It always fails, even when negated.

Proposed resolution

The bug is here:

    if ($entity instanceof FieldableEntityInterface) {
      $field_name = $this->tokenService->replaceClear($this->configuration['field_name']);
      $parts = explode('-', $this->configuration['state_id']);
      if (!empty($field_name) && count($parts) === 2 && $entity->hasField($field_name)) {
        $field = $entity->get($field_name);
        if (!($field instanceof StateItem)) {
          return FALSE;
        }

In the above code, $entity->get() returns a FieldItemList, not a StateItem. You can get the StateItem by adding ->get(0) to the $entity->get.

However, if you add it there, the later check to $field->getValue()[0]['value'] fails, because on that line it's using the API for a FieldItemList, not a StateItem.

A quick fix is simply changing line 35 to:
if (!($field->get(0) instanceof StateItem)) {

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

2.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States freelock Seattle

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024