2
votes

I'm using Odoo 9 community version.

In Sale order form has following buttons:

<button name="action_confirm" states="sent" string="Confirm Sale" class="btn-primary" type="object" context="{'show_sale': True}"/>
<button name="action_confirm" states="draft" string="Confirm Sale" type="object" context="{'show_sale': True}"/>

I'm trying to hide both button from the view. So I have tried with following code.

<record model="ir.ui.view" id="hide_so_confirm_button_form">
    <field name="name">hide.so.confirm.button.form</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">
        <button name="action_confirm" position="attributes">
            <attribute name="invisible">1</attribute>
        </button>
    </field>
</record>

I have also tried following attribute:

<attribute name="states"></attribute>

With above code, it's only hide/affect first button.

Question:

How to hide both Confirm Sale button?

3

3 Answers

5
votes

The mechanism without xpath only influences the first hit. That's why you have to use xpath here.

Another good example (maybe not for Odoo 9 anymore) is setting a new sale.order.line field behind the name field on sale.order form view. The form view is something like this:

<form>
    <field name="name" /> <!-- sale.order name field -->
    <!-- other fields -->
    <field name="order_line">
        <form> <!-- embedded sale.order.line form view -->
            <field name="name" />
            <!-- other fields -->
        </form>
        <tree> <!-- embedded sale.order.line tree view -->
            <field name="name" />
            <!-- other fields -->
        </tree>
    </field>
<form>

Using your way could try setting the new field behind sale.order name field (in this example). Using xpath will lead to the goal.

<xpath expr="//form//tree//field[@name='name']" position="after">
    <field name="new_field" />
</xpath>
<xpath expr="//form//form//field[@name='name']" position="after">
    <field name="new_field" />
</xpath>

So to answer your question directly (EDIT):

<xpath expr="//button[@name='action_confirm' and @states='sent']" position="attributes">
    <attribute name="states" /> <!-- delete states attribute, it's influencing invisible behaviour -->
    <attribute name="invisible">1</attribute>
</xpath
<xpath expr="//button[@name='action_confirm' and @states='draft']" position="attributes">
    <attribute name="states" /> <!-- delete states attribute, it's influencing invisible behaviour -->
    <attribute name="invisible">1</attribute>
</xpath
0
votes

you can use xpath ...

button[@name='action_confirm'][1]

xpath ...

button[@name='action_confirm'][2]

Hope it helps

0
votes

**For Odoo 12

In addition to @CZoellner's answer, For Odoo 12, its definition on view_order_form is changed to

<button name="action_confirm" id="action_confirm"
    string="Confirm" class="btn-primary" type="object"
    attrs="{'invisible': [('state', 'not in', ['sent'])]}"/>
<button name="action_confirm"
    string="Confirm" type="object"
    attrs="{'invisible': [('state', 'not in', ['draft'])]}"/>

Please note, that in this change, there are no states attribute anymore. So, to hide these two button, we can use

<xpath expr="//button[@name='action_confirm'][1]" position="attributes">
    <attribute name="attrs"></attribute>
    <attribute name="invisible">1</attribute>
</xpath>
<xpath expr="//button[@name='action_confirm'][2]" position="attributes">
    <attribute name="attrs"></attribute>
    <attribute name="invisible">1</attribute>
</xpath>