2

I want to add custom field in Customer admin and i need to process some operations but those operations should not to save in database.

I am following this tutorial https://webkul.com/blog/tab-with-form-in-admin-customer-edit-magento-2-0/

But not able to figure it out. i don't know how to get that data ?. Also in this code while initialise $this->initForm(); data is not posted from this form.

Also not able to figure it out where $rowcom is coming from .

Any help would be helpful .

1 Answer 1

3

Just change class following way:


namespace VendorName\ModuleName\Block\Adminhtml\Customer\Edit;

use Magento\Customer\Controller\RegistryConstants;
use Magento\Ui\Component\Layout\Tabs\TabInterface;
use Magento\Backend\Block\Widget\Form;
use Magento\Backend\Block\Widget\Form\Generic;
/**
 * Customer account form block
 */
class Tabs extends Generic implements TabInterface
{
    /**
     * @var \Magento\Store\Model\System\Store
     */
    protected $_systemStore;
    /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Store\Model\System\Store $systemStore,

        array $data = []
    ) {
        $this->_coreRegistry = $registry;
        $this->_systemStore = $systemStore;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * @return string|null
     */
    public function getCustomerId()
    {
        return $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
    }

    /**
     * @return \Magento\Framework\Phrase
     */
    public function getTabLabel()
    {
        return __('Demo Tab');
    }

    /**
     * @return \Magento\Framework\Phrase
     */
    public function getTabTitle()
    {
        return __('Demo Tab');
    }

    /**
     * @return bool
     */
    public function canShowTab()
    {
        if ($this->getCustomerId()) {
            return true;
        }
        return false;
    }

    /**
     * @return bool
     */
    public function isHidden()
    {
        if ($this->getCustomerId()) {
            return false;
        }
        return true;
    }

    /**
     * Tab class getter
     *
     * @return string
     */
    public function getTabClass()
    {
        return '';
    }

    /**
     * Return URL link to Tab content
     *
     * @return string
     */
    public function getTabUrl()
    {
        return '';
    }

    /**
     * Tab should be loaded trough Ajax call
     *
     * @return bool
     */
    public function isAjaxLoaded()
    {
        return false;
    }
    public function initForm()
    {
        if (!$this->canShowTab()) {
            return $this;
        }
        /**@var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create();
        $form->setHtmlIdPrefix('myform_');

        $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Fields Information')]);

        $fieldset->addField(
            'demo_field',
            'text',
            [
                'name' => 'demo_field',
                'data-form-part' => $this->getData('target_form'),
                'label' => __('Demo Field in Customer Section'),
                'title' => __('Demo Field in Customer Section')
            ]
        );
        $this->setForm($form);
        return $this;
    }
    /**
     * @return string
     */
    protected function _toHtml()
    {
        if ($this->canShowTab()) {
            $this->initForm();
            return parent::_toHtml();
        } else {
            return '';
        }
    }
}

Make sure namespace match your module.

After that you can able to get this field value by


/** @var \Magento\Framework\App\RequestInterface $request */
$request = $this->getRequest();
$originalRequestData = $request->getPostValue();
echo $originalRequestData['demo_field'];

I have test this M2.2

[Update]

Create a plugin for getting data during save.

VendorName/ModuleName/etc/adminhtml/di.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Controller\Adminhtml\Index\Save">
        <plugin name="sr_customer_save" type="VendorName\ModuleName\Plugin\Customer\Controller\Adminhtml\Index\Save" sortOrder="1"/>
    </type>
</config>

VendorName/ModuleName/Plugin/Customer/Controller/Adminhtml/Index/Save.php


namespace VendorName\ModuleName\Plugin\Customer\Controller\Adminhtml\Index;

class Save
{
    public function afterExecute(
        \Magento\Customer\Controller\Adminhtml\Index\Save $subject,
        $result
    ) {
        $demoField = $subject->getRequest()->getParam('demo_field');
        error_log($demoField);
        return $result;
    }
}
4
  • Where should I put this code then
    – Rohit
    Commented Feb 20, 2018 at 10:06
  • Check updated answer
    – Sohel Rana
    Commented Feb 20, 2018 at 17:01
  • Hi @SohelRana, I have tried this code. but when save customer I can't get any params of the custom field. Can you please help me?
    – R P
    Commented Dec 19, 2018 at 9:58
  • @RP, I know it's late. But it might help others if they will find the same issue as you and i got. You just need to add this line of code in the in your custom field along with other parameters in the array. 'data-form-part' => $this->getData('target_form') Commented Sep 5, 2020 at 6:36

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.