From 89e855f210c1c091f14e3de423d20883e18b2f4d Mon Sep 17 00:00:00 2001 From: Raffael Meyer <14891507+barredterra@users.noreply.github.com> Date: Wed, 2 Oct 2024 15:01:12 +0100 Subject: [PATCH] fix: generalize link formatter (#43199) --- erpnext/public/js/utils.js | 51 +++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/erpnext/public/js/utils.js b/erpnext/public/js/utils.js index 66a6c18d3de..d9ecab65ca6 100755 --- a/erpnext/public/js/utils.js +++ b/erpnext/public/js/utils.js @@ -976,41 +976,36 @@ erpnext.utils.map_current_doc = function (opts) { } }; -frappe.form.link_formatters["Item"] = function (value, doc) { - if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) { - return value + ": " + doc.item_name; - } else if (!value && doc.doctype && doc.item_name) { - // format blank value in child table - return doc.item_name; - } else { - // if value is blank in report view or item code and name are the same, return as is - return value; - } +frappe.form.link_formatters["Item"] = function (value, doc, df) { + return add_link_title(value, doc, df, "item_name"); }; -frappe.form.link_formatters["Employee"] = function (value, doc) { - if (doc && value && doc.employee_name && doc.employee_name !== value && doc.employee === value) { - return value + ": " + doc.employee_name; - } else if (!value && doc.doctype && doc.employee_name) { - // format blank value in child table - return doc.employee; - } else { - // if value is blank in report view or project name and name are the same, return as is - return value; - } +frappe.form.link_formatters["Employee"] = function (value, doc, df) { + return add_link_title(value, doc, df, "employee_name"); }; -frappe.form.link_formatters["Project"] = function (value, doc) { - if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) { - return value + ": " + doc.project_name; - } else if (!value && doc.doctype && doc.project_name) { - // format blank value in child table - return doc.project; +frappe.form.link_formatters["Project"] = function (value, doc, df) { + return add_link_title(value, doc, df, "project_name"); +}; + +/** + * Add a title to a link value based on the provided document and field information. + * + * @param {string} value - The value to add a link title to. + * @param {Object} doc - The document object. + * @param {Object} df - The field object. + * @param {string} title_field - The field name for the title. + * @returns {string} - The link value with the added title. + */ +function add_link_title(value, doc, df, title_field) { + if (doc && value && doc[title_field] && doc[title_field] !== value && doc[df.fieldname] === value) { + return value + ": " + doc[title_field]; + } else if (!value && doc.doctype && doc[title_field]) { + return doc[title_field]; } else { - // if value is blank in report view or project name and name are the same, return as is return value; } -}; +} // add description on posting time $(document).on("app_ready", function () {