4

Our users can add other products to a product as associations. For most of our association types it doesn't make sense to show all products in the grid, they should select only products from one family.

The user could filter products by family in the Akeneo GUI, but it would be less work & make our data more consistent if the filter would get selected programmatically.

I would modify the code in Pim/Bundle/EnrichBundle/Resources/views/Association/_associations.html.twig like this:

var changeAssociation = function (associationId) {
    var $idField = $('#pim_product_edit_associations').find('input[type="hidden"][value="' + associationId + '"]');

    // 4 is an association example id where only one family should be displayed
    if(associationId == 4) {
        // TODO: Filter family in the product grid 
        // mediator.trigger('datagrid:???:' dataGrids.product.name);
    }

How can I change the filter of the product grid in Javascript?

Or is there a better way for my goal?

Sonson123
  • 10,879
  • 12
  • 54
  • 72

1 Answers1

4

The configuration of this grid is located here (EnrichBundle/Resources/config/datagrid/association_product.yml). You can customize the source.repository_method parameter to use your custom query in the product repository (createAssociationDatagridQueryBuilderWithFamily for example). As the product is passed to this repository method, you will be able to retrieve it's family.

This is a first step but it seems that you want to do that only on specific products.

Two solutions:

  • You decide in the repository method (as you have the product, you can decide or not if you will filter on it's family)
  • You decide on the template: in this case, you can create a copy of the association grid (at the end of the association_product.yml file you can copy the first grid config and rename it (association-with-family-product-grid for example). Then in you template, you can load one ore the other (the grid option is here).

Don't forget to clear your cache after datagrid config manipulation and you are good to go !

Julien Sanchez
  • 843
  • 4
  • 12
  • Thx for your detailed answer :), but it's still unclear to me. In my use case there are f.ex. 4 different association types that are used for all products. The behaviour of the association types should be the same for *all* products: For the association types with id 1-3 *all* products should get displayed in the association grid, but when the user clicks on the association type with id 4, only products of one family should get displayed. As the switching between the association types in the GUI is implemented in Javascript, I'll have also to implement adding a Family-filter in Javascript? – Sonson123 Aug 19 '15 at 09:01
  • 1
    Yes, sorry, the grid configuration link in the template was not the good one. This is the good one : https://github.com/akeneo/pim-community-dev/blob/master/src/Pim/Bundle/EnrichBundle/Resources/views/Association/_associations.html.twig#L142. At this line, you are in the template and have access to the product and the associationType – Julien Sanchez Aug 19 '15 at 09:46
  • Now I understand your solution, thanks!, unfortunately it isn't as easy as I expected. I think, I'll wait some weeks for Akeneo 1.4 :-) and then overwrite all these things. – Sonson123 Aug 19 '15 at 13:30
  • 1
    It will be definitely easier with the new product edit form. I'm currently working hard on it ! :) – Julien Sanchez Aug 19 '15 at 14:51