foreach is string

Created on 24 June 2025, 9 days ago

Drupal 11.2.
File: "unique_content_field_validation.module"

In function "unique_content_field_validation_validate_unique", app. line 248:

  $form_value = $form_state->getValue($field_name) ?? [];
  foreach ($form_value as $key => $values) {
    if (is_numeric($key)) {
      foreach ($values as $key_value => $value) {
        if (in_array($key_value, UniqueContentFieldValidationInterface::UNIQUE_CONTENT_FIELD_VALIDATION_VALID_KEY_VALUES)) {
          $items[$key] = $value;
        }
      }
    }
  }

The value "$values" is not always an array, but can be a string.
Therefore it makes a warning for:

      foreach ($values as $key_value => $value) {

...if $values is not an array.

A solution could be:

  $form_value = $form_state->getValue($field_name) ?? [];
  foreach ($form_value as $key => $values) {
-    if (is_numeric($key)) {
+    if (is_numeric($key) && is_array($values)) {
      foreach ($values as $key_value => $value) {
        if (in_array($key_value, UniqueContentFieldValidationInterface::UNIQUE_CONTENT_FIELD_VALIDATION_VALID_KEY_VALUES)) {
          $items[$key] = $value;
        }
      }
    }
  }

Perhaps it is important to set $tiems[$key]? - I don't know?

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇩🇰Denmark uv516 Denmark

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

Comments & Activities

Production build 0.71.5 2024