I am working on an extension which offers custom widgets that include wysiwyg editor and image fields:

<widget id="masonry_category" class="MyCompany\Module\Block\Widget\Category" is_email_compatible="true">
<label translate="true">Masonry: Category</label>
<description translate="true">Category Image with Link</description>
<parameters>
    ...
    <parameter name="featured_image" xsi:type="image" visible="true" required="false">
        <label translate="true">Category Image</label>
    </parameter>
</parameters>

However, as far as I can see, we are not allowed to use these types:

Element 'parameter', attribute '{http://www.w3.org/2001/XMLSchema-instance}type': The QName value 'image' of the xsi:type attribute does not resolve to a type definition.

I see some rules for widgets in Magento_Widget:etc/widget.xsd, but I am not sure how would I go about extending this. Unfortunately, the official documentation didn't help me to figure this out.

BTW, one very weird thing about this: I don't see these errors on my local environment (with development mode turned ON + PHP mode for displaying all errors). And my types (editor and image) are working fine. I run into trouble as soon as I deploy the code to our staging environment, which doesn't make sense to me.

up vote 3 down vote accepted

Found the solution and wrote a blog post http://mageclass.com/quick-tip-how-to-use-image-wyswyg-or-any-other-non-standard-field-in-magento2-widget/. I am not sure if it's in line with the best practices, the only requirement I had was not to touch the core.

  • I've tried to add this to my widget, and all seemed to work. However when I re-open my widget, then delete an image and try and access that widget again, I get the following error "Exception #0 (Exception): Notice: Array to string conversion in XXXXXXXX/vendor/magento/framework/Data/Form/Element/Image.php on line 51" - can you confirm that this is not a bug with your code? Still struglling with Magento 2, and so there is every chance it is my implementation, but I want to rule out a bug first. Thanks. – user3207332 Jan 17 '17 at 22:03

By default, the input types are:

  • text
  • textarea
  • select
  • multiselect

However, you can declare another types, for example using render function in the block, in my opinion is the best way.

First step, write a block, in this case is for render a date field:

class Theme_Module_Block_Date extends Mage_Adminhtml_Block_Widget_Form_Renderer_Fieldset_Element
    {
        public function render(Varien_Data_Form_Element_Abstract $element)
        {
            $element->setType('date');
            $this->_element = $element;

            return $this->toHtml();
        }
    }

Second step you must declare the input in widget.xml:

<your_widget>
    <date>
      ...
      <type>theme_module/date</type>
      ...
    </date>
</your_widget>

Another examples to render:

class Theme_Module_Block_Hour extends Mage_Adminhtml_Block_Widget_Form_Renderer_Fieldset_Element
{

    public function render(Varien_Data_Form_Element_Abstract $element)
    {
        $element->setType('time');
        $this->_element = $element;

        return $this->toHtml();
    }
}

In widget.xml:

... theme_module/hour ...

I hope this may help you.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.