From afa5c0fca6b80c932ea80bc144ee78805df62095 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Tue, 8 Jun 2021 19:44:13 +0900
Subject: [PATCH] core: keep using default_params from proxy listbox ids

With bf57228a93 ([erp5_core] RelationField listbox must also fetch
default_params value from the relationfield, 2018-05-09), we introduced a
regression that listboxs from relation fields "proxy listbox ids" default params
where no longer used. As a result, if we have a listbox which define some
catalog search with default parameters, like for example the "Suppliers" or
"Clients" listboxs from erp5_trade are doing, these parameters were not
taken into account. In the case of trade listbox, this was showing all nodes.

This changes Base_getRelatedObjectParameter to get catalog search from the
proxy listbox if there is one.
---
 .../Base_getRelatedObjectParameter.py         | 37 +++++++++++++------
 1 file changed, 25 insertions(+), 12 deletions(-)

diff --git a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/Base_getRelatedObjectParameter.py b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/Base_getRelatedObjectParameter.py
index 4aca55f00a..e3a445d149 100644
--- a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/Base_getRelatedObjectParameter.py
+++ b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/Base_getRelatedObjectParameter.py
@@ -1,7 +1,13 @@
 # This script is used in order to retrieve parameter in the listbox Displayed
 # by Base_viewRelatedObjectList from the relation field
-result = None
 
+# This dialog can either display a generic listbox (Base_viewRelatedObjectList/listbox)
+# that takes its configuration dynamically from the relation field configuration, or
+# display listbox from "Proxy Listbox IDs".
+# When using a listbox from "Proxy Listbox IDs", that listbox defines the configuration
+# and have priority over the relation field configuration.
+
+result = None
 request = context.REQUEST
 
 if parameter is not None:
@@ -11,9 +17,18 @@ if parameter is not None:
   form_id = request.get('original_form_id',None) \
       or request.get('field_your_original_form_id', None) \
       or request.get('form_id')
-  listbox = getattr(context, form_id).get_field(field_id)
-  dialog_id = listbox.get_value('relation_form_id') or 'Base_viewRelatedObjectList'
-  result = listbox.get_value(parameter)
+  relation_field = getattr(context, form_id).get_field(field_id)
+  dialog_id = relation_field.get_value('relation_form_id') or 'Base_viewRelatedObjectList'
+
+  # the listbox from "proxy listbox id"
+  proxied_listbox = None
+  relation_field_proxy_listbox = context.Base_getRelationFieldProxyListBoxId()
+  if relation_field_proxy_listbox != \
+          'Base_viewRelatedObjectListBase/listbox':
+    proxied_listbox = context.restrictedTraverse(
+              relation_field_proxy_listbox, None)
+
+  result = relation_field.get_value(parameter)
 
   if result in [ [], (), None, '']:
     if parameter == 'proxy_listbox_ids':
@@ -21,17 +36,15 @@ if parameter is not None:
     if parameter != 'parameter_list':
       result = getattr(context, dialog_id, None).get_field( 'listbox' ).get_orig_value(parameter)
 
+  if parameter == 'parameter_list':
+    if proxied_listbox is None:
+      return result
+    return proxied_listbox.get_value('default_params')
+
   if parameter == 'portal_type':
-    portal_type = listbox.get_value('portal_type')
-    proxied_listbox = None
-    relation_field_proxy_listbox = context.Base_getRelationFieldProxyListBoxId()
-    if relation_field_proxy_listbox != \
-            'Base_viewRelatedObjectListBase/listbox':
-      proxied_listbox = context.restrictedTraverse(
-                relation_field_proxy_listbox, None)
+    portal_type = relation_field.get_value('portal_type')
     if proxied_listbox is None:
       return portal_type
-
     proxied_listbox_portal_type = proxied_listbox.get_value('portal_types')
     portal_type_first_item_list = [x[0] for x in portal_type]
     return [x for x in proxied_listbox_portal_type if x[0] in portal_type_first_item_list] or portal_type
-- 
2.30.9