2

I am getting the following error on di compile:

**Extra parameters passed to parent construct $directoryHelper**

That's the original code:

class PreviewButton extends \Magento\Config\Block\System\Config\Form\Field
{
    
    protected $blockFactory;
   
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\View\Element\BlockFactory $blockFactory,
        array $data = [],
        ?SecureHtmlRenderer $jsonHelper = null,
        ?DirectoryHelper $directoryHelper = null
    ) {
        $this->blockFactory = $blockFactory;
        parent::__construct($context, $data, $jsonHelper, $directoryHelper);
    }

i extended it and my construct is :

class PreviewButton extends OriginalPreviewButton
{
    protected $_directoryHelper;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\View\Element\BlockFactory $blockFactory,
        array $data = [],
        ?SecureHtmlRenderer $jsonHelper = null,
        ?DirectoryHelper $directoryHelper = null
    ) {
        OriginalPreviewButton::__construct($context, $blockFactory, $data, $jsonHelper);
        $this->blockFactory = $blockFactory;

    }

but i am still getting the same error. Can someone help to fix the error? Thanks in advance

3 Answers 3

0

Try with below code:

class PreviewButton extends OriginalPreviewButton
{
    protected $_directoryHelper;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\View\Element\BlockFactory $blockFactory,
        array $data = [],
        ?SecureHtmlRenderer $jsonHelper = null,
        ?DirectoryHelper $directoryHelper = null
    ) {
        parent::__construct($context, $data, $jsonHelper, $directoryHelper);
        $this->blockFactory = $blockFactory;

    }
0

You have to move the '$data' array to last param as given below.

 class PreviewButton extends \Magento\Config\Block\System\Config\Form\Field {
    
    protected $blockFactory;
    
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\View\Element\BlockFactory $blockFactory,
        SecureHtmlRenderer $jsonHelper = null,
        DirectoryHelper $directoryHelper = null,
        array $data = []
    ) {
        $this->blockFactory = $blockFactory;
        parent::__construct($context, $data);
    }
}
2
  • This doesnt work unfortunately.
    – Someguy
    Oct 17, 2023 at 10:50
  • @Someguy Please try it now answer updated. Oct 18, 2023 at 5:07
0

There is an error in your "base" class constructor when you are calling its parent method:

class PreviewButton extends \Magento\Config\Block\System\Config\Form\Field
{
    protected $blockFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\View\Element\BlockFactory $blockFactory,
        array $data = [],
        ?SecureHtmlRenderer $jsonHelper = null,
        ?DirectoryHelper $directoryHelper = null
    ) {
        $this->blockFactory = $blockFactory;
        parent::__construct($context, $data, $jsonHelper, $directoryHelper); // here
    }
}

As I can see from this class on Magento 2.4.6, it has a different set of arguments:

class Field extends \Magento\Backend\Block\Template implements
    \Magento\Framework\Data\Form\Element\Renderer\RendererInterface
{
    /**
     * @var SecureHtmlRenderer
     */
    private $secureRenderer;

    /**
     * @param Context $context
     * @param array $data
     * @param SecureHtmlRenderer|null $secureRenderer
     */
    public function __construct(
        Context $context,
        array $data = [],
        ?SecureHtmlRenderer $secureRenderer = null
    ) {
        parent::__construct($context, $data);
        $this->secureRenderer = $secureRenderer ?? ObjectManager::getInstance()->get(SecureHtmlRenderer::class);
    }
}

You get an error on the $directoryHelper (4th argument when only 3 exist in the original class). Try to change your "base" class code to this one, run setup:di:compile and test:

class PreviewButton extends \Magento\Config\Block\System\Config\Form\Field
{
    protected $blockFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\View\Element\BlockFactory $blockFactory,
        array $data = [],
        ?SecureHtmlRenderer $jsonHelper = null,
        ?DirectoryHelper $directoryHelper = null
    ) {
        $this->blockFactory = $blockFactory;
        parent::__construct($context, $data); // changed
    }
}

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.