Problem/Motivation

I want to use CSHS to limit the selection to the first level.
When configuring in the field (Manage Form Display) I always get the error: "The hierarchy depth cannot be 1 because the selection list has 0 levels."

What am I doing wrong?

Steps to reproduce

My settings:
Parent: (none)
Hierachy depth: 1
Required depth: 1

Proposed resolution

Looking at the source at CshsOptionsFormHelper.php in validateSettingsForm it seems to me, that all options are removed from the list, because no parent is selected. Shouldn't there be a check that the list of options is only filtered if a parent is also specified?

Issue fork cshs-3270062

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

RoKi created an issue. See original summary.

drs2034’s picture

Hi, I'm having similar issue with. Any suggestions?

rosk0’s picture

Version: 8.x-3.5 » 8.x-3.x-dev
Component: User interface » Code
Assigned: Unassigned » rosk0
Status: Active » Needs review
Related issues: +#3188299: Add validation for the `hierarchy_depth`
StatusFileSize
new4.06 KB

Faced with this as well.

It looks like the validation logic added in #3188299: Add validation for the `hierarchy_depth` doesn't count the fact that empty option '- Please select -' has value of string 0 ('0') and so doing type strict check against NULL is not going to work in Drupal\cshs\CshsOptionsFromHelper::validateSettingsForm() with if ((string) $id === $settings['parent']) {.

Adding test only patch to confirm problem. Will work on the fix.

Status: Needs review » Needs work

The last submitted patch, 3: cshs-3270062-3-test-only.patch, failed testing. View results

rosk0’s picture

Assigned: rosk0 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new7.34 KB
new3.29 KB

Initial patch version.

This allows to select top level terms as expected.

jasonflaherty’s picture

The patch from #5 has been tested and working just as we needed here. Thanks for the patch.

steelstrung’s picture

Is this truly fixed? I am using the 8.x-3.6 version and I am having the same issue trying to restrict users to only select from the first level of terms.
Maybe I am not interpreting correctly, but neither release shows that it was updated in September.

Taxonomy of depth 2
Form display:
- Widget: Client side hierarchical select
- Parent: - Please select - (using the root)
- Hierarchy depth: 1

I get red data validation outline around Hierarchy depth and an error:
"The hierarchy depth cannot be 1 because the selection list has 0 levels."

I attempted to use the patch after adding cweagans/composer-patches and updating my composer.json, but composer update was unable to apply the patch:
Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2022-06-27/cshs-3270062-5.patch

I didn't see the release updated in June 2022:

8.x-3.6 Stable release covered by the Drupal Security Team released 26 April 2022
Works with Drupal: ^8 || ^9
✓ Recommended by the project’s maintainer.
Development version: 8.x-3.x-dev updated 28 Jan 2022 at 12:51 UTC

lroels’s picture

As stated in #7, this patch does not apply anymore.
Is the foreach loop even needed when the parent setting is not filled in?

I tested this locally by adding a check on the parent field and skip the loop if this is not filled.
This seems to work without any issues.

    if (!empty($settings['parent'])) {
      foreach ($options as $id => $option) {
        // This always removes at least the first item, which is what we
        // want. If a user selects nothing we remove the `- Please select -`
        // and count only the number of nesting levels. In another case,
        // we remove everything before and including the selected item and
        // count the rest.
        unset($options[$id]);
        // Leave the rest of the list after the selected option.
        if ((string) $id === $settings['parent']) {
          break;
        }
      }
    }

Is there a reason why we can't solve this issue by doing something as seen in the code above?

mrshowerman’s picture

Version: 8.x-3.x-dev » 4.0.x-dev
StatusFileSize
new7 KB

Here's a re-roll of #5 against 4.0.x.

wrd-oaitsd’s picture

I ran into the same issue today. #9 appears to resolve the issue.

shaunlaws’s picture

The patch in #7 solved the issue that I was seeing with a 4-level taxonomy in 4.0.0 with the following settings:

Parent: (none)
Hierarchy depth: 3
Required depth: 1

I am no longer seeing the error message "The hierarchy depth cannot be 3 because the selection list has 0 levels." that I was seeing without the patch.

ludo.r’s picture

StatusFileSize
new7.33 KB

Patch #5 works for version 8.x-3.6.

However if you work with an older version of PHP (7.3 in my case) it's broken.

Here's a version where I remove the typed property to make it work: protected ?int $depth; => protected $depth;

nitrocad’s picture

Can you please re-roll this patch to the latest 4.x version?

nitrocad’s picture

StatusFileSize
new395 bytes

I made a really simple but working solution. please test it.

lizuka’s picture

StatusFileSize
new688 bytes
joseph.olstad’s picture

StatusFileSize
new7.53 KB

Hmm, #15 looks like the simplest/easiest solution.

With that said, patch #12 had test coverage.

joseph.olstad’s picture

Keep in mind, the HEAD of 4.0.x isn't passing tests at this time. There was some D11 refactoring done to the tests due to deprecated PHPUnit code and so far the refactoring hasn't been straight forward and isn't completed in HEAD.