0

I want to have an admin user who would only be able to view the 'Images And Videos' and 'Search Engine Optimiation' in the admin product edit form. enter image description here

I tried the following. Created app/code/Vendor/Module/view/adminhtml/layout/catalog_product_edit.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="page.main.actions">
            <block class="Vendor\Module\Block\Adminhtml\Permission" name="admin.permission" template="Vendor_Module::product_edit.phtml" after="-" />
        </referenceContainer>
    </body>
</page>

Created block file app/code/Vendor/Module/Block/Adminhtml/Permission.php

use Magento\Backend\Block\Template;
use Magento\Backend\Block\Template\Context;
use Magento\Backend\Model\Auth\Session as AdminSession;

class Permission extends Template
{
    /**
     * Constructor
     * 
     * @param Context      $context
     * @param AdminSession $adminSession
     * @param array        $data
     */
    public function __construct(
        Context $context,
        AdminSession $adminSession,
        array $data = []
    )
    {
        $this->adminSession = $adminSession;
        parent::__construct($context, $data);
    }

    /**
     * @return string
     */
    public function getCurrentAdminUserRole()
    {
        return $this->adminSession->getUser()->getRole()->getRoleName();
    }
}

Added template at app/code/Vendor/Module/view/adminhtml/templates/product_edit.phtml

<?php 
/**
 * @var $block \Vendor\Module\Block\Admin\Permission
 */
?>
<script type="text/x-magento-init">
    {
        "*": {
            "adminProductPermission": {
                "role":"<?= $block->getCurrentAdminUserRole() ?>"
            }
        }
    }
</script>

Added my js in require js app/code/Vendor/Module/view/adminhtml/requirejs-config.js

var config = {
    map: {
        '*': {
            adminProductPermission:'Vendor_Module/js/permission'
        }
    }
};

And finally in the JS file app/code/Vendor/Module/view/adminhtml/web/js/permission.js

define([
    'jquery'
], function ($) {
    return function(config) {
        var userRole = config.role;
        if(userRole == 'SEO') {
            $('.fieldset-wrapper div:not([data-index="gallery"]):not([data-index="search-engine-optimization"])').hide();
        }
    }
});

This is not working , as I believe the form is being loaded by KO js and it is being loaded after my js executed.

3 Answers 3

0

Please replace your permission.js this file code to the below code.

define([
'jquery'
], function ($) {
   return function(config) {
        var userRole = config.role;
        console.log(userRole);
        if(userRole == 'SEO') {
            setTimeout(function() {
                 $('[data-index="gallery"]').hide();
                 $('[data-index="search-engine-optimization"]').hide();
            }, 2000);
        }
    }
});

After applying the below commands.

sudo php bin/magento s:up && sudo php bin/magento setup:di:compile && sudo php bin/magento s:static-content:de -f && sudo php bin/magento c:f && sudo chmod -R 777 var/ pub/ generated/

Please try this code. let me know this is working or not. if my solution is working then accept my answer and give an upvote.

thanks

1
  • setTimeout works. Please update your answer to hide all the elements except 'gallery' & 'search-engine-optimisation'. Your code block hides the ones I require to show. Commented Jan 24 at 8:09
0

Using setTimeout from Mohit's answer, the Js is now applying to the code. Following is my code for file app/code/Vendor/Module/view/adminhtml/web/js/permission.js

define([
    'jquery'
], function ($) {
    return function(config) {
        var userRole = config.role;
        if(userRole == 'SEO') {
            setTimeout(function(){
                $('.entry-edit.form-inline > div:not([data-index="gallery"]):not([data-index="search-engine-optimization"])').hide();
            }, 5000);
        }
    }
});

Had to do slight modification to the element selector as well.

One flaw in this I found is that if the page load is slower that the setTimeout, the elements will remain visible.

1
  • i am also implementing your code in my module so it works for me. Commented Jan 24 at 8:40
0

Please follow below steps to make specific tab invisible without use to setTimeout.

  • Remove requirejs-config.js
  • add window.config global variable in product_edit.phtml so you can access role in js file
  • Add new file with below code in product_form.xml, this file located in view/adminhtml/ui_component/product_form.xml
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="salable_quantity">
        <field name="check_role" sortOrder="40" formElement="hidden" component="Vendor_Module/js/permission">
            <settings>
                <dataType>text</dataType>
                <label translate="true">For check the role</label>
            </settings>
        </field>
    </fieldset>
</form>

Update below code in permission.js

define([
'underscore',
'uiRegistry',
'Magento_Ui/js/form/element/abstract'], function (_, uiRegistry, abstract) {
'use strict';
return abstract.extend({
    initialize : function (){
        uiRegistry.get(function(component){
                if(component.componentType == 'fieldset'){
                    var fieldName = uiRegistry.get('index = '+ component.index);
                    fieldName.visible(false);
                }
        });
        this._super();
        return this;
    }
}); 
});
5
  • Your answer is not clear to me. Where is the product_edit.phtml file ? And what are the contents of the file ? You have shared xml content for that file Commented Jan 25 at 12:36
  • <script> window.adminrole = '<?php echo $block->getCurrentAdminUserRole(); ?>'; </script> Commented Jan 26 at 10:33
  • Path is same as you have used for this file. Commented Jan 26 at 10:34
  • I have updated the file path in answered Commented Jan 26 at 10:40
  • Thanks, will try this out Commented Jan 29 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.