Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento 2, custom widget, add parameter as textarea

I create a custom widget on magento 2, and I want to use a field for text as (textarea). I try to use:

parameter name="text" xsi:type="textarea" visible="true" sort_order="3"

But I got this error: The QName value 'textarea' of the xsi:type attribute does not resolve to a type definition.

How can I use a textarea on a widget field, on magento 2?

Thank you

like image 284
mvistas Avatar asked May 20 '16 11:05

mvistas


1 Answers

Set the field you want to be a text area to type=block, example:

<parameter name="text" xsi:type="block" required="true" visible="true" sort_order="3">
            <label translate="true">Text</label>

            <block class="Namespace\Modulename\Block\Adminhtml\Widget\TextField"/>
        </parameter> 

Now create the new block "TextField.php", like this:

namespace Namespace\Modulename\Block\Adminhtml\Widget;
Class TextField extends \Magento\Backend\Block\Template{
protected $_elementFactory;
/**
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Framework\Data\Form\Element\Factory $elementFactory
 * @param array $data
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Framework\Data\Form\Element\Factory $elementFactory,
    array $data = []
) {
    $this->_elementFactory = $elementFactory;
    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)
{
    $input = $this->_elementFactory->create("textarea", ['data' => $element->getData()]);
    $input->setId($element->getId());
    $input->setForm($element->getForm());
    $input->setClass("widget-option input-textarea admin__control-text");
    if ($element->getRequired()) {
        $input->addClass('required-entry');
    }

    $element->setData('after_element_html', $input->getElementHtml());
    return $element;
}



}
like image 114
mvistas Avatar answered Oct 21 '22 17:10

mvistas