0

I am trying to add a custom field to Admin Store Edit

I have overridden the block function _prepareStoreFieldset locate in vendor/magento/module-backend/Block/System/Store/Edit/Form/Website.php

This is my 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\Backend\Block\System\Store\Edit\Form\Website">
        <preference for="Magento\Backend\Block\System\Store\Edit\Form\Website" type="Vendor\Module\Block\System\Store\Edit\Form\Website" />
    </type>
</config>

Then I created a file in Vendor\Module\Block\System\Store\Edit\Form

<?php

namespace Vendor\Module\Block\System\Store\Edit\Form;

/**
 * Adminhtml store edit form for the website
 *
 * @author      Magento Core Team <[email protected]>
 *
 * @SuppressWarnings(PHPMD.DepthOfInheritance)
 */
class Website extends \Magento\Backend\Block\System\Store\Edit\Form\Website
{
    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param \Magento\Store\Model\GroupFactory $groupFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Store\Model\GroupFactory $groupFactory,
        array $data = []
    ) {
        parent::__construct($context, $registry, $formFactory, $groupFactory, $data);
    }

    /**
     * Prepare website specific fieldset
     *
     * @param \Magento\Framework\Data\Form $form
     * @return void
     */
    public function _prepareStoreFieldset(\Magento\Framework\Data\Form $form)
    {
        $fieldset = $form->getForm()->getElement('website_fieldset');

        $fieldset->addField(
            'website_type',
            'text',
            [
                'name' => 'website[type]',
                'label' => __('Type'),
                'value' => 1,
                'required' => true,
                'disabled' => false
            ]
        );
    }
}

But it did not work. Anywhere I am wrong? Please help me. Thanks

4
  • where you create di.xml? Commented Aug 17, 2020 at 12:12
  • I created in etc/adminhtml Commented Aug 17, 2020 at 12:23
  • This is not your main issue, but you shouldn't add custom files in vendor, otherwise they will be erased the next time you will run composer. You need to override in one of your custom module out of vendor.
    – Claims
    Commented Aug 17, 2020 at 12:28
  • ah, I didn't add custom file in vendor, I created file in app/code/vendor/module, just describe which file I want to override Commented Aug 17, 2020 at 12:34

2 Answers 2

4

Try the following way using event observer:

Step 1: app/code/VendorName/ModuleName/etc/adminhtml/events.xml

    <?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_store_edit_form_prepare_form">
        <observer name="vm_adminhtml_store_edit_form_prepare_form"
                  instance="VendorName/ModuleName\Observer\AdminhtmlStoreEditFormPrepareForm"/>
    </event>
</config>

Step 2: app/code/VendorName/ModuleName/Observer/AdminhtmlStoreEditFormPrepareForm.php

    <?php
declare(strict_types=1);

namespace VendorName\ModuleName\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Backend\Block\System\Store\Edit\Form\Store as StoreForm;
use Magento\Framework\Registry;

class AdminhtmlStoreEditFormPrepareForm implements ObserverInterface
{
    /**
     * @var Registry
     */
    private $registry;

    /**
     * AdminhtmlStoreEditFormPrepareForm constructor.
     *
     * @param Registry $registry
     */
    public function __construct(
        Registry $registry
    ) {
        $this->registry = $registry;
    }

    /**
     * @param Observer $observer
     * @return $this|void
     */
    public function execute(Observer $observer)
    {
        $block = $observer->getBlock();
        if ($block instanceof StoreForm) {
            $form = $block->getForm();
            $fieldset = $form->getElement('store_fieldset');
            $storeModel = $this->registry->registry('store_data');
            $fieldset->addField('website_type', 'text', array(
                'name'      => 'store[type]',
                'label'     => __('Type'),
                'value'     => $storeModel->getType(),
                'required'  => false,
            ));
        }
        return $this;
    }
}
3
  • This works perfectly fine. And I feel this is the best way to add a new field. Preference will cause conflicts when we have multiple modules doing the same operation. Observers will not cause any conflicts and will fulfill the purpose.
    – Jason
    Commented Oct 8 at 8:52
  • There are other changes needed to create this field type? Commented Nov 11 at 16:29
  • @EliasSoares you need to create a new module called VendorName/ModuleName, then add above two files
    – Sohel Rana
    Commented Nov 12 at 8:56
0

if you want to create override the things, then you need to write this into your di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Backend\Block\System\Store\Edit\Form\Website" type="Vendor\Module\Block\System\Store\Edit\Form\Website" /> 
</config>

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.