Split from #2846614: Incorrect field name is used in views integration for multi-value base fields.
Problem/Motivation
From \Drupal\views\EntityViewsData::mapFieldDefinition().
// @todo Introduce concept of the "main" column for a field, rather than
// assuming the first one is the main column. See also what the
// mapSingleFieldViewsData() method does with $first.
When building the entity views data, the class identifies the main property/column of a field as the first column from field schema. This has been designed early when we had no "main property" API in place.
Proposed resolution
Fix the @todo by using \Drupal\Core\Field\FieldStorageDefinitionInterface::getMainPropertyName().
Remaining tasks
None.
User interface changes
None.
API changes
None.
Data model changes
None.
Comments
Comment #2
claudiu.cristeaPatch.
Comment #3
claudiu.cristeaComment #4
tstoecklerLooks good to me, not sure if the argument change in
mapSingleFieldViewsData()is considered an API change, though. I would tend to say "yes", so maybe we just have to deprecate the parameter? Not sure though.Comment #5
claudiu.cristea@tstoeckler, according to https://www.drupal.org/core/d8-bc-policy#protected-methods
It's not a BC break. Let's RTBC it if otherwise looks OK for you and see what committers are saying,
Comment #6
claudiu.cristeaOK, here's a version that doesn't change the signature, except that is renaming the local variable
$firstto$is_main_propertybut this is not a BC break. Also, the value passed there is the same as in HEAD meaning that there's no risk if somebody has extendedmapSingleFieldViewsData().Comment #8
dawehnerI'm wondering whether there should be a test with multiple fields, where the main property is not the first available one?
Comment #9
berdirAlso note that it is possible that a field type has no main property, what happens then? Not sure what the current state is, but DER for example doesn't really have a main property, neither entity_id nor entity_type make sense on their own, and what I discussed with @jibran is that it should return NULL.
Comment #10
claudiu.cristea@Berdir, In this use case, we need
::getMainPropertyName()only to append or not a suffix to the views field column title. Looking at the patch code, when::getMainPropertyName()returns NULL, all columns will get the suffix. And I think this what we want.@dawehner, added the test. The test-only patch is interdiff.
Comment #12
jibranIt is true that conceptually DER shouldn't have the main property but to support Entity field rendering for views and EFQ we started using target_id as main property in #2818261: Add target_id as the main property just like ERItem.
In core, if you want to test multiple properties then shape field type can be used.
Comment #13
claudiu.cristeaRevert unneeded changes. Fix docs.
Comment #14
dawehnerIt would be great to get feedback from @jibran / @berdir whether they believe the NULL prefix solves it. Personally I would cast NULL explicit to an empty string at least though.
I'd have used "Check that all other properties ..." it just reads nicer for some reason :)
Comment #15
jibranOnce the patch is ready I can test it against https://github.com/jibran/dynamic_entity_reference. DER has a bunch of tests related to views data.
Comment #17
claudiu.cristea@dawehner, Thank you for review. I don't understand exactly about what prefix are you talking. I think the code is clear:
If
$main_propertyis NULL, then$is_main_propertywill evaluate to FALSE (note, I used === strict comparison). In this case all field's column titles will be suffixed with " ({column})". But isn't this the expected behavior when no column is the main column?I added the docs fix. I changed the placeholder representation to use the curly brackets and used the term "column" which seems more appropriate in the context of field schema. (IMO this should be subject of a future field type terminology consolidation: 'column' vs. 'property').
@jibran It would be nice if you can run a test with this patch.
Comment #18
Munavijayalakshmi commentedRerolled the patch.
Comment #23
joachim commentedGoing by #17, it looks like this needs approval from a maintainer rather than a general review.
Comment #31
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.