Problem/Motivation

You can define bundle fields on your entity in bundleFieldDefinitions(), you may need a patch like #2817751: Create an API for bundle plugins or at the very least a class that extends BaseFieldDefinition and sets isBaseField to FALSE. See Drupal\entity_test\FieldStorageDefinition as an example.

These bundle fields are not added into the views data in EntityViewsData.

Proposed resolution

Add the fields.

Remaining tasks

Discuss the solution, add tests.

CommentFileSizeAuthor
#85 13189 on 11.x.patch17.57 MBgiuseppe87
#77 2898635-77.patch7.57 KBvoleger
#65 4953.patch7.02 KBjon pugh
#60 2898635-60.patch5.47 KBsander wemagine
#58 2898635-58.patch2.99 KBsander wemagine
#48 2898635-48.patch7.05 KBcatch
#48 2898635-48-interdiff.txt1.15 KBcatch
#47 2898635-47.patch6.68 KBcatch
#47 interdiff-2898635-46-47.txt587 bytescatch
#46 2898635-46.patch6.68 KBcatch
#46 interdiff-2898635-45-46.txt631 bytescatch
#45 2898635-45-stacked.patch6.29 KBcatch
#45 2898635-45.patch2.94 KBcatch
#45 2898635-45-stacked.patch6.29 KBcatch
#44 2898635-44-stacked.patch6.29 KBcatch
#44 2898635-44.patch2.94 KBcatch
#43 2898635-43-stacked.patch4.58 KBcatch
#43 2898635-43.patch1.22 KBcatch
#42 2898635-42-combined.patch13.69 KBcatch
#41 2898635-41.patch2.94 KBcatch
#41 2898635-41-combined.patch12.95 KBcatch
#41 2898635-interdiff.txt2.49 KBcatch
#40 2898635-40.patch1.22 KBcatch
#40 2898635-40-combined.patch11.23 KBcatch
#38 2898635-38.patch11.26 KBcatch
#33 interdiff_30-32.txt2.56 KBdwkitchen
#33 2898635-32_post-2930736-32_bundleFieldDefinitions-views.patch1.03 KBdwkitchen
#33 2898635-32_inc-2930736-32_bundleFieldDefinitions-views.patch11.22 KBdwkitchen
#30 2898635-30.drupal.bundleFieldDefinitions-are-not-added-in-EntityViewsData-11-reroll.patch2.02 KBjoachim
#28 2898635-28.patch8.9 KBluisnicg
#27 2898635-27.patch8.9 KBbradjones1
#27 interdiff-26-27.txt675 bytesbradjones1
#26 2898635-26.patch8.79 KBbradjones1
#22 2898635-22.patch8.68 KBcatch
#22 2898635-22-interdiff.txt758 bytescatch
#16 views-code_bundle_fields-2898635-16.patch8.62 KBplach
#11 2898635-11.drupal.bundleFieldDefinitions-are-not-added-in-EntityViewsData.patch2.01 KBjoachim
#4 interdif.txt916 bytesbenjy
#4 2898635-3.patch1.37 KBbenjy
#2 2898635-2.patch1.42 KBbenjy

Issue fork drupal-2898635

Command icon 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

benjy created an issue. See original summary.

benjy’s picture

Status: Active » Needs review
StatusFileSize
new1.42 KB

This patch works with a custom entity for me. See what the bot says.

  • I don't think we should rely on the getBundleEntityType() if we can help it, you shouldn't be required to have a entity type for bundles although it barely works without them right now.
  • setName and setTargetEntityTypeId are required to translate the config definitions into storage definitions. You can see similar here: https://github.com/fago/entity/pull/49/files#diff-2cdf1c6f5f084a1288d552...

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

benjy’s picture

StatusFileSize
new1.37 KB
new916 bytes

This is better.

Status: Needs review » Needs work

The last submitted patch, 4: 2898635-3.patch, failed testing. View results

kristiaanvandeneynde’s picture

You could try the following to account for bundles properly:

    // Add any bundle fields defined in code.
    if (isset($this->entityType->getKeys()['bundle'])) {
      $bundle_field_definitions = [];
      foreach ($this->entityManager->getBundleInfo($this->entityType->id()) as $bundle_id => $bundle_info) {
        $bundle_field_definitions += $this->entityManager->getFieldDefinitions($this->entityType->id(), $bundle_id);
      }

      $field_definitions += $bundle_field_definitions;
    }

Problem is this causes a crash because EntityViewsData claims it's using field definitions on line 252 whereas it's using that variable for both FieldDefinitionInterface[] and FieldStorageDefinitionInterface[] interchangeably, which causes the above code to crash because it may end up (correctly) providing FieldConfig instances, which implement FDI but not FSDI.

kristiaanvandeneynde’s picture

Also keep in mind we might start seeing duplicate entries because configurable fields (FieldStorageConfig/FieldConfig) are already being dealt with in views_field_default_views_data().

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joachim’s picture

> Also keep in mind we might start seeing duplicate entries because configurable fields (FieldStorageConfig/FieldConfig) are already being dealt with in views_field_default_views_data().

We definitely will see something, as Field module declares config fields via hook_entity_bundle_field_info(): https://api.drupal.org/api/drupal/core%21modules%21field%21field.module/...
Not sure if that'll mess up Views data, or just be a case of doing the work twice.

Either way, we need to do one of:

- remove views_field_default_views_data(), and consider that config fields are now handled in EntityViewsData
- add a filter to this patch so that config fields aren't handled here. In which case, I think we possibly *aren't* blocked by #2930736: EntityViewsData assumes BaseFieldDefinitions where it should use FieldDefinitionInterface

joachim’s picture

Status: Needs work » Needs review
StatusFileSize
new2.01 KB

Here is a patch.

I've implemented @kristiaanvandeneynde's suggestion from #6

I've also added a filter to the bundle fields to skip those which don't implement both FieldDefinitionInterface and FieldStorageDefinitionInterface.

The benefits of this:

- sidesteps the problem of #2930736: EntityViewsData assumes BaseFieldDefinitions where it should use FieldDefinitionInterface as an interim measure.
- config fields get skipped, so we also don't need to figure out that problem just yet.
- Entity module's BundleFieldDefinitions are added (#2922784: Expose bundle fields from bundle plugin types to views)
- bundle fields from #2346347: Finalize API for creating, overriding, and altering code-defined bundle fields when it lands should work.

Status: Needs review » Needs work

The last submitted patch, 11: 2898635-11.drupal.bundleFieldDefinitions-are-not-added-in-EntityViewsData.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

joachim’s picture

Test failure is 'Invalid argument supplied for foreach()', coming from:

      foreach ($this->entityManager->getBundleInfo($this->entityType->id()) as $bundle_id => $bundle_info) {

But AFAICT, getBundleInfo() always returns an array, even for config entities!

joachim’s picture

    if (isset($this->entityType->getKeys()['bundle'])) {
      $bundle_field_definitions = [];
      foreach ($this->entityManager->getBundleInfo($this->entityType->id()) as $bundle_id => $bundle_info) {

I think there might be another problem with this approach: is it legitimate to define bundle fields on an entity that has no bundles? E.g. a module might use the hook_entity_bundle_field_info() to put bundle fields on everything, and that would include entity types like 'user'.

The code would skip that field, because the user entity has no bundle key, though getBundleInfo() will return a single bundle.

joachim’s picture

> But AFAICT, getBundleInfo() always returns an array, even for config entities!

As seen with the failures in #2930736: EntityViewsData assumes BaseFieldDefinitions where it should use FieldDefinitionInterface, it'll be methods on mocks that are returning NULL.

plach’s picture

Status: Needs work » Needs review
StatusFileSize
new8.62 KB

I'm trying to expose a code-defined bundle field of type date range and #11 does not work for me because it does not support hook_field_views_data() / hook_field_views_data_alter(). The attached patch seems to be working nicely.

Status: Needs review » Needs work

The last submitted patch, 16: views-code_bundle_fields-2898635-16.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

joachim’s picture

Status: Needs work » Needs review
+++ b/core/modules/views/src/EntityViewsData.php
@@ -321,6 +324,19 @@ public function getViewsData() {
+    // Add any bundle fields defined in code.
+    // @todo Here we are assuming that hard-coded bundle field definitions
+    //   extend BaseFieldDefinition, which is a common case. Revisit this logic
+    //   in https://www.drupal.org/project/drupal/issues/2930736.
+    $storage_definitions = $this->entityManager->getFieldStorageDefinitions($this->entityType->id());

I think we should be fixing #2930736: EntityViewsData assumes BaseFieldDefinitions where it should use FieldDefinitionInterface first.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

bradjones1’s picture

Title: bundleFieldDefinitions() are not added in EntityViewsData » [PP-1] bundleFieldDefinitions() are not added in EntityViewsData

Marking postponed by related issue.

catch’s picture

Issue tags: +Needs tests, +Needs re-roll
StatusFileSize
new758 bytes
new8.68 KB

Found a bug in the patch - it's not carrying settings over, which causes problems for entity reference fields (you lose the entity type and bundle settings, which falls back to 'node' from EntityReferenceItem).

Leaving postponed but uploading the patch for reference. Also this is against 8.7.x - whole patch needs a re-roll for 8.8.x. We should add explicit test coverage for entity reference here.

catch’s picture

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

avpaderno’s picture

Issue tags: -needs re-roll +Needs reroll
bradjones1’s picture

Status: Needs review » Needs work
Issue tags: -Needs reroll
StatusFileSize
new8.79 KB

Re-rolled #22 for 8.8.x.

bradjones1’s picture

StatusFileSize
new675 bytes
new8.9 KB

Needed a little more refactoring.

Tests still outstanding.

luisnicg’s picture

StatusFileSize
new8.9 KB

I was getting this error:

Call to undefined method Drupal\Core\Entity\EntityTypeManager::getFieldStorageDefinitions() in core/modules/views/src/EntityViewsData.php:363

I had to update the patch

from this:
$storage_definitions = $this->entityManager->getFieldStorageDefinitions($this->entityType->id());

to this:
$storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($this->entityType->id());

Hope this helps.

joachim’s picture

+++ b/core/modules/field/src/BaseFieldStorageConfigWrapper.php
@@ -0,0 +1,111 @@
+class BaseFieldStorageConfigWrapper extends FieldStorageConfig {

Revisiting this issue after a while, and I'm struck by the introduction of this wrapper class. Why do we need it? Could we not just add the methods we need to FieldStorageConfig? The whole point of the unified field system is that base fields and config fields behave the same way and consumers of those fields can ignore the detail of their definition and storage. So it seems like a core smell that we need a wrapper like this!

joachim’s picture

I needed an update of this for 8.8, and I've not had time to delve into why patches after #11 add loads of extra stuff, so here's a reroll of #11.

I was about to write tests for this too, and then I hit what I now remember is one of the main reasons (IMO at least) that all the various issues around views data are stuck: the tests are a nightmare to change, because it's a unit test which therefore mocks EVERYTHING. Which means that any change in what EntityViewsData gets from services or entity storage means the mocking in the test needs to be changed, and it's a huge barrier, both because it's difficult, and because it's scary (changing both tests and SUT at the same time never feels good).

So I think actually we need a separate issue to replace the EntityViewsData unit test with a kernel test: create test entity classes (like system module has, or use those, or add to those), get the result. Test doesn't need to care about any of the plumbing in between the entity field definitions and the result.

joachim’s picture

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

dwkitchen’s picture

While we are waiting for the tests I have made two patches, one including #2930736: EntityViewsData assumes BaseFieldDefinitions where it should use FieldDefinitionInterface and one post applying.

I have also made a minor change to use $this->entityType->hasKey('bundle')

@joachim agree about the Wrapper.

avpaderno’s picture

Status: Needs work » Needs review

The last submitted patch, 30: 2898635-30.drupal.bundleFieldDefinitions-are-not-added-in-EntityViewsData-11-reroll.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

catch’s picture

Issue tags: +Bug Smash Initiative
StatusFileSize
new11.26 KB
joachim’s picture

This should be postponed on #3116481: Convert EntityViewsDataTest from a unit test to a kernel test, as that rewrites the tests.

catch’s picture

StatusFileSize
new11.23 KB
new1.22 KB

Splitting so there's a combined patch with the blocking issue as well as the actual changes in this one.

catch’s picture

StatusFileSize
new2.49 KB
new12.95 KB
new2.94 KB

Bundle info isn't on the entity type manager.

catch’s picture

StatusFileSize
new13.69 KB

This should be a bit better.

catch’s picture

StatusFileSize
new1.22 KB
new4.58 KB

Re-roll - patch from this issue, and also the stacked version alongside #2930736: EntityViewsData assumes BaseFieldDefinitions where it should use FieldDefinitionInterface.

catch’s picture

StatusFileSize
new2.94 KB
new6.29 KB

Something went wrong with the re-roll, trying again.

catch’s picture

StatusFileSize
new6.29 KB
new2.94 KB
new6.29 KB

Even more wrong.

catch’s picture

StatusFileSize
new631 bytes
new6.68 KB

Finding actual issues with the patch now.

catch’s picture

StatusFileSize
new587 bytes
new6.68 KB

Oof.

catch’s picture

StatusFileSize
new1.15 KB
new7.05 KB

More real issues with the patch.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

rob230’s picture

Status: Needs review » Needs work

This patch prevents "List (text)" fields from working. They appear as a StringFilter instead of a ListField.

It can be fixed by removing this code:

// Add any bundle fields defined in code.
if ($this->entityType->hasKey('bundle')) {
  $bundle_field_definitions = [];
  foreach ($this->entityTypeBundleInfo->getBundleInfo($this->entityType->id()) as $bundle_id => $bundle_info) {
    $bundle_field_definitions += $this->entityFieldManager->getFieldDefinitions($this->entityType->id(), $bundle_id);
  }

  $field_definitions += $bundle_field_definitions;
}

$field_storage_definitions = array_map(function (FieldDefinitionInterface $definition) {
  return $definition->getFieldStorageDefinition();
}, $field_definitions);

What seems to be happening is fieldable entities such as nodes have duplicate fields being added. I'm not sure what the implication of removing those lines is, but this patch is currently breaking core functionality.

joachim’s picture

I wonder if this is because config fields get declared as bundle fields? (I think they do??)

This might be related to #2337515: Allow @FieldType to customize views data -- base fields don't get the right views data set up for them.

berdir’s picture

It's related in that EntityViewData provides hardcoded and limited support for base fields (which is what that issue is about) and views_views_data_alter() only supports field_storage_config config entities. Non-base and non-config fields are in a blindspot as far as Views is concerned, it only knows those about those two.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

sander wemagine’s picture

StatusFileSize
new2.99 KB

This is a working version of #48 for Drupal 10. Please test it well before using it, I haven't tested it very thoroughly yet.

sander wemagine’s picture

sander wemagine’s picture

StatusFileSize
new5.47 KB

Updated version of patch #58, found a bug. This new patch works for Drupal 10.

albeorte made their first commit to this issue’s fork.

albeorte’s picture

Patch #60 does not conflict with Drupal 10.1.x, however, it generates an error when applying the database updates.

An MR has been created associated with Drupal 10.1.x version where the error is fixed.

albeorte’s picture

jon pugh’s picture

StatusFileSize
new7.02 KB

For anyone looking, the patch for the above MR can be viewed here: https://git.drupalcode.org/project/drupal/-/merge_requests/4953.patch

Uploading it here for testing.

rob230’s picture

The patch #65 still breaks List (text) fields in views filters. Is nobody else having that problem?

omarlopesino changed the visibility of the branch 2898635-views-data-bundle-field-definitions to hidden.

omarlopesino changed the visibility of the branch 2898635-95x-PP1-bundleFieldDefinitions-are-not-added-in-EntityViewsData to hidden.

omarlopesino’s picture

I've had the same problem as #66. I've added some commits to the latest MR, to allow handlers to detect bundle fields. I've hidden the rest to prevent confusion to know which MR is canonical.

The only problem is that the changes are not compatible with Drupal 11.x because on the changes added at this issue, related with computed bundle field and views. So it is needed to update the MR to be compatible with 11.x

omarlopesino’s picture

I added blob support after finding a regression in a filter that was working before the patch. The specific filter is the layout section field. The field was already added by reading field storage definitions, and now is being added in both field storage definitions and bundle field definitions.

longwave’s picture

Title: [PP-1] bundleFieldDefinitions() are not added in EntityViewsData » bundleFieldDefinitions() are not added in EntityViewsData
kristiaanvandeneynde’s picture

Awesome, one step closer to better bundle field support in Views :)

pingwin4eg’s picture

@omarlopesino

I've had the same problem as #66. I've added some commits to the latest MR, to allow handlers to detect bundle fields.

So did you fix the problem from comment #66?

rowrowrowrow made their first commit to this issue’s fork.

voleger made their first commit to this issue’s fork.

voleger’s picture

StatusFileSize
new7.57 KB

Opened MR !13189, which is a rebase of `2898635-10.1.x-views-data-bundle-field-definitions` branch against `11. x` branch, and added a deprecation message for the newly introduced constructor argument.

oily changed the visibility of the branch 2898635-11.x to hidden.

oily changed the visibility of the branch 2898635-10.3.x-views-data-bundle-field-definitions to hidden.

oily changed the visibility of the branch 2898635-10.1.x-views-data-bundle-field-definitions to hidden.

oily’s picture

Hid 3x branches to leave visible only one 11.x branch, see #77.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

vasike’s picture

Some feedback for this MR: It seems datetime fields filters get "reverted" to a String filter.

giuseppe87 made their first commit to this issue’s fork.

giuseppe87’s picture

StatusFileSize
new17.57 MB

I tried to rebase the patch against main because conflicted against the latest 11.3.8.

However the patch doesn't work because the getFieldStorageDefinitions is duplicated, as I think as it moved inside a trait.

What is the correct approach in this case? Openining a new MR against 11.x?
Meanwhile I've attached the patch rebased against 11.x

catch’s picture

grevil’s picture

@giuseppe87 as you can see, the MR adds an entirely new "getFieldStorageDefinitions()" method implementation. Even though one implementation already exists in "EntityViewsData" for "drupal/core-recommended" >= 11.3.9 (maybe even in lower versions, but "11.3.9" is the one I use).

We don't need to open a new MR, we just have to fix the existing one, since this is already incorrect for current 11.x.

@catch, what do you mean with "may help here"?

grevil’s picture

I created a new 11.x branch with a freshly rebased version of the old MR, since trying to rebase the old branch caused some very weird merge conflicts (furthermore the old version implemented a seperate "getFieldStorageDefinitions").

I also created a new main branch variant without the "getFieldStorageDefinitions" method changes. This version needs to be double checked, whether we need replacement code for the the "getFieldStorageDefinitions" additions.

MR https://git.drupalcode.org/project/drupal/-/merge_requests/13189 can be closed, as it is redundant now.

grevil changed the visibility of the branch 2898635-bundle_field_definitions_are_not_added to hidden.