3

I want to override protected method by rewrite class in magento2. I want to overrride and change code when you save attribute multi select option from backend here is my code.

<?php 
 namespace Testmodule\Testattribute\Model\ResourceModel\Entity;
 class Attribute extends \Magento\Eav\Model\ResourceModel\Entity\Attribute 
 {
    protected function _updateAttributeOption($object, $optionId, $option)
    {
        //custom code 
    }
 }

Second method I want to rewrite for set attribute option value when you edit multi select attribute.

<?php
 namespace Testmodule\Testattribute\Block\Adminhtml\Attribute\Edit\Options;
 class Options extends \Magento\Eav\Block\Adminhtml\Attribute\Edit\Options\Options
 {
      protected $_template = 'Testmodule_TestAttribute::catalog/product/attribute/options.phtml';

      protected function _prepareUserDefinedAttributeOptionValues($option, $inputType, $defaultValues)
      {
            //custom code 
      }
 }

This is my di.xml

<?xml version="1.0"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Eav\Block\Adminhtml\Attribute\Edit\Options\Options" type="Drc\Attributeimage\Block\Adminhtml\Attribute\Edit\Options\Options" />
    <preference for="Magento\Eav\Model\ResourceModel\Entity\Attribute" type="Testmodule\Testattribute\Model\ResourceModel\Entity\Attribute" /> 
    <preference for="Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Form" type="Testmodule\Testattribute\Block\Adminhtml\Product\Attribute\Edit\Form" /> 
</config>

This is form file I want to change form enctype property

 <?php
  namespace Testmodule\Testattribute\Block\Adminhtml\Product\Attribute\Edit;

  class Form extends \Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Form
  {
     protected function _prepareForm()
     {
        $form = $this->_formFactory->create(
        ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'),'enctype'=>'multipart/form-data', 'method' => 'post']]
    );
       $form->setUseContainer(true);
       $this->setForm($form);
       return parent::_prepareForm();
     }
  }

Please Help me for override this protected methods in magento2.

  • what is the issue are available after use above methods any errors? – Rakesh Jesadiya Jun 14 '16 at 5:43
2

For first method override use below code,

<?php 
 namespace Testmodule\Testattribute\Model\ResourceModel\Entity;

use Magento\Eav\Model\Entity\Attribute as EntityAttribute;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Framework\DB\Select;
use Magento\Framework\Model\AbstractModel;

 class Attribute extends \Magento\Eav\Model\ResourceModel\Entity\Attribute 
 {
    protected function _updateAttributeOption($object, $optionId, $option)
    {
        //custom code 
    }
 }

For second method,

<?php
 namespace Testmodule\Testattribute\Block\Adminhtml\Attribute\Edit\Options;

 use Magento\Store\Model\ResourceModel\Store\Collection;

 class Options extends Magento\Eav\Block\Adminhtml\Attribute\Edit\Options\Options
 {
      protected $_template = 'Testmodule_TestAttribute::catalog/product/attribute/options.phtml';

      protected function _prepareUserDefinedAttributeOptionValues($option, $inputType, $defaultValues)
      {
            //custom code 
      }
 }

For form prepareform override use below methods,

<?php
namespace Testmodule\Testattribute\Block\Adminhtml\Product\Attribute\Edit;

use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Framework\Data\Form as DataForm;

  class Form extends Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Form
  {
     protected function _prepareForm()
     {
        $form = $this->_formFactory->create(
        ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'),'enctype'=>'multipart/form-data', 'method' => 'post']]
    );
       $form->setUseContainer(true);
       $this->setForm($form);
       return parent::_prepareForm();
     }
  }

Remove var folder and try again.

| improve this answer | |
  • other method is working but this method _updateAttributeOption() is not calling from my file. – Shyam Jun 14 '16 at 12:09

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.