Problem/Motivation

If a multi-value field is set to require the deepest selection, the user can bypass this requirement by adding multiple terms. The problem is that the TID being passed changes to a space delimited string of multiple TID values which causes an invalid result when trying to load the taxonomy term entity.

The fix from https://git.drupalcode.org/project/shs/-/commit/00a31ecbf5ade69ac4a2734e... prevents this problem from causing a system failure but it also seems to bypass the “force deepest” check.

Steps to reproduce

This can be done on RC3.

  • My test user does not have the “administer taxonomy” or “view unpublished terms in…” permissions
  • My Content type’s entity reference field is set to “taxonomy term”
  • The field is not required
  • The field form display is set to “Force selection of deepest level”
  • The field allows multiple values
  • Add two different top level terms, note that a child term field appears, leave it set to None, and save the content

Proposed resolution

I'm not really sure when this should be caught. By the time this gets to shs_term_has_children($tid) it's probably already too late.

CommentFileSizeAuthor
#6 3273035-6.patch617 bytestrackleft2

Issue fork shs-3273035

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

inversed created an issue. See original summary.

inversed’s picture

Issue summary: View changes

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

trackleft2’s picture

Status: Active » Needs review

I've encountered an issue when selecting multiple terms, for example, with IDs 2 and 3, using the Simple Hierarchical Select (SHS) widget. The form field value for these selections is concatenated into a single string: '2 3'. This format seems to be directly influenced by the cardinality setting of the field. Specifically:

When the field's cardinality is set to allow only a single value, the expected format is a simple string or integer representing a single term ID.
For fields allowing multiple values, it appears there's an expectation for an array of IDs. However, SHS concatenates multiple term IDs into a single string.
To align with the expected data format, especially when handling multiple selections, I propose a temporary adjustment to the data shape by splitting the string based on spaces:

if (is_string($value)) {
  $value = explode(' ', $value);
}

This modification would transform the concatenated string '2 3' into an array [2, 3], making it consistent with the expected input format for fields allowing multiple values. The current handling inadvertently wraps the string into an array like [2 3], which leads to validation errors.

See the merge request here: https://git.drupalcode.org/project/shs/-/merge_requests/26/diffs

trackleft2’s picture

StatusFileSize
new617 bytes
rozzi’s picture

The patch doesn't seem to help even after clearing the cache. Using 3.0.0-beta5 of SHS and running Drupal 11.3.8. I also have the Select list values incorrectly truncated patch applied.

The patch I tested for 3.0:

--- a/src/Plugin/Field/FieldWidget/OptionsShsWidget.php
+++ b/src/Plugin/Field/FieldWidget/OptionsShsWidget.php
@@ -355,6 +355,9 @@
       return;
     }
     $value = $element['#value'];
+    if (is_string($value)) {
+      $value = explode(' ', $value);
+    }
     if (!is_array($value)) {
       $value = [$value];
     }