0

I have a custom widget in which I need to create the checkbox field in one of my widget. I am using parameter type as block

Below is my code

1 . widget.xml file

<widget class="Sandesh\ProductWidgets\Block\Widget\WhatMakesItFabulous" id="what_makes_it_fabulous">
        <label>What Makes It Fabulous</label>
        <description>what makes it fabulous</description>
        <parameters>
            <parameter name="checkbox" xsi:type="block" visible="true" required="true" sort_order="10">
                <label translate="true">Select Checkbox</label>
                <block class="Sandesh\ProductWidget\Block\Adminhtml\Widget\CheckBoxChooser" />
            </parameter>
        </parameters>
</widget>

  1. CheckBoxChooser filer
use Magento\Backend\Block\Template;
use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Framework\Data\Form\Element\AbstractElement as Element;
use Magento\Framework\Data\Form\Element\Factory;
use Magento\Framework\Json\Helper\Data as JsonHelper;

class CheckBoxChooser extends Template
{

/**
     * @var Template\Context
     */
    private $context;
    /**
     * @var array
     */
    private $data;
    /**
     * @var JsonHelper|null
     */
    private $jsonHelper;
    /**
     * @var DirectoryHelper|null
     */
    private $directoryHelper;
    /**
     * @var Factory
     */
    private $elementFactory;

    /**
     * @param Template\Context $context
     * @param Factory $elementFactory
     * @param array $data
     * @param JsonHelper|null $jsonHelper
     * @param DirectoryHelper|null $directoryHelper
     */
    public function __construct(
        Template\Context $context,
        Factory $elementFactory,
        array $data = [],
        ?JsonHelper $jsonHelper = null,
        ?DirectoryHelper $directoryHelper = null
    ) {
        parent::__construct($context, $data, $jsonHelper, $directoryHelper);
        $this->context = $context;
        $this->data = $data;
        $this->jsonHelper = $jsonHelper;
        $this->directoryHelper = $directoryHelper;
        $this->elementFactory = $elementFactory;
    }

    public function prepareElementHtml(Element $element): Element
    {
        $config = $this->_getData('config');
        $input = $this->elementFactory->create('checkbox', ['data' => $element->getData()]);
        $input->setForm($element->getForm());
        $input->setConfig($config);
        $element->setData(
            'after_element_html',
            $input->getElementHtml()
        );
        return $element;
    }

Below is my output I can see my checkbox but even after checking and save it wont show it as checked after coming back

Ouput of widget after using this code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.