In Odoo 16, the Command Palette is a powerful tool that allows you to access and execute various actions within the system quickly. It provides search functionality to find and execute menu items, actions, and configuration settings. The command palette uses some commands to execute these actions.
In this blog, we will explore how to add new commands to the command register.
For example, let's say we want to add the “!” command to the command palette. When we click the exclamation mark key on the keyboard, we should display a visually appealing template command palette.
We need to add a new command type to the registry for this.
1: Add a new command to the registry and set a template for render while we click the exclamation mark.
/** @odoo-module **/
import { _lt } from "@web/core/l10n/translation";
import { registry } from "@web/core/registry";
const rpc = require("web.rpc");
import { Component } from "@odoo/owl";
import { computeAppsAndMenuItems } from "@web/webclient/menus/menu_helpers";
class ModelItemCommand extends Component {}
ModelItemCommand.template = "text_commander2.ModelItemsCommand";
const ModelItemCommandRegistry = registry.category("command_setup");
ModelItemCommandRegistry.add("!", {
debounceDelay: 200,
emptyMessage: _lt("Commands: "),
name: _lt("Record"),
placeholder: _lt("Search for a record..."),
});
const ModelItemCommandProvider = registry.category("command_provider");
ModelItemCommandProvider.add("model",{
namespace: "!",
async provide(env, options){
const suggestion = []
suggestion.push({
Component: ModelItemCommand,
});
return suggestion
},
});
class ModelItemCommand extends Component {}
ModelItemCommand.template = "text_commander2.ModelItemsCommand";
In the code above, we create a ModelItemCommand component that extends from the primary component and sets a template. So when we click on the exclamation point, this model will render the ModelItemCommand.
ModelItemCommandRegistry.add("!", {
debounceDelay: 200,
emptyMessage: _lt("Commands: "),
name: _lt("Record"),
placeholder: _lt("Search for a record..."),
});
In the above code, we are adding the “!” key command registry. Here you can see that you can also set placeholders and empty messages.
ModelItemCommandProvider.add("model",{
namespace: "!",
async provide(env, options){
const suggestion = []
suggestion.push({
Component: ModelItemCommand,
});
return suggestion
}
Here you can see “ModelItemCommandProvider” component is set to namespace “!”,
2: Here is an example of Xml template file
<?xml version="1.0" encoding="utf-8"?>
<template>
<t t-name="text_commander2.ModelItemsCommand" owl="1">
<div style="margin-left:25px;padding:10px;">
<span>Template</span>
</div>
</t>
</template>
3: Result
As you can see above, the exclamation point command is called the template body. Also, you can see the instructions below the template body.
To read more about creating a new view type in Odoo 16, refer to our blog How to Create a New View Type in Odoo 16