forvas
forvas

Reputation: 10189

How to set a sequence for Odoo smart buttons?

I am adding a new button inside the button_box div of products. To do that I wrote the following code:

<record id="product_template_form_view" model="ir.ui.view">
    <field name="name">product.template.common.form</field>
    <field name="model">product.template</field>
    <field name="inherit_id" ref="product.product_template_form_view"/>
    <field name="arch" type="xml">
        <div name="button_box" position="inside">
            <button class="oe_stat_button" type="object" name="open_maintenance_equipments" groups="maintenance.group_equipment_manager" icon="fa-wrench">
                <field string="Maintenance Equipments" name="maintenance_equipment_count" widget="statinfo"/>
            </button>
        </div>
    </field>
</record>

It works OK, but the button appears on the left side, before the Odoo main buttons of sale, purchase, etc. I would change the code to:

<record id="product_template_form_view" model="ir.ui.view">
    <field name="name">product.template.common.form</field>
    <field name="model">product.template</field>
    <field name="inherit_id" ref="product.product_template_form_view"/>
    <field name="arch" type="xml">
        <xpath expr="//div[@name='button_box']/button[last()]" position="after">
            <button class="oe_stat_button" type="object" name="open_maintenance_equipments" groups="maintenance.group_equipment_manager" icon="fa-wrench">
                <field string="Maintenance Equipments" name="maintenance_equipment_count" widget="statinfo"/>
            </button>
        </xpath>
    </field>
</record>

But the problem here would be that in the source view product.product_template_form_view there is still no button inside the div. So to apply this I should inherit from one of the main views which modify this product view (like the ones introduced by sale, purchase, etc apps). But I do not want to do so, because:

  1. My module should depend on apps which are not related to my module. I cannot automatically install for example purchase when installing my module.
  2. Despite doing this, it won't guarantee the fact that my button is after these "main" buttons, because it will depend on the module installation order.

So, do you know a way to achieve my purpose, like a sequence field or something like that?

Upvotes: 2

Views: 1108

Answers (2)

Kenly
Kenly

Reputation: 26698

Purchase and stock modules inherit the product.product_template_only_form_view view and sale module inherit the product.product_normal_form_view and you are inheriting the parent view which will always be fully resolved before the current view’s inheritance specs are applied:

if the view has a parent, the parent is fully resolved then the current view’s inheritance specs are applied

For more information, check the view resolution documentation

If you had inherited one of the views above, the button would have automatically been added to the right, depending on the priority of the view, as already mentioned by @CZoellner

Upvotes: 2

CZoellner
CZoellner

Reputation: 14768

There is a sequence field on model ir.ui.view which could be used for your requirement. So try to inherit one of the base views but also set a high priority which means a high sequence on that new view.

<record id="product_template_form_view" model="ir.ui.view">
    <field name="name">product.template.common.form</field>
    <field name="model">product.template</field>
    <field name="inherit_id" ref="product.product_template_form_view"/>
    <field name="priority" eval="100" />
    <field name="arch" type="xml">
        <div name="button_box" position="inside">
            <button class="oe_stat_button" type="object" name="open_maintenance_equipments" groups="maintenance.group_equipment_manager" icon="fa-wrench">
                <field string="Maintenance Equipments" name="maintenance_equipment_count" widget="statinfo"/>
            </button>
        </div>
    </field>
</record>

You could ofcourse use other values than 100 but that seems to be a magic border value, because nothing in Odoo is using such high sequences.

If you want a customer to get some more compatibility with Odoo Studio bear in mind that Studio views always get a sequence value of 99 (IIRC).

Upvotes: 2

Related Questions