How to Add a Custom Action on Clicking Tags in Odoo 18
Introduction:
The blog given demonstrates integration of functionality in the Many2Many Tags field in Odoo whereby upon clicking a tag a dialog – “are you sure” is displayed as a confirmation message. When that is agreed to, the user has a view of the Windows form in relation to the clicked item. This particular detail will be easy to grasp by both the technical and non-technical audience.
Code:
1. Module Declaration
This line declares the JavaScript file as an Odoo module.
Odoo automatically recognizes and loads this file when the module is installed.
2. Import Statements
_t: Used for multi-language translations in Odoo.
useService: Access Odoo’s core services, like dialog management and navigation.
Many2ManyTagsFieldColorEditable: Refers to the original Many2Many Tags widget we are enhancing.
ConfirmationDialog: A prebuilt Odoo dialog for user confirmations.
patch: A tool to modify existing widgets or components without rewriting them entirely.
3. Patching the Many2Many Tags Widget:
This line patches the Many2ManyTagsFieldColorEditable widget. It overrides specific behaviors while keeping the rest intact.
4. Setup Function
The setup function initializes the patched widget.
super.setup(): Calls the original setup method from the base widget.
this.action: Provides navigation functionality to open forms, lists, etc.
this.dialogService: Enables displaying dialogs (popups).
5. Tag Click Behavior
This function defines what happens when a user clicks a tag.
Parameters:
ev: The click event.
record: The tag's corresponding record.
6. Error Handling for Missing Records
Checks if the clicked tag is linked to a valid record. If not, logs an error and stops further execution.
7. Displaying the Confirmation Dialog
Adds a confirmation dialog.
body: The dialog's message text.
confirmClass: Styles the confirmation button (e.g., blue for primary).
confirmLabel: Text for the confirmation button.
confirm: Action to take when the user clicks "Open Form View."
8. Opening the Form View
type: "ir.actions.act_window": Opens a form view.
res_model: The related model of the tag.
res_id: The specific record ID.
views: Specifies the form view.
target: "current": Opens the form in the current window.
9. Cancel Button Behavior
Defines a cancel button with no additional behavior when clicked.
Manifest:
web.assets_backend: Ensures the file is loaded for the Odoo backend.
File Path: Points to where the JavaScript file is stored in your module.
How It Works for Users:
1.Click a Tag:
A user clicks on a tag in the Many2Many field.
2.Popup Dialog:
A confirmation dialog appears with two options:
Open Form View: Redirects the user to the tag's related record.
Cancel: Closes the dialog without further action
3.Smooth Navigation:
If "Open Form View" is selected, the user is taken directly to the relevant form.
Benefits:
For Developers:
Reusable Code: Easily extendable to other widgets.
Clean Design: Uses Odoo’s built-in services for consistency.
Minimal Maintenance: Only modifies the necessary parts of the widget.
For Business Users:
Enhanced Experience: Simplifies workflows by enabling tag interaction.
Error-Free Navigation: Prevents unintentional clicks.