Hi guys,

Thanks very much for this very useful module that comes as a handy complement to Drupal Commerce.

As I was following up with a feature request at #1319810: Improve Entity Relationships in Views, I was brought to take a closer look at the Product Reference and Customer modules, and more particularly their integration with Views.

The comment at #1319810-28: Improve Entity Relationships in Views, by nigelwaters, particularly attracted my attention:

There are many missing relationships between all of the entities that commerce provides

Entities in core commerce:

Product entity
Customer profile entity
Line item entity
Order entity
Payment transaction entity

Another example or where I cannot get the relationship is in the default Customer Profile view. In Customer Profiles try adding a relationship to Orders to get the total amount that a customer has spent on your site. Doesn't exist.

So after further investigation and doing an in-depth review of all the objects quoted above (implemented by Drupal Commerce), and their associated views relationships integration, it seems only two major relationships would be actually "missing":

  1. A reverse Product Reference relationship to be configured for views on Line Item entities.
    In practice, it means it would be possible to relate a product to a Line Item entity that is referencing it through one of its Product Reference fields.
    For example, for the commerce_product field, on Product Line Item entites, a new views relationship called:
    Commerce Product: Commerce Line item referencing products from commerce_product could be configured.
    For more information on this relationship, see #1319810-26: Improve Entity Relationships in Views.
     
  2. A reverse Customer Profile relationship could be configured for views on Customer Profile entities.
    Concretelly speaking, this means that it would be possible to relate a Customer Profile entity with another entity that is referencing it through one of its Customer Profile Reference fields.
    For example, for the commerce_customer_billing field, a new views relationship called:
    Commerce Customer Profile: Commerce Order referencing customer profiles from commerce_customer_billing could be configured on a views on customer profiles.

Since most of views integration components could be added through Views API hooks, such as hook_views_data_alter, I assumed these relationships could be provided by a contributed module.
In this particular case, an implementation of hook_views_data_alter would be enough to add these relationships.
 
To sum it up, the idea would be to add a sub-module to Commerce Extra, that could be called Commerce Extra Views Extra for example, and would allow adding additional Commerce Views integration components, such as relationships, plugins, handlers, etc... that could be requested by the community or claimed as "missing".

The reason why I turned myself to Commerce Extra, simply comes from its project page first few lines description:

Commerce Extra module allows site builders to enable extra features or improvements which are missing from Drupal Commerce core. The module is a collection of minimal submodules which some of developer might not consider worth of a full project and therefore are added into this module.

The proposed Commerce Extra Views Extra module would exactly correspond to this description (features supposed "missing" and too short for a full module).

I would greatly appreciate to have your feedback on this feature request, particularly this sub-module idea, and if you could let me know if I overlooked or missed anything in any of these modules' implementations, Views integration code or APIs.
Feel free to let me know if you would have any questions, comments or concerns on any aspects of the discussed implementations, I would be glad to explain in more details.

Thanks very much to all, in advance, for your comments, feedback, and insight.
Cheers!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

DYdave’s picture

Quick follow up on this feature request.

Please find attached to this comment a patch against the commerce_extra-7.x-1.x version at 925211d in response to the suggested sub-module/idea in ticket summary: commerce_extra-views-extra-relationships-submodule-1917156-1.patch

This patch adds a new sub-module called Commerce Extra Views Extra which provides the relationships as specified in the issue summary (points 1 and 2).
This sub-module wouldn't need to have any dependency with Commerce Extra, since there is no configuration related with the it or its API. It could probably be used alone, but according to Commerce Extra's project description, it still seemed to be a good place for such a small piece of code.

Commerce Extra Views Extra mostly includes an implementation of hook_views_data_alter greatly inspired from the code in commerce_product_reference.views.inc, line 84.

After applying the patch and enabling the sub-module (commerce_extra is not required), two new relationships should be made available:

  1. For example, for the commerce_product field, on Product Line Item entities, a new views relationship called:
    Commerce Product: Commerce Line item referencing products from commerce_product could be configured.
     
  2. For example, for the commerce_customer_billing field, a new views relationship called:
    Commerce Customer Profile: Commerce Order referencing customer profiles from commerce_customer_billing could be configured in a views of Customer Profile entities.

 
Patch has been tested and seems to work as expected.
 
Please let me know if you would have any questions, objections, comments, suggestions, recommendations or concerns on the patch or this particular feature request, I would be glad to explain in more details or re-roll the patch if necessary.
I would greatly appreciate some help from module maintainers and if any of you could take a bit of time to look into the attached patch to give me your feedback/opinion on this ticket.
 
Any questions, feedback, testing, changes, recommendations would be highly appreciated.
Thanks to all in advance.

DYdave’s picture

Quick fix of the coding standards: changed a wrong indentation.

Please ignore previous patch and consider the one attached to this comment:
commerce_extra-views-extra-relationships-submodule-correct-indent-1917156-2.patch

Any questions, feedback, testing, changes, recommendations would be highly appreciated.
Thanks to all in advance.

DYdave’s picture

For the sake of testing, I thought it might be handy to also include in this ticket a Views export of the default Products view provided by Drupal Commerce and configurable in Views at: admin/structure/views/view/commerce_products

Please find attached to this comment a complete export of the commerce_products view to demonstrate the use of the reverse product reference relationship on Line Items, as suggested in point 1 of the issue summary:
views-commerce_products-reverse-line-items-aggregate-1.txt

Simply import the view from /admin/structure/views/import by copy/pasting the code in the attached txt file in the field Paste view code here and select Replace an existing view if one exists with the same name.

I would greatly appreciate if anyone could take a closer look at the patch and spend a little bit of time testing the views export or the relationships.
Once again any questions, feedback, testing, changes, recommendations would be highly appreciated.
Thanks to all in advance

DYdave’s picture

Fixed warning error message, caused by commerce_extra_views_extra_help.

Please ignore previous patch and consider the one attached to this comment:
commerce_extra-views-extra-relationships-submodule-1917156-4.patch

Any questions, feedback, testing, changes, recommendations would be highly appreciated.
Thanks to all in advance.

iMiksu’s picture

Status: Needs review » Postponed (maintainer needs more info)

Hi!
I'm wondering that is this issue raised in Drupal Commerce? If they are not happy with the feature request, then we could consider to include it here?

Jerenus’s picture

I think it's time to integrate this feature. Since 3 years passed.

Jerenus’s picture

Status: Postponed (maintainer needs more info) » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

Jerenus’s picture

Status: Needs work » Reviewed & tested by the community