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
This function currently always use ->value
.
For entity reference items, this is just NULL and you would have to use ->target_id
Proposed resolution
Use \Drupal\Core\Field\FieldItemBase::mainPropertyName
to fetch the right value.
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#40 | 2621504-1.patch | 890 bytes | jibran |
Comments
Comment #2
dawehnerWorking on it now
Comment #3
dawehnerHere is a first bugfix
Comment #5
dawehnerThere we go
Comment #6
mpdonadioI think this needs some comments as to why you are doing the view setup yourself and not using the one from ViewKernelTestBase.
Comment #7
dawehnerHere is one.
Comment #8
mpdonadioChange looks good, and should future proof fields that are implemented in similar ways. Test looks good and valid. Verified test fails w/o the patch.
Comment #10
alexpottWe only need to get the main_property_name for one side of the ternary so lets just use an
if
statement instead.Do have test coverage of when the cardinality is 1 and when it is higher?
Comment #11
dawehnerWell, we probably render a node title and an image field.
\Drupal\rest\Tests\Views\StyleSerializerTest::testFieldapiField
uses certainly cardinality 1Comment #12
morsokThis patch helped me to get a value from a color field (see #2701933: Declare the main property name for related informations).
Looks good to me, maybe the title should change from "needs to support entity reference items" to "needs to support field which main value is not 'value'".
Comment #13
timmillwoodComment #14
mpdonadioThis doesn't apply against 8.1.x. Needs reroll.
Comment #15
mpdonadioLet see.
Comment #16
dawehner@mpdonadio Do you think the patch looks more or less sane beside of that?
Comment #17
dawehnerComment #18
mpdonadio#16, I think so. You can't interdiff them b/c git output the hunks in a different order, but they all look the same. The conflict was b/c the namespace change in #2304461: KernelTestBaseTNG™ for KernelTestBase. It was an easy merge once I realized that.
Comment #19
dawehnerYeah I think its totally fine for you to RTBC the patch :)
Comment #20
tstoecklerFieldItemInterface::mainPropertyName()
can returnNULL
so would be nice to add a check for that. I don't think we need to support a property named0
soif ($main_property_name = $field_item->mainPropertyName()) { ... }
should be fine.Comment #21
dawehnerHaha, I hope the same.
Here is an update of the patch.
Comment #22
jibranDER has this in DynamicEntityReferenceItem.php
So this will generate an error here.
Comment #23
dawehnerNo, it won't. We now check for the main property and return NULL instead. I'm sorry, but if the field type doesn't return anything useful we cannot guess what to render. Decide on one: 'target_id' IMHO.
Comment #24
LendudeWith the current logic, if the cardinality == 1 but both the if and the elseif are false, you will fall through to the cardinality > 1 logic and return an array (possibly an empty one).
Shouldn't the cardinality == 1 logic include an else, and return something other then an array, maybe the original $field_item_list->value fallback?
Comment #25
dawehnerNice point! This allowed us to refactor the code and makes it actually much nicer
Comment #26
mpdonadioDoes #24 and #25 mean that we have a test coverage problem? Maybe the one mentioned in #10 didn't cover this for some reason?
Comment #27
dawehnerHere is a better test for it.
Comment #29
dawehnerComment #30
jibranLet me create an issue in DER and discuss it with ER maintainers as well.
Comment #32
dawehner.
Comment #33
Lendude@dawehner oh yeah this now looks way better. Also the new test addresses @alexpotts comment in #10, so nice!
The first comment isn't quite right anymore, there is something we can do, namely 'take a guess'. Maybe combine the two comments into one with something like:
Find the value using the main property of the field. If no main property is provided fall back to 'value'.
Otherwise, the second comment contains two grammatical errors: its => it's and propery => property
Nitpicking aside, this looks ready.
Comment #34
dawehnerGood idea
Comment #35
LendudeThanks!
Comment #38
catchCommitted/pushed to 8.2.x and cherry-picked to 8.1.x. Thanks!
Comment #39
jibranSorry for being late here but I have a question.
Instead of all this why not just use
$field_item->getValue()
here?Comment #40
jibranSomething like this.
Comment #42
dawehnerThank you @jibran
I think we could replace
with
. Do you think this would make sense?
Comment #43
dawehner@jibran
Let's take that onto a followup!