1

In the customer/edit page (adminhtml) I set up some custom fields, they appear by default under information account tab.

I created an observer (event is adminhtml_block_html_before), and I need to remove the fields from the form. I see there is a removeField method I could use, but I cannot find any explanation on how to do it from the observer.

2 Answers 2

3

you can try this

$this->getForm()->getElement('content_fieldset')->removeField('content_heading');

or this

// Remove the elementId from the form
$this->getForm()->removeField('password_fieldset');
// Remove the fieldset
$this->getForm()->getElements()->remove('password_fieldset');

or you can just disabled it

$event = $observer->getEvent();
$form = $event->getForm();    
$form->getElement('is_active')->setData('disabled', true);
2
3

@andhi Irawan you can achieve same thing in Magento 2 using below code.

create events.xml on path Namespace\ModuleName\etc\adminhtml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
   <event name="adminhtml_block_html_before">
       <observer name="namespace_moodulename_observer_hidefields" instance="Namespace\ModuleName\Observer\HideFields" />
   </event>
</config>

Create Observer file (Hidefields.php) on path Namespace\ModuleName\Observer
you can change the file name as per your choice.

<?php
namespace Namespace\ModuleName\Observer;

class HideFields implements \Magento\Framework\Event\ObserverInterface
{
    protected $_request;

    public function __construct(
        \Magento\Framework\App\Request\Http $request
    ){
        $this->_request = $request;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $acionName = $this->_request->getFullActionName();

        $ruleId = $this->_request->getParam('entity_id');
        // $actionName is your admin edit form action
        if($acionName === 'pricerules_lrpricerules_edit')
        {
            $block = $observer->getEvent()->getBlock();
            if (!isset($block))
                return $this;

            if ($block->getType() == 'Namespace\ModuleName\Block\Adminhtml\Lrpricerules\Edit\Tab\Main') 
            {
                $form = $block->getForm();
                $filedArray = array();
                // base_fieldset is the fieldset id defined in your form
                $fieldset = $form->getElement('base_fieldset');
                if($ruleId)
                {
                    // fields ids array to remove from form
                    $filedArray = array('field1','field2');


                    if(!empty($filedArray))
                    {
                        foreach($filedArray as $fieldId)
                        {
                           $fieldset->removeField($fieldId);
                        }   
                    }
                }
            }
        }   

        return $this;
    }
}

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.