2

I want to override protected prepareform() method because I want to add new fields so how to override it.

2 Answers 2

1

If anyone else needs a more complete answer based on '@gajjala sandeep' response:

<?php
namespace XXX\XXX\Plugin\Amasty\Label\Block\Adminhtml\Labels\Edit\Tab;

class Conditions
{
    /**
     * @var \Magento\Framework\Registry
     */
    private $registry;

    /**
     * @param \Magento\Framework\Registry $registry
     */
    public function __construct(
        \Magento\Framework\Registry $registry
    ) {
        $this->registry = $registry;
    }

    /**
     * @param \Amasty\Label\Block\Adminhtml\Labels\Edit\Tab\Conditions $subject
     * @param \Closure $proceed
     * @return string
     */
    public function aroundGetFormHtml( 
        \Amasty\Label\Block\Adminhtml\Labels\Edit\Tab\Conditions $subject,
        \Closure $proceed
    ) { 
        $form = $subject->getForm();
        if (is_object($form)) { 
            /* Get the fieldset by the parent form's fieldset name */
            $fieldset = $form->getElement('conditions_fieldset');
            $fieldset->addField( 
                'fieldname', 'textarea', 
                ['name' => 'fieldname', 'label' => __('Story B'), 'title' => __('Story B') ]
            ); 

            $model = $this->registry->registry('current_amasty_label');
            $form->addValues($model->getData()); // Using addData() to keep previous data but you can add only your data like: addData(['fieldname' => 'value'])
            $subject->setForm($form); 
        } 
        
        return $proceed(); 
    }
}

Declare the plugin on XXX/XXX/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="Amasty\Label\Block\Adminhtml\Labels\Edit\Tab\Conditions">
        <plugin name="xxx_xxx_edit_tab_conditions" type="XXX\XXX\Plugin\Amasty\Label\Block\Adminhtml\Labels\Edit\Tab\Conditions" />
    </type>
</config>

I hope that helps who come here in the future :)

0

You can override prepareform() function as per the following code:

First, You need to override the 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">
<preference for="Vendor\ExtensionName\Block\Adminhtml\ABC\XYZ\Tab\Test" type="OverrideVendor\OverrideExtensionName\Block\Adminhtml\ABC\XYZ\Tab\Test" />
</config>

Then you need to a file OverrideVendor\OverrideExtensionName\Block\Adminhtml\ABC\XYZ\Tab\Test.php

<?php

namespace OverrideVendor\OverrideExtensionName\Block\Adminhtml\ABC\XYZ\Tab;

class Test extends \Vendor\ExtensionName\Block\Adminhtml\ABC\XYZ\Tab\Test
{
    protected function _prepareForm()
    {

        $fieldset->addField(
            'add_field', 'text', array(
                'label'    => __('New Fiedl'),
                'name'     => 'add_field',
            )
        );

        $this->setForm($form);
        return $this;

    }
}

Let me know if you have any other issues.

Thanks

7
  • 1
    Hi, @shankar thanks for the reply I have already completed this task using plugin and thanks for the reply :) Commented May 27, 2019 at 9:05
  • @gajjalasandeep I'm glad to know that you fixed the issue but can you please also share your answer? Thanks :)
    – Shankar
    Commented May 28, 2019 at 3:46
  • 2
    <type name="Vendor\Module\Block\Adminhtml\Post\Edit\Tab\Main"><plugin name="custom" type="Custom\Module\Plugin\Block\Adminhtml\Post\Edit\Tab\Main" /></type> ****plugin file **** public function aroundGetFormHtml( \Vendor\Module\Block\Adminhtml\Post\Edit\Tab\Main $subject,\Closure $proceed){ $form = $subject->getForm(); if (is_object($form)) { $fieldset->addField( 'customerscallout', 'textarea', ['name' => 'story', 'label' => __('Story B'), 'title' => __('Story B') ]); $form->setValues($model->getData()); $subject->setForm($form); } return $proceed(); Commented May 28, 2019 at 8:59
  • Above is the code which I used. Commented May 28, 2019 at 9:11
  • Thanks for sharing your solution :)
    – Shankar
    Commented May 28, 2019 at 9:45

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.