0

I have created an option on my custom widget that uses WYSIWYG. All works fine with the depends in as far as if it matches it shows and if it doesn't it hides the field. However, if it does not match the label still shows.

widget.xml:

<?xml version="1.0" ?>
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
    <widget class="Vendor\Module\Block\Widget\Content" id="Vendor_Module">
        <label>Blocks</label>
        <description>example...</description>
        <parameters>
            <!-- Content Type -->
            <parameter name="dropdown" sort_order="20" visible="true" required="true" xsi:type="select">
                <label>Type of content</label>
                <options>
                    <option name="option1" value="option1" selected="true">
                        <label>option 1</label>
                    </option>
                    <option name="option2" value="option2">
                        <label>option 2</label>
                    </option>
                    <option name="option3" value="option3">
                        <label>option 3</label>
                    </option>
                </options>
            </parameter>
            <!-- First Block -->
            <parameter name="content" sort_order="20" visible="true" required="false" xsi:type="block">
                <label>Content</label>
                <block class="Vendor\Module\Block\Widget\Editor" />
            </parameter>
            <!-- Second Block -->
            <parameter name="second_content" sort_order="20" visible="true" required="false" xsi:type="block">
                <label>Second Block</label>
                <depends>
                    <parameter name="dropdown" value="option3"/>
                </depends>
                <block class="Vendor\Module\Block\Widget\Editor" />
            </parameter>
        </parameters>
    </widget>
</widgets>

Editor.php:

<?php

namespace Vendor\Module\Block\Widget;

class Editor extends \Magento\Backend\Block\Widget\Form\Element 
{

    /**
     * @var \Magento\Cms\Model\Wysiwyg\Config
     */
    protected $_wysiwygConfig;

    /**
     * @var Factory
     */
    protected $_factoryElement;

    /**
     * @param Factory $factoryElement
     * @param CollectionFactory $factoryCollection
     * @param array $data
     */
    public function __construct(
    \Magento\Backend\Block\Template\Context $context, \Magento\Framework\Data\Form\Element\Factory $factoryElement, \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig, $data = []
    ) {
        $this->_factoryElement = $factoryElement;
        $this->_wysiwygConfig = $wysiwygConfig;
        parent::__construct($context, $data);
    }

    /**
     * Prepare chooser element HTML
     *
     * @param \Magento\Framework\Data\Form\Element\AbstractElement $element Form Element
     * @return \Magento\Framework\Data\Form\Element\AbstractElement
     */
    public function prepareElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element) {
        $editor = $this->_factoryElement->create('editor', ['data' => $element->getData()])
                ->setLabel('')
                ->setForm($element->getForm())
                ->setWysiwyg(true)
                ->setConfig($this->_wysiwygConfig->getConfig());

        if ($element->getRequired()) {
            $editor->addClass('required-entry');
        }

        $element->setData(
                'after_element_html', $this->_getAfterElementHtml() . $editor->getElementHtml()
        );

        return $element;
    }

    /**
     * @return string
     */
    protected function _getAfterElementHtml() {
        $html = <<<HTML
    <style>
        .admin__field-control.control .control-value {
            display: none !important;
        }
    </style>
HTML;

        return $html;
    }

}

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.