While Report Type plugins provide out of the box reports, end users will want to be able to build custom reports using Views. Since the reports are content entities this should "just work." But it needs to be verified. And maybe we should provide a test module with a sample View for testing and documentation.

I've spent some time trying to summarize the work currently being done in core that relates to this issue. It seems that there are some major bundle-related problems that need to be sorted out. This is the most directly relevant issue:

In Entity API Issue #2914091: Views filter handler for bundle field crashes for an entity with bundle plugins, @joachim proposes the following possible workarounds:

This could be fixed by providing a custom field handler for the bundle field for this sort of entity.

However, its probably going to come up on other views plugins too.

I think the best long-term solution would be to have entities that use bundle plugins automatically define a config entity type, and automatically create the bundle config entities in sync with the plugins. That would also solve a *lot* of issues wherever core expects entity bundles to be themselves entities.

I don't have any experience with views field handlers, but this may be the fastest (if not necessarily the right or best) path forward for order report plugins. Use hook_views_data_alter() to define the bundle fields for views and then create a field handler for each bundle field. After experimenting a bit, it seems that views just needs to be "told" where to find the data. (It looks for the bundle fields as a column in the commerce_order_report base table by default.)

If that won't work, here are some of the various core issues that seem most relevant:
1. Core Issue #2898635: bundleFieldDefinitions() are not added in EntityViewsData
* several patches have been created, most recent work was 4/20/18
* setName() and setTargetEntityTypId() are required to translated the config definitions into storage definitions.
* possibly blocked by core issue #2930736

2. Core Issue #2930736: EntityViewsData assumes BaseFieldDefinitions where it should use FieldDefinitionInterface
* The gist of the problem seems to be using FieldDefinitionInterface and FieldStorageDefinitionInterface objects interchangeably, which works for BaseFieldDefinition objects but not bundle fields:

EntityViewsData has the following code around line 252:
    // Load all typed data definitions of all fields. This should cover each of
    // the entity base, revision, data tables.
    $field_definitions = $this->entityManager->getBaseFieldDefinitions($this->entityType->id());
    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    if ($table_mapping = $this->storage->getTableMapping($field_definitions)) {

* work has been done on this issue, but the current patch is not passing.

3. Entity API Issue #2817751: Create an API for bundle plugins
* This issue is closed/fixed
* Included in this summary only since it seems to relate to proposed solutions in core.

4. Core Issue #2346347: Finalize API for creating, overriding, and altering code-defined bundle fields
* Seems to me like too massive of an issue.

@kiamlaluno provided a summary of the current to-do items:

☐ Fix the problem hook_entity_bundle_field_info_alter() and hook_entity_base_field_info_alter() have (#2346329: hook_entity_base_field_info_alter() and hook_entity_bundle_field_info_alter() are documented to get a parameter that doesn't implement an interface with setter methods)
☐ Define a new class for bundle field definitions (#2935932: Add a class for bundle fields definitions)
☐ Correct the example given in hook_entity_bundle_field_info() (#2935932: Add a class for bundle fields definitions)
☐ Create bundle_field_override config entities (#2935978: Create a bundle_field_override configuration entity to override a bundle field with configuration)
☐ Update the documentation where necessary
☐ Update the code and remove the comments pointing to this issue, once this marked as fixed

CommentFileSizeAuthor
#5 2945568-05.patch18.36 KBlisastreeter

Comments

mglaman created an issue. See original summary.

mglaman’s picture

lisastreeter’s picture

Issue summary: View changes
lisastreeter’s picture

StatusFileSize
new18.36 KB

For "normal" (non-plugin-based) bundle fields, the hook_views_data() implementation in views.views.inc takes care of providing their views data. It does so by loading all field storage config and then building the views data for the storage entities with:
views_field_default_views_data($field_storage);

Unfortunately, there is no stored field config for the plugin-based bundle fields. So what I've done in this patch is modify views_field_default_views_data() for the plugin bundle fields. I have not attempted to write test code yet, but it seems to be working when I test through the UI.

This is pretty messy code, so I expect to find some bugs to sort out. But I thought I'd post what I have so far. The good news is that what I've done here should be easy to apply to Payments and other entities with plugin bundles. I set $entity_type_id and $base_table variables at the start of the function but otherwise, there is nothing specific to order reports.

Note: I took out the views code related to revisions and translations, to keep it as simple as possible; for a more general use-case, that would likely need to be added back in.

lisastreeter’s picture

Status: Active » Needs review
mglaman’s picture

+++ b/commerce_reports.views.inc
@@ -0,0 +1,433 @@
+  $all_field_definitions = array_filter($all_field_definitions, function (FieldDefinitionInterface $field_definition) {
+    return ($field_definition instanceof BundleFieldDefinition);
+  });

Ooh, I like. This way we could put it in the entity module.

But I'd push for Commerce itself first.

We could also check if it's not an instance of FieldStorageConfigInterface and isBaseField is false (instead of hard checking BundleFieldDefinition) but I doubt there are any other use cases.

I'm assuming anything after that is copy and paste from Views itself.

mglaman’s picture

Title: Verify support for Views » Ensure bundle plugin fields are available in fields

Renaming

  • mglaman committed 75ab9c4 on 8.x-1.x authored by lisastreeter
    Issue #2945568 by lisastreeter: Ensure bundle plugin fields are...
mglaman’s picture

Status: Needs review » Fixed

Yay! We have basic views support

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.