1

Good evening. I just recently started learning magento 2 and faced the following problem. I have an XML form that contains information, such as "Name, address, telephone" I need to create a block that will return which fields will be in the fieldset, but I dont know how I think that I'm calling the wrong class, I ask for help. Ty my code:

form.xml

<fieldset class="[Vendor]\[Module]\Block\Adminhtml\[Module]\Edit\Tab\Checkbox" />

di.xml

<preference for="Magento\Tax\Block\Adminhtml\Rate\Form" type="[Vendor]\[Module]\Block\Adminhtml\[Module]\Edit\Tab\Checkbox" />

[Vendor][Module]\Block\Adminhtml[Module]\Edit\Tab\Checkbox.php

class Checkbox extends \Magento\Tax\Block\Adminhtml\Rate\Form
{

    protected $_taxData = null;

    /**
     * @var \Magento\Tax\Block\Adminhtml\Rate\Title\FieldsetFactory
     */
    protected $_fieldsetFactory;

    /**
     * @var \Magento\Directory\Model\Config\Source\Country
     */
    protected $_country;

    /**
     * @var \Magento\Directory\Model\RegionFactory
     */
    protected $_regionFactory;

    /**
     * @var \Magento\Tax\Api\TaxRateRepositoryInterface
     */
    protected $_taxRateRepository;

    /**
     * @var \Magento\Tax\Model\TaxRateCollection
     */
    protected $_taxRateCollection;

    /**
     * @var \Magento\Tax\Model\Calculation\Rate\Converter
     */
    protected $_taxRateConverter;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param \Magento\Directory\Model\RegionFactory $regionFactory
     * @param \Magento\Directory\Model\Config\Source\Country $country
     * @param \Magento\Tax\Block\Adminhtml\Rate\Title\FieldsetFactory $fieldsetFactory
     * @param \Magento\Tax\Helper\Data $taxData
     * @param \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository
     * @param \Magento\Tax\Model\TaxRateCollection $taxRateCollection
     * @param \Magento\Tax\Model\Calculation\Rate\Converter $taxRateConverter
     * @param array $data
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Directory\Model\RegionFactory $regionFactory,
        \Magento\Directory\Model\Config\Source\Country $country,
        \Magento\Tax\Block\Adminhtml\Rate\Title\FieldsetFactory $fieldsetFactory,
        \Magento\Tax\Helper\Data $taxData,
        \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository,
        \Magento\Tax\Model\TaxRateCollection $taxRateCollection,
        \Magento\Tax\Model\Calculation\Rate\Converter $taxRateConverter,
        array $data = []
    ) {
        parent::__construct($context, $registry, $formFactory, $regionFactory, $country, $fieldsetFactory, $taxData, $taxRateRepository, $taxRateCollection, $taxRateConverter, $data);

    }


    /**
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
    }


    public function _prepareForm()
    {
        parent::_prepareForm();

            $form = $this->getForm();
            $fieldset = $form->п('new_fieldset',
                array('legend'=>'GST Tax Rate Information')
            );
            $fieldset->addField('hsn_title', 'text',
                array(
                    'label' => 'HSN Code',
                    'class' => 'input-text',
                    'name'  => 'hsn_title',
                    'required' => false
                )
            );

            $this->setForm($form);
            return $this;
    }
  • add xml form... – Rk Rathod May 11 at 13:24
0

custom filed in admin grid can be added in XML file. This XML file is created under view/adminhtml/layout and also view/adminhtml/layout/ui_component folders.

In view/adminhtml/layout create layout file which should be something like this

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <body>
        <referenceContainer name="content">
            <uiComponent name="frontname_controlername_listing"/>
        </referenceContainer>
    </body>
</page>

In view/adminhtml/layout/ui_component folder

create a file which should be mentioned in above layout file in this example it is frontname_controlername_listing

this XML file includes many things and you just wanted custom field to add so adding code specific to that.

<column name="name of filed">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="sorting" xsi:type="string">asc</item>
                    <item name="label" xsi:type="string" translate="true">Name of filed </item>
                </item>
            </argument>
        </column>

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.