0

I want to add some implementaion code in system configuration section for my custom module. How is that possible. Please help.

This is my block file

<?php

namespace Vendor\Module\Block\Adminhtml\System\Config;

use Magento\Framework\Data\Form\Element\AbstractElement;

class Implementationcode extends \Magento\Config\Block\System\Config\Form\Field
{
    /**
     * @var \Magento\Directory\Helper\Data
     */
    protected $_directoryHelper;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Directory\Helper\Data $directoryHelper
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Directory\Helper\Data $directoryHelper,
        array $data = []
    ) {
        $this->_directoryHelper = $directoryHelper;
        parent::__construct($context, $data);
    }

    /**
     * @param AbstractElement $element
     * @return string
     */
    protected function _getElementHtml(AbstractElement $element)
    {
        $html = parent::_getElementHtml($element);
        $html .= "<div>Code for Implementing Responsive Banner Slider</div>";
        return $html;
    }
}

This is my system.xml file

 <group id="slider" translate="label" type="text" sortOrder="11" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Implementaion Code</label>
                <field id="slider" translate="label" type="hidden" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">                   
                    <source_model>Vendor\Respbannerslider\Block\Adminhtml\System\Config\Implementationcode</source_model>
                </field>
            </group>
1
  • you want to add your own field in configuration and then need to use that data in your block ? – Manthan Dave Jan 25 '17 at 9:49
1

Your code won't work because you specified a source model.

As a reminder:

  • Source models: a model class that serves to get existing values (stored in the db or somewhere else) for further displaying inside the setting’s field. Example: the yes/no source model.
  • Frontend models : as a rule, it’s a block’s class. Methods of this class return html of setting’s field. To be more specific, the block had to have method _getElementHtml() described inside the class which returns the raw html of setting’s field
  • Backend models : a class which allows to operate with configuration data on the different stages (save, load). It contains three major methods respectively for each event: _afterLoad(), _beforeSave() and _afterSave().

In your case, you need to use a frontend model:

<group id="slider" translate="label" type="text" sortOrder="11" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Implementaion Code</label>
    <field id="slider" translate="label" type="hidden" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">                   
        <frontend_model>Cartin24\Respbannerslider\Block\Adminhtml\System\Config\Implementationcode</frontend_model>
    </field>
</group>
0

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.