Problem/Motivation
From TermForm::buildEntity:
// Assign parents with proper delta values starting from 0.
$term->parent = array_keys($form_state->getValue('parent'));
With a normal select form element the result in form state usually would be [1 => '1', 5 => '5']
and in this case TermForm::buildEntity will process the values correctly, but if the form element for the parent property is disabled with '#disabled'
then the form state value for it would be [0 => '1', 1 => '5']
.
It is wrong to assume the keys are the proper field values...
Proposed resolution
Replace array_keys with array_values to ensure the proper field values are extracted and there will be proper delta order as well.
Remaining tasks
Do we need tests? It is a pretty simple issue...
User interface changes
none
API changes
none
Data model changes
none
Comment | File | Size | Author |
---|---|---|---|
#43 | 2637538-43.patch | 11.98 KB | alexpott |
#43 | 34-43-interdiff.txt | 552 bytes | alexpott |
#34 | 2637538-34.patch | 11.91 KB | tstoeckler |
#34 | 2637538-34--tests-only.patch | 11.34 KB | tstoeckler |
#2 | 2637538-2.patch | 580 bytes | hchonov |
Comments
Comment #2
hchonovComment #3
hchonovComment #4
farse CreditAttribution: farse at Zoocha commentedDo we not need to update the comment as well?
I have made that update.
Comment #5
radiancebox CreditAttribution: radiancebox commentedComment #6
radiancebox CreditAttribution: radiancebox commentedI've applied termform_buildentity-2637538-4.patch to branch 8.0.x.
-Patch applied cleanly
-Taxonomy remained functional
A more experienced tested may want to create their own form and test.
Comment #7
mikemiles86Comment #8
mikemiles86Comment #9
mikemiles86I have done some additional testing of this issue, using xdebug.
When a term is saved, the parent value in the $form_state is structured as @hchonov said:
When the 'parent' field is disabled by setting the '#disabled' attribute to TRUE (I completed this using a form alter):
Then the form state value for 'parent' is different. The keys are no longer the parent id's, but delta values.
So TermForm::buildEntity() will copy the delta values and not the parent ids in this instance.
I'm not sure if this patch is actually needed as the ability to set the parent term works without it and I think this is a very limited edge case. But it does take the array structure differences into account, handling both states (enabled and '#disabled') the same way.
RTBC?
Comment #10
alexpottThis bug looks testable.
Comment #11
tstoecklerComment #13
hchonovComment #15
catchBumping this to major since it's a PHP error from using the standard UI.
Comment #16
chipway CreditAttribution: chipway at Chipway commentedtermform_buildentity-2637538-4.patch applies on 8.2.x.
Still needs tests.
Comment #17
lathantermform_buildentity-2637538-4.patch does not allow creation of the term even with the patch applied. And still get the error "Warning: array_values() expects parameter 1 to be array, null given in Drupal\taxonomy\TermForm->buildEntity() (line 113 of core/modules/taxonomy/src/TermForm.php)." when creating the new term.
Comment #19
k4v CreditAttribution: k4v as a volunteer commentedComment #20
abu-zakham CreditAttribution: abu-zakham at Vardot commentedHello, Patch #4 doesn't fix the problem, May this patch fix the problem.
Comment #22
abu-zakham CreditAttribution: abu-zakham at Vardot commentedComment #23
hchonovWhy would that not be set? There is only one possibility and it is that a contrib module is doing overrides, but in this case it should take care of setting the form state value "parent".
In which case is the previous patch not working for you? Could you describe it or provide a test case?
Comment #25
robotjox CreditAttribution: robotjox commentedFor what it's worth I can confirm that abu-zakham's code:
solves the problem I had of terms not being added (or shown). The patch did not.
Working on a drupal 8.x.4-dev. Migrated from Drupal 6 to 8. Lots of contributed modules.
I was redirected here from https://www.drupal.org/node/2816497#comment-11810162
Comment #26
cverde CreditAttribution: cverde as a volunteer commentedThis needs a test cover the edge case situation.
One more minor thing, there should be a space after the "if".
Comment #27
diogo_plta CreditAttribution: diogo_plta commentedI´m with the same error:
Drupal 8.4.4
php 7.1
* migrated content from D6 to D8
When add a taxonomy term appears to be ok, but the term is not listed and return this warning.
Sugestion #25, worked for me!
Comment #28
jeetendrakumar CreditAttribution: jeetendrakumar as a volunteer and at HyTechPro.com commentedComment #29
jeetendrakumar CreditAttribution: jeetendrakumar as a volunteer and at HyTechPro.com commentedAs per #26 suggestion. Update the patch file.
Comment #30
diogo_plta CreditAttribution: diogo_plta commentedFixed the problem. thank you.
Comment #32
mohit1604 CreditAttribution: mohit1604 at Google Summer of Code commentedAdding test for version 8.5.x and 8.6.x in patch #29
Comment #33
othermachines CreditAttribution: othermachines commentedI don't think this is a bug.
According to W3C:
(A successful control is "valid" for submission.)
This means that if you implement
hook_form_alter
to set the 'disabled' attribute on your form element, you should not expect that element to be processed on the server when the form is submitted.That being said, although there *may* be something improper about the way this is being handled, the end result (I think) is the correct one.
So what to do if you want to make a dropdown select non-selectable and yet still be "successful"? That is a complicated question (and obviously not specific to Drupal). See: (Stack Overflow) HTML form readonly SELECT tag/input
As an extra FYI, there's a bit of documentation in FormBuilder::handleInputElement that explains Drupal's handling of 'disabled' and 'readonly':
Cheers -
Comment #34
tstoecklerOK, so read through the issue and here's my thoughts:
#default_value
key.So this issue has been derailed for a while and really just needs an automated test.
So this now provides a test. Unfortunately I didn't find a test which deals with parents through the UI at all, so I wrote one which at least tests the basic functionality of adding and editing parents before testing with a disabled parents form element. This revealed that the order of parents (i.e. the field item list deltas) will be different for terms with multiple parents depending on whether or not the parent form element is disabled or not. This is a bit quirky, but not really a bug, so the test explicitly tests this for now, so if we ever decide to make this consistent, we can update the test directly.
The test-only patch is also the interdiff, which is relative to #2.
Comment #36
OptimusPrime23 CreditAttribution: OptimusPrime23 commentedThe issue is still there. I'm using Drupal 8.5.4. I've used migrate module to migrate my existing vocabularies from drupal7. The vocabularies gets created. But, when you try to add terms i'm getting the message, term is saved but with following warnings
And the terms that got created are missing from the list. Tried applying #34's patch. But its still failing
Comment #37
hottaco CreditAttribution: hottaco commentedI upgraded from 6 to 8.5
Currently running 8.5.4 along with php 7.0
Warning: array_keys() expects parameter 1 to be array, null given in Drupal\taxonomy\TermForm->buildEntity() (line 118 of core/modules/taxonomy/src/TermForm.php) #0
Comment #39
tstoecklerRe #36. I think you are hitting a similar - but different - issue. It seems as though the parent field is completely removed in your case, not just disabled. Maybe you can open a new issue and add some steps to reproduce your error as otherwise it will be hard to provide any help.
Comment #42
geaseI believe it's time to set it to RTBC. Arguments laid out in #34 seem valid to me, patch applies to 8.9.x, fixes the issue, and provides the overall test coverage for term parents. Just the minor thing - shouldn't be the test class called TermParentTest instead of TermParentsTest? Drupal always sticks to singular.
Comment #43
alexpottFixing the test defaultTheme deprecation
Comment #44
alexpottSo @jeetendrakumar and @abu-zakham are not credited because as per #34 the patches were fixing another issue entirely.
Credited @mikemiles86 for a reviewing the issue.
I think we should apply this to 8.8.x as a bug fix asking another committer for a +1.
Comment #47
hchonov@alexpott, thank you for taking the time to fix the test!
Comment #48
alexpottDiscussed with @catchwho +1'd to 8.8.x backport