Odoo 12 Development Essentials
上QQ阅读APP看书,第一时间看更新

Adding the app's top menu item

Since we are creating a new app, it should have a main menu item. On the CE, this is shown as a new entry in the top-left drop-down menu. On the EE, it is shown as an additional icon in the App Switcher main menu.

Menu items are view components added using XML data files. Create the views/library_menu.xml file to define a menu item:

<?xml version="1.0"?> 
<odoo> 
  <!-- Library App Menu -->
  <menuitem id="menu_library" 
            name="Library" /> 
</odoo> 

The user interface, including menu options and actions, is stored in database tables. The preceding code is an Odoo data file, describing a record to load into the Odoo database. The <menuitem> element is an instruction to write a record on the ir.ui.menu model. 

The id attribute is also called XML ID, and is used to uniquely identify each data element, providing a way for other elements to reference it. For example, when adding library submenu items, we will need to reference the XML ID of their parent menu item, which should be menu_library. XML IDs are an important topic, and are discussed in greater detail in Chapter 5, Import, Export, and Module Data.

The menu item added here is very simple, and the only attribute used is name. Other frequently-used attributes are not used here. We didn't set a parent menu item, because this is a top-level menu, and we didn't set action, because this menu item won't do anything—it is only the top element where submenu items will later be placed.

Our module does not yet know about this new XML data file. For this to happen, it needs to be declared in the __manifest__.py file, using the data attribute. It is a list of the data files to be loaded by the module upon installation or upgrade. Add this attribute to the manifest's dictionary:

'data': [
'views/library_menu.xml',
],

To load these menu configurations into our Odoo database, we need to upgrade the module. Doing that at this point won't have any visible effects. This menu item has no actionable submenu yet, and so won't be shown. It will be visible later, once we add one, and add the appropriate access-control permissions.

Items in the menu tree are only shown if there are any visible submenu items. The lower-level menu items, with Window Actions opening Views, are only visible if the user has access to the underlying Model.