From 4c0840cac3a2557f226b40dffdaa228f12c0bd7c Mon Sep 17 00:00:00 2001 From: "Guillaume (gdi)" Date: Mon, 22 May 2023 07:23:54 +0000 Subject: [PATCH] [FIX] website: use the domain on the form fields For each form action, existing fields can be defined. These existing fields are references to objects in the ORM. It is possible to put a domain on the field in order to filter the elements you want. For example, the "Create an opportunity" action has a "Salesperson" field that refers to users. This field filters the external users of the company in order to have only the internal users. Unfortunately, although this domain is present, it was not taken into account when displaying the values for the field. This commit fixes this problem and takes into account the defined domain. Steps to reproduce the bug fixed by this commit: - Have the website_crm module installed - Put a form on a page of the website - Set the form action to "Create an Opportunity" - Add a new field - Set the field type to "Salesperson" => The list of users displayed contains external users of the company. task-3095745 closes odoo/odoo#121903 Signed-off-by: Benoit Socias (bso) --- .../website/static/src/js/editor/wysiwyg.js | 3 ++ .../src/snippets/s_website_form/options.js | 33 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/addons/website/static/src/js/editor/wysiwyg.js b/addons/website/static/src/js/editor/wysiwyg.js index a16de263657..5f90122d625 100644 --- a/addons/website/static/src/js/editor/wysiwyg.js +++ b/addons/website/static/src/js/editor/wysiwyg.js @@ -3,6 +3,7 @@ import Wysiwyg from "web_editor.wysiwyg"; import snippetsEditor from "website.snippet.editor"; import socialMediaOptions from "@website/snippets/s_social_media/options"; +import formOptions from "@website/snippets/s_website_form/options"; /** * Show/hide the dropdowns associated to the given toggles and allows to wait @@ -113,6 +114,8 @@ const WebsiteWysiwyg = Wysiwyg.extend({ // from the accessed website. socialMediaOptions.clearDbSocialValuesCache(); + formOptions.clearAllFormsInfo(); + this._restoreMegaMenus(); this._super.apply(this, arguments); }, diff --git a/addons/website/static/src/snippets/s_website_form/options.js b/addons/website/static/src/snippets/s_website_form/options.js index f5f592b754d..06668951c3f 100644 --- a/addons/website/static/src/snippets/s_website_form/options.js +++ b/addons/website/static/src/snippets/s_website_form/options.js @@ -14,6 +14,31 @@ const qweb = core.qweb; const _t = core._t; let currentActionName; +const allFormsInfo = new Map(); +const clearAllFormsInfo = () => { + allFormsInfo.clear(); +}; +/** + * Returns the domain of a field. + * + * @private + * @param {HTMLElement} formEl + * @param {String} name + * @param {String} type + * @param {String} relation + * @returns {Object|false} + */ +function _getDomain(formEl, name, type, relation) { + // We need this because the field domain is in formInfo in the + // WebsiteFormEditor but we need it in the WebsiteFieldEditor. + if (!allFormsInfo.get(formEl) || !name || !type || !relation) { + return false; + } + const field = allFormsInfo.get(formEl).fields + .find(el => el.name === name && el.type === type && el.relation === relation); + return field && field.domain; +} + const FormEditor = options.Class.extend({ //---------------------------------------------------------------------- // Private @@ -607,6 +632,7 @@ options.registry.WebsiteFormEditor = FormEditor.extend({ if (!formInfo || !formInfo.fields) { return; } + allFormsInfo.set(this.$target[0], formInfo); const proms = formInfo.fields.map(field => this._fetchFieldRecords(field)); return Promise.all(proms).then(() => { formInfo.fields.forEach(field => { @@ -849,7 +875,8 @@ options.registry.WebsiteFieldEditor = FieldEditor.extend({ this.fields = {}; for (const [fieldName, field] of Object.entries(fields)) { field.name = fieldName; - field.domain = field.domain || []; + const fieldDomain = _getDomain(this.formEl, field.name, field.type, field.relation); + field.domain = fieldDomain || field.domain || []; this.fields[fieldName] = field; } // Create the buttons for the type we-select @@ -1590,3 +1617,7 @@ options.registry.DeviceVisibility.include({ && !this.$target.hasClass('s_website_form_field_hidden'); }, }); + +export default { + clearAllFormsInfo, +};