如何在用户菜单 odoo 15 中添加操作菜单

How can I add action menu in usermenu odoo15

在 odoo 15 中,如何在用户菜单中添加新的操作菜单?在其他 odoo 版本(13 和 14)中,可以通过继承 UserMenu.Actions.

在 odoo 15 中,我尝试了以下代码,但它不起作用。

感谢任何建议

/** @odoo-module **/

import { registry } from "@web/core/registry";
import { preferencesItem } from "@web/webclient/user_menu/user_menu_items";

export function UserLog(env) {
    return Object.assign(
        {},
        preferencesItem(env),
        {
            type: "item",
            id: "log",
            description: env._t("UserRecent Log"),
            callback: async function () {
                const actionDescription = await env.services.orm.call("user.recent.log", "action_get");
                actionDescription.res_id = env.services.user.userId;
                env.services.action.doAction(actionDescription);
            },
            sequence: 70,
        }
    );
}

registry.category("user_menuitems").add('profile', UserLog, { force: true })

这是我的模型代码。

class UserRecentLog(models.Model):
    _name = 'user.recent.log'
    _order = "last_visited_on desc"

    
    @api.model
    def action_get(self):
        return self.env['ir.actions.act_window']._for_xml_id('user_recent_log.action_user_activity')

这是我的 xml 观点。

    <!-- actions opening views on models -->    
    <record model="ir.actions.act_window" id="action_user_activity">
        <field name="name">User Recent Log(s)</field>
        <field name="res_model">user.recent.log</field>
        <field name="view_mode">tree,form</field>
        <field name="view_id" ref="user_activity_view_tree"/>
    </record>

您无需更改任何内容,您的代码应该可以正常工作。您可以在 web 模块中查看用户首选项菜单项(类似于您的菜单项)。

export function preferencesItem(env) {
    return {
        type: "item",
        id: "settings",
        description: env._t("Preferences"),
        callback: async function () {
            const actionDescription = await env.services.orm.call("res.users", "action_get");
            actionDescription.res_id = env.services.user.userId;
            env.services.action.doAction(actionDescription);
        },
        sequence: 50,
    };
}

registry
    .category("user_menuitems")
    .add("profile", preferencesItem)

hr模块中还有一个实现:

import { registry } from "@web/core/registry";
import { preferencesItem } from "@web/webclient/user_menu/user_menu_items";

export function hrPreferencesItem(env)  {
    return Object.assign(
        {}, 
        preferencesItem(env),
        {
            description: env._t('My Profile'),
        }
    );
}

registry.category("user_menuitems").add('profile', hrPreferencesItem, { force: true })

所以你可以重写上面的代码如下:

import { registry } from "@web/core/registry";
import { preferencesItem } from "@web/webclient/user_menu/user_menu_items";

export function UserLog(env) {
    return Object.assign(
        {},
        preferencesItem(env),
        {
            type: "item",
            id: "log",
            description: env._t("Log"),
            callback: async function () {
                const actionDescription = await env.services.orm.call("res.users.log", "action_user_activity");
                env.services.action.doAction(actionDescription);
            },
            sequence: 70,
        }
    );
}

registry.category("user_menuitems").add('profile', UserLog, { force: true })

编辑:

在执行 window 操作时忽略 tree 视图模式。

_executeActWindowAction will check for the tree view type in the views registry to construct the views object and unfortunately, the tree view mode was not added to that registry.

要显示树视图,您可以将 [false, 'list'] 添加到视图列表并在 doAction 选项中指定视图类型 (list):

actionDescription.views.push([actionDescription.view_id[0], 'list'])
env.services.action.doAction(actionDescription, {viewType: 'list'});

或更新视图列表并将 tree 更改为 list:

actionDescription.views[0][1] = 'list';  

当然,你可以在action_get方法中做同样的事情:

action = self.env['ir.actions.act_window']._for_xml_id('user_recent_log.action_user_activity')
action['views'][0] = action['view_id'][0], 'list'
return action