Question

I want to create a new tab and some options in the product attribute.enter image description here

So, how can I add that tab with options, please give me some ideas or any reference? Thanks

Was it helpful?

Solution

Create plugin for add custom tab in product attribute form :

app/code/Vendor/Module/etc/adminhtml/di.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tabs">
        <plugin name="Vendor_Module::CustomTab"
                type="Vendor\Module\Plugin\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tabs"/>
    </type>
</config>

Plugin file :

app/code/Vendor/Module/Plugin/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tabs.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */


namespace Vendor\Module\Plugin\Catalog\Block\Adminhtml\Product\Attribute\Edit;

use Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tabs as AttributeEditTabs;

class Tabs
{
    /**
     * @param AttributeEditTabs $subject
     * @return array
     */
    public function beforeToHtml(AttributeEditTabs $subject)
    {
        $content = $subject->getChildHtml('customtab');
        $subject->addTabAfter(
            'vendor_module_tab',
            [
                'label' => __('Custom Tab'),
                'title' => __('Custom Tab'),
                'content' => $content,
            ],
            'front'
        );

        return [];
    }
}

UPDATE :

For display html content in your custom tab, create catalog_product_attribute_edit.xml file and add this below code

app/code/Vendor/Module/view/adminhtml/layout/catalog_product_attribute_edit.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="attribute_edit_tabs">
            <block class="Vendor\Module\Block\Adminhtml\Product\Attribute\Edit\Tab\CustomTab" as="customtab"/>
        </referenceBlock>
    </body>
</page>

Now, create CustomTab.php block file :

app/code/Vendor/Module/Block/Adminhtml/Product/Attribute/Edit/Tab/CustomTab.php

<?php

namespace Vendor\Module\Block\Adminhtml\Product\Attribute\Edit\Tab;

class CustomTab extends \Magento\Backend\Block\Widget\Form\Generic {

    protected $_adminSession;
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Backend\Model\Auth\Session $adminSession,
        array $data = []
    ) {
        $this->_adminSession = $adminSession;
        parent::__construct($context, $registry, $formFactory, $data);

    }

    /**
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    protected function _prepareForm() {
        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create();

        $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Tab Information')]);
        $fieldset->addField(
            'name',
            'text',
            [
                'name' => 'name',
                'label' => __('Name'),
                'title' => __('Name'),
                'required' => true,
                'value' => "Name",
            ]
        );
        $this->setForm($form);

        return parent::_prepareForm();
    }
    public function getTabLabel() {
        return __('Tab Information');
    }

    public function getTabTitle() {
        return __('Tab Information');
    }

    public function canShowTab() {
        return true;
    }

    public function isHidden() {
        return false;
    }

    protected function _isAllowedAction($resourceId) {
        return $this->_authorization->isAllowed($resourceId);
    }
}

You need to add more field in CustomTab.php based on your requirements.

Clean cache and check it. Hope, It will helpful for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top