Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
When doing a simple EQ on a top-level array property of a config entity, this warning is given:
<b>Warning</b>: mb_strtolower() expects parameter 1 to be string, array given in <b>/home/andrei/work/d8.dev/core/lib/Drupal/Component/Utility/Unicode.php</b> on line <b>295</b>
Proposed resolution
Fix it :)
Comment | File | Size | Author |
---|---|---|---|
#9 | 2248567-9-do-not-test.patch | 3.04 KB | amateescu |
Comments
Comment #1
amateescu CreditAttribution: amateescu commentedHere's a test-only patch that shows the problem.
Comment #2
amateescu CreditAttribution: amateescu commentedScratch that, the test data has to be even simpler.
Comment #4
amateescu CreditAttribution: amateescu commentedThat test class makes it sooo difficult to add something to it without having to update all the assertions. This one should be more clear about the bug at hand.
Comment #7
amateescu CreditAttribution: amateescu commentedOk, nevermind me, the
property.*
syntax seems to work.Comment #8
amateescu CreditAttribution: amateescu commentedDiscussed a bit with chx. Doing this would mean we need to load config schemas to know that the condition field holds an array.
Comment #9
amateescu CreditAttribution: amateescu commentedAnd here's a first shot at it, also from chx. Tested locally and not fully working, but it's a start at least :)
Comment #10
chx CreditAttribution: chx commentedThe title is wrong: content entities can't do this because content entities can't store an array. It is sensible that a
foo: [1, 2, 3]
be matched withcondition('foo', 1)
that's for sure but there's nothing similar in content entities .Comment #11
BerdirWell, content entities don't have arrays, but they have list of values, condition('tags.target_id', 5) with a multi-value tags reference field is a very similar situation as far as the API is concerned?
Comment #12
chx CreditAttribution: chx commentedThere's a huge difference between $node->field[0]->target_id $node->field[1]->target_id and foo: [1, 2, 3]. Once again: the expectation is expected :) so this might be a bug but there's nothing in content mirroring this.
Comment #13
chx CreditAttribution: chx commentedComment #14
chx CreditAttribution: chx commentedComment #21
pfrenssenTo make it clear, here is the correct way of adding an array condition to an entity query for a config entity.
This is wrong, it throws "mb_strtolower() expects parameter 1 to be string, array given":
Instead do this:
Comment #22
hchonov@pfrenssen, thank you for providing the example.
However it is very confusing for developers if an entity query for content entities and config entities has to be used differently when searching for a value in an array (config entities) / multiple field (content entities). I think that we could offer an consistent way for such queries.
Comment #23
jhedstromI wonder if an
assert
could be added somewhere to provide better feedback to developers if the incorrect approach outlined in #21 is used instead of the correct approach (eg, assert the value is a string not an array.)Comment #26
hadros CreditAttribution: hadros as a volunteer commented@pfrenssen thanks man