1
votes

I would like to hide "Create" and "Edit" buttons on state and group role in form view .For example hide Create and Edit buttons when the state is not draft and user belongs to request user group.

As I understand hide buttons I can on editing views. And on group role rules I can disable create or edit.

I tried to write a rule for request user group but then user can't use the button but see it.

From view I found only way to hide default Create and Edit buttons:

<form string="Request" create="false" edit="false">

But in that way I hide them for all users for all states. Is there another way how can I hide Create and Edit buttons depend on state and group role?

I tried to expand the base.xml template on conditions state is "approved" or "done" and the group role is purchase_request_user and view id is view_purchase_request_form "Create" and "Edit" button:

<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space='preserve'>
    <t t-extend="FormView.buttons">
        <t t-if="widget.fields_view.state !== 'done' or widget.fields_view.state !== 'approved'">
        <div class="o_form_buttons_view">
            <button t-if="widget.is_action_enabled('edit')"
                    type="button"
                    class="oe_form_button_edit btn btn-default btn-sm" accesskey="E">
                Edit
            </button>
            <button t-if="widget.is_action_enabled('create')"
                    type="button" class="oe_form_button_create btn btn-default btn-sm"
                    accesskey="C">
                Create
            </button>
        </div>
        </t>
    </t>
</templates>

added 'base' to dependences.

UPDATED base.xml Now I can change "Create" button name depend on my view name but nothing that depends on my module states.

<templates>
<t t-extend="FormView.buttons">
    <t t-jquery="button.oe_form_button_create" t-operation="replace">
        <t t-if="widget.fields_view.name == 'purchase.request.form'">
            <button t-if="widget.is_action_enabled('create')"
                    type="button" class="oe_form_button_create btn btn-default btn-sm"
                    accesskey="C">
                New button name
            </button>
        </t>
    </t>
 </t>
</templates>

My form view xml peace:

<openerp>
    <data>
        <record model="ir.ui.view" id="view_purchase_request_form">
            <field name="name">purchase.request.form</field>
            <field name="model">purchase.request</field>
            <field name="arch" type="xml">
                <form string="Purchase Request" create="false" edit="false">
                <header>
                    <button name="%(action_sale_order_reset)d" attrs="{'invisible': [('state','not in', ('to_approve_first'))]}" string="Reset" type="action" groups="purchase_request.group_purchase_request_manager"/>
                    <button name="button_to_approve_first" states="draft" string="Request approval" type="object" class="oe_highlight"/>
                    <button name="button_approved" states="to_approve_first" string="Approve" type="object" class="oe_highlight" groups="purchase_request.group_purchase_request_manager"/>
                    <button name="button_approvedd" states="approved" string="Return Request" type="object" class="oe_highlight" groups="purchase_request.group_purchase_request_manager"/>
                    <button name="button_create_order" states="approvedd" string="Create Order" type="object" class="oe_highlight" groups="purchase_request.group_purchase_request_user"/>
                    <button name="button_to_approve_second" states="create_order" string="Approve" type="object" class="oe_highlight" groups="purchase_request.group_purchase_request_manager"/>
                    <button name="button_approved2" states="to_approve_second" string="Done" type="object" class="oe_highlight" groups="purchase_request.group_purchase_request_manager"/>
                    <button name="button_rejected" states="draft,approvedd" string="Reject" type="object" groups="purchase_request.group_purchase_request_user"/>
                    <field name="state" widget="statusbar" statusbar_visible="draft,to_approve_first,approved,rejected" statusbar_colors="{&quot;approved&quot;:&quot;green&quot;}"/>
                </header>
                <sheet>
                    <group>
                        <group>
                            <field name="date_start" readonly="1"/>
                            <field name="participate_process"/>
                        </group>
                        <group>
                            <field name="requested_by" readonly="1"/>
                            <field name="assigned_to" attrs="{'readonly': [('state','not in', ('draft'))]}" />
                        </group>
                  <notebook>
                        <page string="Order" attrs="{'invisible': [('state','in', ('draft', 'to_approve_first', 'approved', 'approvedd'))]}">
                            <field name="supply_ids" attrs="{'readonly': [('state','not in', ('to_approve_second'))]}"/>
                        </page>
                   </notebook>
                </sheet>
                </form>
            </field>
        </record>
3

3 Answers

0
votes

You have to write a template inheriting the 'FormView.buttons' one, check the context fields, like groups and state, and put a condition based on the wanted fields.

Something like this:

<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space='preserve'>
    <t t-extend="FormView.buttons">
        <t t-if="widget.fields_view.state == <wanted_state>">
            <your operations>
        </t>
    </t>
</templates>
0
votes

Using this you can inspect all the FormView widget fields, you can find it in /odoo/addons/web/static/src/js/views/form_view.js.
I also found datarecord inside it, which should contain your state info and value.
Then, if you want to do the same with the buttons in edit mode, replace '.o_form_buttons_view' with '.o_form_buttons_edit'.

Tell me if you have problems. And again sorry for the delay, I was a little busy.

<t t-extend="FormView.buttons">
    <t t-jquery=".o_form_buttons_view" t-operation="before">
        <t t-log="widget"/>
    </t>
</t>
0
votes

Thank you! @Michele Zaccheddu using your answer I can get many types of the widget using

<t t-extend="FormView.buttons">
    <t t-jquery=".o_form_buttons_view" t-operation="before">
        <t t-log="widget"/>
    </t>
</t>

Just put the code as a test and after update apps - go to form view, check using browser Developer Tools, In the console you will get the list from "Class", I'm using odoo 11 and can target

<t t-if="widget.initialState.data.state === 'draft'">

For other versions, please check the t-log yourself it can be different, but I haven't check for user group it may or may not be there...