0

I recently chanced upon a tutorial in datasheat[dot]com to add a custom tab into the Product edit/create form. I was able to make it work, but the issue I found is that the code has been added to the Magento Catalog component. Can any of you point out how I can move the added code into the component I created?

This is the code that was added (based on the tutorial):

  1. Add code to magento\module-catalog\view\adminhtml\layout\catalog_product_new.xml

    <block class="Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\CustomTab" name="product_tabs.customtab" as="custom-tab">
        <arguments>
            <argument name="label" xsi:type="string" translate="true">Custom Tab</argument>
        </arguments>
    </block>
    
  2. Create CustomTab.php in Magento\Catalog\Block\Adminhtml\Product\Edit\Tab

    <?php
    namespace Magento\Catalog\Block\Adminhtml\Product\Edit\Tab;
    
    class CustomTab extends \Magento\Backend\Block\Widget
    {
        protected $_template = 'catalog/product/tab/customtab.phtml';
    
        public function __construct(
            \Magento\Backend\Block\Template\Context $context,
            array $data = []
        ) {
           parent::__construct($context, $data);
          }
     }
    
  3. Create file customtab.phtml in magento\module-catalog\view\adminhtml\templates\catalog\product\tab

    <fieldset class="fieldset form-inline">
        <legend class="legend"><span><?php echo __('Custom Tab') ?></span></legend>
        <br>
        <div id="table_cataloginventory">
            <div class="field">
                <label class="label" for="custom_first">
                    <span><?php  echo __('FIrst') ?></span>
                </label>
                <div class="control">
                    <input type="text" class="input-text" id="custom_first" name="first" value="first">
                </div>
            </div>
            <div class="field">
                <label class="label" for="custom_second">
                    <span><?php  echo __('Second') ?></span>
                </label>
                <div class="control">
                    <input type="text" class="input-text" id="custom_second" name="second" value="second">
                </div>
            </div>
        </div>
    </fieldset>
    
  4. Add code below inside Magento\Catalog\Block\Adminhtml\Product\Edit\Tabs.php

     $ This-> AddTab (
          'Custom-tab'
          [
          'Label' => __ ( 'Custom Tab')
          'Content' => $ this -> _ translateHtml (
          $ This-> getChildHtml ( 'custom-tab')
          )
          'Group_code' => self :: BASIC_TAB_GROUP_CODE
          ]
          );
    
0

Here you can find a complete solution "How to create own module" where you can add your tab.

At addition, try to change:

$ This-> AddTab (
      'Custom-tab'
      [
      'Label' => __ ( 'Custom Tab')
      'Content' => $ this -> _ translateHtml (
      $ This-> getChildHtml ( 'custom-tab')
      )
      'Group_code' => self :: BASIC_TAB_GROUP_CODE
      ]
      );

to the:

$this->addTab(
    'Custom-tab',
    [
        'Label' => __( 'Custom Tab'),
        'Content' => $this->__translateHtml(
            $this->getChildHtml('custom-tab')
        ),
        'Group_code' => self::BASIC_TAB_GROUP_CODE
    ]
);
  • Hi Siarhey, yeah thats the code I had, but I pasted the wrong source code here sorry. Anyways, I already have my own module. What I am after is where I should add the code I added in catalog_product_new.xml and Tabs.php if I were to move them to my own module. You have any idea? Thanks. – Rahahaha Apr 17 '16 at 4:54
  • @Rahahaha Does your module already has the adminhtml part (blocks, controllers)? – Siarhey Uchukhlebau Apr 17 '16 at 7:34
  • No it does have. The module I have does not have catalog_product_new.xml and Tabs.php. Those 2 files are found inside the Module/Catalog directory from the sample components. The problem is, those 2 files have other codes inside of them so I am not sure what to do. – Rahahaha Apr 17 '16 at 7:40

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.