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?
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | cshs-3270062-15.patch | 7.53 KB | joseph.olstad |
| #15 | cshs-3270062-14.patch | 688 bytes | lizuka |
| #14 | cshs-3270062-13.patch | 395 bytes | nitrocad |
| #12 | cshs-3270062-12.patch | 7.33 KB | ludo.r |
| #9 | 3270062-problem-filtering-first-9.patch | 7 KB | mrshowerman |
Issue fork cshs-3270062
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
Comment #2
drs2034 commentedHi, I'm having similar issue with. Any suggestions?
Comment #3
rosk0Faced 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 againstNULLis not going to work inDrupal\cshs\CshsOptionsFromHelper::validateSettingsForm()withif ((string) $id === $settings['parent']) {.Adding test only patch to confirm problem. Will work on the fix.
Comment #5
rosk0Initial patch version.
This allows to select top level terms as expected.
Comment #6
jasonflaherty commentedThe patch from #5 has been tested and working just as we needed here. Thanks for the patch.
Comment #7
steelstrung commentedIs 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
Comment #8
lroelsAs 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.
Is there a reason why we can't solve this issue by doing something as seen in the code above?
Comment #9
mrshowermanHere's a re-roll of #5 against 4.0.x.
Comment #10
wrd-oaitsd commentedI ran into the same issue today. #9 appears to resolve the issue.
Comment #11
shaunlaws commentedThe 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.
Comment #12
ludo.rPatch #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;Comment #13
nitrocad commentedCan you please re-roll this patch to the latest 4.x version?
Comment #14
nitrocad commentedI made a really simple but working solution. please test it.
Comment #15
lizuka commentedComment #16
joseph.olstadHmm, #15 looks like the simplest/easiest solution.
With that said, patch #12 had test coverage.
Comment #18
joseph.olstadKeep 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.