0

I've added a custom image attribute to my category with Magento 2 Module Generator from https://mage2gen.com/ (Magento version 2.2) Everything is perfect, but when I upload an image to the attribute and save category, an error occurred:

Fatal error: Method Magento\Ui\TemplateEngine\Xhtml\Result::__toString() must not throw an exception, caught Error: Cannot use string offset as an array in C:\xampp\htdocs\magento2\vendor\magento\module-ui\Component\Wrapper\UiComponent.php on line 0

This is my InstallData.php

<?php
namespace Vendor\Module\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
class InstallData implements InstallDataInterface
{
 private $eavSetupFactory;
 public function __construct(EavSetupFactory $eavSetupFactory)
 {
 $this->eavSetupFactory = $eavSetupFactory;
 }
 public function install(
 ModuleDataSetupInterface $setup,
 ModuleContextInterface $context
 ) {
 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 $eavSetup->addAttribute(
 \Magento\Catalog\Model\Category::ENTITY,
 'thumbnail_image',
 [
 'type' => 'varchar',
 'label' => 'Thumbnail Image',
 'input' => 'image',
 'sort_order' => 5,
 'source' => '',
 'global' => 0,
 'visible' => true,
 'required' => false,
 'user_defined' => false,
 'default' => '',
 'group' => 'General Information',
 'backend' => 'Magento\Catalog\Model\Category\Attribute\Backend\Image'
 ]
 );
 }
}

And the category_form.xml

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="general">
 <field name="thumbnail_image">
 <argument name="data" xsi:type="array">
 <item name="config" xsi:type="array">
 <item name="required" xsi:type="boolean">false</item>
 <item name="validation" xsi:type="array">
 <item name="required-entry" xsi:type="boolean">false</item>
 </item>
 <item name="sortOrder" xsi:type="number">5</item>
 <item name="dataType" xsi:type="string">string</item>
 <item name="formElement" xsi:type="string">fileUploader</item>
 <item name="label" translate="true" xsi:type="string">Thumbnail Image</item>
 <item name="uploaderConfig" xsi:type="array">
 <item name="url" path="catalog/category_image/upload" xsi:type="url"/>
 </item>
 <item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
 <item name="previewTmpl" xsi:type="string">Magento_Catalog/image-preview</item>
 <item name="source" xsi:type="string">category</item>
 </item>
 </argument>
 </field>
</fieldset>

I am sure the value is saved OK (checked in database for eav_attribute, catalog_category_entity_varchar tables) and the image is uploaded into \pub\media\catalog\category

Thanks for any help!

Manashvi Birla
8,8739 gold badges29 silver badges53 bronze badges
asked Nov 27, 2017 at 9:04

1 Answer 1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.