2

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?

forvas
  • 9,801
  • 7
  • 62
  • 158

2 Answers2

2

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).

CZoellner
  • 13,553
  • 3
  • 25
  • 38
  • Hi @CZoellner, I've tried your code and it did not work. I've opened the developer mode option *Edit FormView* to check that the priority of my extension view was moved to 100, and it was, even I've re-installed my module, but my smart button still appears on the left. I guess it is because my view is the only one inheriting from the parent view. The rest of views (purchase, stock, etc) inherit from the child view, so `priority` can do nothing in order to put my view after those ones. Do you think so? – forvas Jul 19 '21 at 10:34
  • Yes that could definitely be a problem. Is there no chance (because of module dependencies) to extend one of the inheriting views? – CZoellner Jul 19 '21 at 13:19
  • 1
    No, it should not depend on other modules. If it was the case, a greater `priority` would be the solution. I guess the right answer is the one by @Kenly. Anyway +1 because yours is useful too. – forvas Jul 19 '21 at 15:46
2

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

Kenly
  • 24,317
  • 7
  • 44
  • 60
  • Inheriting from `product.product_template_only_form_view` works for me. I thought it was going to throw an error since there is no `div` with name `button_box` in the definition of `product.product_template_only_form_view`, but this view inherits from another view which in fact has that `div`, so this is the reason why no error was raised, isn't it? – forvas Jul 19 '21 at 11:15
  • 1
    Yes of course, when your inheritance specs are applied, the `button_box` div is already there (The parent view was fully resolved before the current view’s inheritance specs are applied). – Kenly Jul 19 '21 at 11:31
  • 1
    Oh didn't see that part in the doc. The new one has way more interesting information than the old one. – CZoellner Jul 19 '21 at 13:20