0

How could we disable some input field for customer in admin form.For custom specific Roles Resources.

0

For achieving this things i have used the following steps.As we know basic structure of creating module step. so here i am just putting required files.

app/code/[Vendor_Name]/[Module_Name]/etc/acl.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
    <acl>
        <resources>
            <resource id="Magento_Backend::admin">
                <resource id="Magento_Customer::customer">
                    <resource id="Magento_Customer::manage">
                        <resource id="[Vendor_Name]_[Module_Name]::allow_editable_some_fields" title="Allow editable some fields" translate="title" sortOrder="110" />
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

app/code/[Vendor_Name]/[Module_Name]/view/base/ui_component/customer_form.xml

<?xml version="1.0" encoding="UTF-8"?>
    <!--
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    -->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="customer">
        <field name="[custom_attribute_field_name]" formElement="input" class="[Vendor_Name]/[Module_Name]\Ui\Component\Form\Field\DisableSomeField">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">customer</item>
                    <item name="sortOrder" xsi:type="number">400</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

app/code/[Vendor_Name]/[Module_Name]/Ui/Component/Form/Field/DisableSomeField.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace [Vendor_Name]\[Module_Name]\Ui\Component\Form\Field;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Framework\AuthorizationInterface;

/**
 * Process setting to set input field Disable according to roles resources
 *
 * Class [Vendor_Name]\[Module_Name]\Ui\Component\Form\Field\DisableSomeField
 */
class DisableSomeField extends \Magento\Ui\Component\Form\Field
{
    /**
     * @var AuthorizationInterface
     */
    private $authorization;

    /**
     * Constructor
     *
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param AuthorizationInterface $authorization
     * @param UiComponentInterface[] $components
     * @param array $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        AuthorizationInterface $authorization,
        array $components = [],
        array $data = []
    ) {
        $this->authorization = $authorization;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    /**
     * Prepare component configuration
     *
     * @return void
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function prepare()
    {
        parent::prepare();
        
        $isAllowed = $this->authorization->isAllowed('[Vendor_Name]_[Module_Name]::allow_editable_some_fields');
        if (!$isAllowed) {
            $currentConfig = $this->getData('config');
            $currentConfig['disabled'] = true;
            $this->setData('config', $currentConfig);
        }
    }
}

Disabling Magento default functionality group dropdown not editable in mass action and grid listing.Here is the code to achieve it.

Create File on the below location :

app/code/[Vendor]/[Module_Name]/view/adminhtml/ui_component/customer_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <listingToolbar name="listing_top">
        <massaction name="listing_massaction" component="Magento_Ui/js/grid/tree-massactions" class="[Vendor]\[Module_Name]\Ui\Component\MassAction\Group\DisableGroup">
            <action name="assign_to_group">
                <settings>
                    <type>assign_to_group</type>
                    <label translate="true">Assign a Customer Group</label>
                    <actions class="Magento\Customer\Ui\Component\MassAction\Group\Options"/>
                </settings>
            </action>
        </massaction>
    </listingToolbar>
    <columns name="customer_columns" class="Magento\[Module_Name]\Ui\Component\Listing\Columns">
        <column name="group_id" component="Magento_Ui/js/grid/columns/select" sortOrder="50" class="[Vendor]\[Module_Name]\Ui\Component\Listing\Column\DisableGroup">
            <settings>
                <filter>select</filter>
                <editor>
                    <editorType>select</editorType>
                </editor>
                <dataType>select</dataType>
                <label translate="true">Group</label>
            </settings>
        </column>
    </columns>
</listing>

app/code/[Vendor]/[Module_Name]/Ui/Component/MassAction/Group/DisableGroup.php

<?php
    
    namespace [Vendor]\[Module_Name]\Ui\Component\MassAction\Group;
    
    use Magento\Framework\View\Element\UiComponent\ContextInterface;
    use Magento\Framework\View\Element\UiComponentInterface;
    use Magento\Framework\AuthorizationInterface;
    
    /**
     * Process setting to set input field Disable according to roles resources
     *
     * Class \[Vendor]\[Module_Name]\Ui\Component\Listing\Column\DisableGroup
     */
    class DisableGroup extends \Magento\Ui\Component\MassAction
    {
    
        /**
         * @var AuthorizationInterface
         */
        private $authorization;
    
        /**
         * Constructor
         *
         * @param ContextInterface                      $context
         * @param AuthorizationInterface                $authorization
         * @param UiComponentInterface[]                $components
         * @param array                                 $data
         */
        public function __construct(
            ContextInterface $context,
            AuthorizationInterface $authorization,
            array $components,
            array $data
        ) {
            $this->authorization = $authorization;
            parent::__construct($context, $components, $data);
        }
    
        /**
         * Prepare component configuration
         *
         * @return void
         * @throws \Magento\Framework\Exception\LocalizedException
         */
        public function prepare()
        {
            parent::prepare();
            if (!$this->authorization->isAllowed('[Vendor_Name]_[Module_Name]::allow_editable_some_fields')) {
                $config = $this->getConfiguration();
                $notAllowedActions = ['assign_to_group'];
                $allowedActions = [];
                foreach ($config['actions'] as $action) {
                    if (!in_array($action['type'], $notAllowedActions)) {
                        $action['actionDisable'] = true;
                        $allowedActions[] = $action;
                    }
                }
                $config['actions'] = $allowedActions;
                $this->setData('config', (array)$config);
            }
        }
    }

Disable group select in customer grid :

app/code/[Vendor]/[Module_Name]/Ui/Component/Listing/Column/DisableGroup.php

<?php

declare(strict_types=1);

namespace [Vendor]\[Module_Name]\Ui\Component\Listing\Column;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\AuthorizationInterface;

/**
 * Process setting to set input field Disable according to roles resources
 *
 * Class \[Vendor]\[Module_Name]\Ui\Component\Listing\Column\DisableGroup
 */
class DisableGroup extends Column
{
    /**
     * @var AuthorizationInterface
     */
    private $authorization;

    /**
     * Constructor
     *
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param AuthorizationInterface $authorization
     * @param UiComponentInterface[] $components
     * @param array $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        AuthorizationInterface $authorization,
        array $components = [],
        array $data = []
    ) {
        $this->authorization = $authorization;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    /**
     * Prepare component configuration
     *
     * @return void
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function prepare()
    {
        parent::prepare();

        if (!$this->authorization->isAllowed('[Vendor_Name]_[Module_Name]::allow_editable_some_fields')) {
            $currentConfig = $this->getData('config');
            if (isset($currentConfig['editor']['editorType'])) {
                $currentConfig['editor']['editorType'] = false;
                $this->setData('config', $currentConfig);
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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