2

I'm trying to disable the download button located into the attachment preview widget of Odoo (Pdf_viewer) as you can see into the code below:

 <field name="preview" attrs="{'readonly': [('preview', '=', True)]}" widget="pdf_viewer" />

PS: I tried to set the field to readonly but still give for the reader the entire file.

enter image description here

I had a little help from a friend that suggested of changing the JS file containing the template of this button, but I don't know the steps to do that! Thank you for your helps.

Kenly
  • 24,317
  • 7
  • 44
  • 60
Sadiki Ayoub
  • 101
  • 1
  • 10

1 Answers1

2

You can override the _disableButtons function and hide the download button.

Example:

var basic_fields = require('web.basic_fields');

basic_fields.FieldPdfViewer.include({
    _disableButtons: function (iframe) {
        $(iframe).contents().find('button#download').hide();
        // $(iframe).contents().find('button#secondaryDownload').hide();
        this._super(iframe);
    },
});

If you need to control the download button visibility using the context attribute, try the following code:

var basic_fields = require('web.basic_fields');
var Context = require('web.Context');

basic_fields.FieldPdfViewer.include({
    _disableButtons: function (iframe) {
        var self = this;
        if (self.attrs.context) {
            var context = new Context(self.attrs.context).eval();
            if(!context.download) {
                $(iframe).contents().find('button#download').hide();
                // $(iframe).contents().find('button#secondaryDownload').hide();
            }
        }
        this._super(iframe);
    },
});

Edit:

Create an XML file with the following content and add it to the data entry in the manifest file:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <template id="assets_backend" inherit_id="web.assets_backend" name="assets_backend">
            <xpath expr="." position="inside">
                <script type="text/javascript"
                        src="/module_name/static/src/js/pdf_viewser.js"></script>
            </xpath>
        </template>
    </data>
</odoo>

Create pdf_viewser.js under static/src/js and add the above code:

odoo.define('module_name.PDFViewer', function (require) {
    "use strict";
    
    var basic_fields = require('web.basic_fields');

    basic_fields.FieldPdfViewer.include({
        _disableButtons: function (iframe) {
            $(iframe).contents().find('button#download').hide();
            // $(iframe).contents().find('button#secondaryDownload').hide();
            this._super(iframe);
        },
    });

});

The steps are listed in the Adding files in an asset bundle section. For more details check the Assets Management documentation.

Kenly
  • 24,317
  • 7
  • 44
  • 60
  • Great thanks @kenly, i just add the line into the existed _disableButtons function like so: _disableButtons: `function (iframe) { $(iframe).contents().find('button#openFile').hide(); $(iframe).contents().find('button#download').hide(); },` and it work the question is if i want to use overide methode should i put your code under the `var FieldPdfViewer = FieldBinaryFile.e..` or out side the cote? – Sadiki Ayoub May 05 '21 at 10:07
  • Do you want to apply your modifications without editing the original file? – Kenly May 05 '21 at 10:35
  • yes, like it is shown ih this link : [How to extend / add new widget in chrome in pos](https://www.odoo.com/fr_FR/forum/aide-1/how-to-inherit-js-file-in-odoo-113575) – Sadiki Ayoub May 05 '21 at 10:41
  • 1
    Check my edit. I also added a link to the official documentation. – Kenly May 05 '21 at 11:02