Hi guys,

As I was trying to follow up with #1319810: Improve Entity Relationships in Views, I took a closer look at the Product Reference and Customer modules, and more particularly their integration with Views.

Currently both modules implement Reference fields (commerce_product_reference, in commerce_product_reference.module, line 464, and commerce_customer_profile_reference in commerce_customer.module, line 759, respectively) with declared views relationships provided through implementations of hook_field_views_data:
See commerce_product_reference.views.inc, line 33

<?php
        $data[$table_name][$field_name]['relationship'] = array(
          'title' => t('Referenced product'),
          'help' => t('Appears in: @bundles.', array('@bundles' => implode(', ', $bundles))),
          'base' => 'commerce_product',
          'base field' => 'product_id',
          'handler' => 'views_handler_relationship',
          'label' => t('Product'),
        );
?>

and commerce_customer.module, line 436

<?php
        $data[$table_name][$field_name]['relationship'] = array(
          'title' => t('Referenced customer profile'),
          'help' => t('A bridge to the Customer Profile entity that is referenced via !field_name.\nAppears in: @bundles.', array('!field_name' => $field['field_name'], '@bundles' => implode(', ', $bundles))),
          'base' => 'commerce_customer_profile',
          'base field' => 'profile_id',
          'handler' => 'views_handler_relationship',
          'label' => t('Customer profile referenced from !field_name', array('!field_name' => $field['field_name'])),
        );
?>

The problem currently is that it is not clearly specified, in the Views user interface (more specifically in the relationships selection dialog box), through which defined reference field the corresponding relationship would be made.
To clarify this issue, I have attached a quick screenshot (file named 201302141501DC_commerce_customer_profile_reference-improve-relationship-rev1.jpg) that shows two relationships for Custom Profile Reference fields (for example, commerce_customer_billing and commerce_customer_shipping) and it's hard to tell to which field each relationship corresponds.
It is only after having selected and added a particular relationship that it would display field's label in the Views overview summary.
 
So I thought it could be a good idea to try to find some inspiration from other modules and took a closer look at how the Entity Reference module does it, which brought me to the following code in entityreference.views.inc, line 23:

<?php
       $data[$table_name][$field_name]['relationship'] = array(
         'handler' => 'views_handler_relationship',
         'base' => $entity_info['base table'],
         'base field' => $entity_info['entity keys']['id'],
         'label' => t('@entity entity referenced from !field_name', $parameters),
         'group' => t('Entity Reference'),
         'title' => t('Referenced Entity'),
         'help' => t('A bridge to the @entity entity that is referenced via !field_name', $parameters),
       );
?>

and it seems indeed the !field_name is actually mentioned in several places with a little bit better help text.
 
Therefore, I was wondering if it would sound reasonable/acceptable to slightly change the help and label properties of the relationships of the fields commerce_product_reference and commerce_customer_profile_reference, to specify the field instance to which it would correspond.
 
This may sound pretty minor, but I think it would be a rather easy/simple change that could provide a better usability/easy of use of the views relationships implemented by these modules, and perhaps also get more aligned with the Entity Reference module.
 
I would greatly appreciate to have your feedback on that and if you could let me know if I overlooked or missed anything in any of these modules' implementations or Views integration code.
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!

Comments

dydave’s picture

Quick follow up on this ticket.

Please find attached to this comment a patch against the 7.x-1.x version at ed9d68b in response to the suggested changes in ticket summary: commerce-relationship-product-customer-reference-better-views-help-text-1917000-1.patch

I assumed the way it was done in the Entity Reference module, in entityreference.views.inc, line 23, could be exploited for the Product and Customer Profile Reference fields as well, so this patch simply changes the relationships' help and label properties to display in Views a more complete help text which includes referenced field name, for example:

Commerce Order: Referenced customer profile
A bridge to the Customer Profile entity that is referenced via commerce_customer_billing.
Appears in: commerce_order (commerce_order).

The text: A bridge to the @entity entity that is referenced via !field_name, was directly taken from entityreference.views.inc, line 30.
 
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 the request above, 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 feedback, testing, changes, recommendations would be highly appreciated.
Thanks to all in advance.

tstoeckler’s picture

tstoeckler’s picture

Patch itself looks great. Wanted to try it out but it doesn't apply locally. Don't have the latest dev, but still sending for a re-test to make sure.

Status: Needs review » Needs work