Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
4798 Lượt xem

How to show/hide menu item based on system parameter?


I've this code to create menu item, and it working fine, but i need to hide it depends on system parameter.


Does anyone know how to do that? Thanks.


<!-- this is for test, remove this after testing -->
<record id="check_qty_and_move_from_internal_customer_to_customer" model="ir.actions.server" style="color:#e8bf6a;">>
<field name="name">Cron Run Directly 2</field>
<field name="model_id" ref="model_kiotviet_cron"/>
<field name="binding_model_id" ref="onnet_kiotviet.model_kiotviet_cron"/>
<field name="state">code</field>
<!-- function called -->
<field name="code">model.check_qty_and_move_from_internal_customer_to_customer()</field>
</record>

<!-- this is for test, remove this after testing -->
<menuitem id="check_qty_and_move_from_internal_customer_to_customer" name="Cron Run Directly 2" parent="kiotviet_menu_root"
action="check_qty_and_move_from_internal_customer_to_customer" sequence="6"/>

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

After followed Niyas Raphy answer, i've solved my problem, and here's the code:


from odoo import models, api, tools, _


class Menu(models.Model):
_inherit = 'ir.ui.menu'

@api.model
@tools.ormcache('frozenset(self.env.user.groups_id.ids)', 'debug')
def _visible_menu_ids(self, debug=False):
menus = super(Menu, self)._visible_menu_ids(debug)
# get system parameter
if not self.env['ir.config_parameter'].sudo().get_param('your_module.your_config_name'):
# get menu item id
menu_item_id = self.env.ref('your_module.menu_item_id').id
# remove it from menus
menus.discard(menu_item_id)
return menus

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

You can inherit the _visible_menu_ids function from the ir.ui.menu model and filter the menus based on the configuration in the system parameters.

An use case can be found in this module: https://apps.odoo.com/apps/modules/15.0/kg_hide_menu/


Thanks

Ảnh đại diện
Huỷ bỏ
Tác giả

thanks you sir, i will try this and will update the result on this post.

Tác giả

i've followed your answer and found the answer for my self, thanks you sir.

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 8 22
2839
1
thg 2 25
7255
2
thg 4 24
8328
2
thg 11 22
3193
1
thg 6 25
1592