cancel
Showing results for 
Search instead for 
Did you mean: 

How do I add an image upload attribute to a category

How do I add an image upload attribute to a category

im looking to add an additional image upload field into my categories. I have had a look around but can't find a solution that works.

 

I'm running version 2.1

 

Any ideas on how I would achieve this ?

 

Cheers

3 REPLIES

Re: How do I add an image upload attribute to a category

Hello matthans0n,

 

We think you need create the simple module.

In order to add an attribute to  a category in Magento 2.1 - it should be created in  InstallData and also described with the help of UI Component:

 

app/code/YOUR/MODULE/Setup/InstallData.php

 

$eavSetup->addAttribute(
    \Magento\Catalog\Model\Category::ENTITY,
    'attribute_name',
    [
        'type' => 'text',
        'input' => 'fileUploader',
        'label' => 'Image Field',
        'backend' => '',
        'frontend' => '',
        'group' => 'Group Name',
        'class' => '',
        'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
        'visible' => true,
        'required' => false,
        'user_defined' => '',
        'default' => '',
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => false,
        'used_in_product_listing' => false,
        'unique' => false,
        'apply_to' => '',
        'position' => 250
    ]
);

app/code/YOUR/MODULE/view/adminhtml/ui_component/category_form.xml

 

<field name="attribute_name">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="dataType" xsi:type="string">string</item>
            <item name="source" xsi:type="string">category</item>
            <item name="label" xsi:type="string" translate="true">Image Field</item>
            <item name="visible" xsi:type="boolean">true</item>
            <item name="formElement" xsi:type="string">fileUploader</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="required" xsi:type="boolean">false</item>
            <item name="sortOrder" xsi:type="number">100</item>
            <item name="uploaderConfig" xsi:type="array">
                <item name="url" xsi:type="url" path="catalog/category_image/upload"/>
            </item>
        </item>
    </argument>
</field>

Hope it will help.

 

Regards,
Plumrocket Team

 

Re: How do I add an image upload attribute to a category

I want to do the same thing.  Add an option for another image upload within the Category section.  I have followed the above directions making a basic module.  However, after running 

 

bin/magento setup:upgrade

 

and

 

bin/magento setup:static-content:deploy

 

I do not see anything added on the admin page.  When I ran the upgrade command, I did not see the module listed either.  Is there more to do?

Re: How do I add an image upload attribute to a category

I made a module shell with the code provided above.  When I enable and update, I get these errors on the Category page.  I do not understand what is happening.  Any suggestions?

 

1 exception(s):
Exception #0 (Exception): Warning: DOMDocument::loadXML(): Namespace prefix xsi for type on argument is not defined in Entity, line: 2 in /home/swimmers/public_html/vendor/magento/framework/View/Element/UiComponent/Config/DomMerger.php on line 324

Exception #0 (Exception): Warning: DOMDocument::loadXML(): Namespace prefix xsi for type on argument is not defined in Entity, line: 2 in /home/swimmers/public_html/vendor/magento/framework/View/Element/UiComponent/Config/DomMerger.php on line 324
#0 [internal function]: Magento\Framework\App\ErrorHandler->handler(2, 'DOMDocument::lo...', '/home/swimmers/...', 324, Array)
#1 /home/swimmers/public_html/vendor/magento/framework/View/Element/UiComponent/Config/DomMerger.php(324): DOMDocument->loadXML('<field name="at...')
#2 /home/swimmers/public_html/vendor/magento/framework/View/Element/UiComponent/Config/DomMerger.php(395): Magento\Framework\View\Element\UiComponent\Config\DomMerger->createDomDocument('<field name="at...')
#3 /home/swimmers/public_html/vendor/magento/framework/View/Element/UiComponent/Config/Reader.php(60): Magento\Framework\View\Element\UiComponent\Config\DomMerger->merge('<field name="at...')
#4 /home/swimmers/public_html/vendor/magento/framework/View/Element/UiComponent/Config/Reader.php(47): Magento\Framework\View\Element\UiComponent\Config\Reader->readFiles(Array)
#5 /home/swimmers/public_html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(93): Magento\Framework\View\Element\UiComponent\Config\Reader->__construct(Object(Magento\Framework\View\Element\UiComponent\Config\FileCollector\AggregatedFileCollector), Object(Magento\Framework\View\Element\UiComponent\Config\Converter), Object(Magento\Framework\View\Element\UiComponent\Config\DomMerger))
#6 /home/swimmers/public_html/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(89): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Magento\\Framewo...', Array)
#7 /home/swimmers/public_html/vendor/magento/framework/ObjectManager/ObjectManager.php(57): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Magento\\Framewo...', Array)
#8 /home/swimmers/public_html/vendor/magento/framework/View/Element/UiComponent/Config/ReaderFactory.php(40): Magento\Framework\ObjectManager\ObjectManager->create('Magento\\Framewo...', Array)
#9 /home/swimmers/public_html/vendor/magento/module-ui/Model/Manager.php(244): Magento\Framework\View\Element\UiComponent\Config\ReaderFactory->create(Array)
#10 /home/swimmers/public_html/vendor/magento/module-ui/Model/Manager.php(258): Magento\Ui\Model\Manager->getReader('category_form')
#11 /home/swimmers/public_html/vendor/magento/module-ui/Model/Manager.php(166): Magento\Ui\Model\Manager->prepare('category_form')
#12 /home/swimmers/public_html/vendor/magento/framework/View/Element/UiComponentFactory.php(144): Magento\Ui\Model\Manager->prepareData('category_form')
#13 /home/swimmers/public_html/vendor/magento/framework/View/Layout/Generator/UiComponent.php(125): Magento\Framework\View\Element\UiComponentFactory->create('category_form', NULL, Array)
#14 /home/swimmers/public_html/vendor/magento/framework/View/Layout/Generator/UiComponent.php(93): Magento\Framework\View\Layout\Generator\UiComponent->generateComponent(Object(Magento\Framework\View\Layout\Data\Structure), 'category_form', Array, Object(Magento\Framework\View\Layout\Interceptor))
#15 /home/swimmers/public_html/vendor/magento/framework/View/Layout/GeneratorPool.php(86): Magento\Framework\View\Layout\Generator\UiComponent->process(Object(Magento\Framework\View\Layout\Reader\Context), Object(Magento\Framework\View\Layout\Generator\Context))
#16 /home/swimmers/public_html/vendor/magento/framework/View/Layout.php(327): Magento\Framework\View\Layout\GeneratorPool->process(Object(Magento\Framework\View\Layout\Reader\Context), Object(Magento\Framework\View\Layout\Generator\Context))
#17 /home/swimmers/public_html/var/generation/Magento/Framework/View/Layout/Interceptor.php(89): Magento\Framework\View\Layout->generateElements()
#18 /home/swimmers/public_html/vendor/magento/framework/View/Layout/Builder.php(129): Magento\Framework\View\Layout\Interceptor->generateElements()
#19 /home/swimmers/public_html/vendor/magento/framework/View/Page/Builder.php(55): Magento\Framework\View\Layout\Builder->generateLayoutBlocks()
#20 /home/swimmers/public_html/vendor/magento/framework/View/Layout/Builder.php(65): Magento\Framework\View\Page\Builder->generateLayoutBlocks()
#21 /home/swimmers/public_html/vendor/magento/framework/View/Layout.php(244): Magento\Framework\View\Layout\Builder->build()
#22 /home/swimmers/public_html/vendor/magento/framework/View/Layout.php(859): Magento\Framework\View\Layout->build()
#23 /home/swimmers/public_html/var/generation/Magento/Framework/View/Layout/Interceptor.php(414): Magento\Framework\View\Layout->getBlock('menu')
#24 /home/swimmers/public_html/vendor/magento/module-backend/Model/View/Result/Page.php(59): Magento\Framework\View\Layout\Interceptor->getBlock('menu')
#25 /home/swimmers/public_html/var/generation/Magento/Backend/Model/View/Result/Page/Interceptor.php(24): Magento\Backend\Model\View\Result\Page->setActiveMenu('Magento_Catalog...')
#26 /home/swimmers/public_html/vendor/magento/module-catalog/Controller/Adminhtml/Category/Edit.php(102): Magento\Backend\Model\View\Result\Page\Interceptor->setActiveMenu('Magento_Catalog...')
#27 /home/swimmers/public_html/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Catalog\Controller\Adminhtml\Category\Edit->execute()
#28 /home/swimmers/public_html/var/generation/Magento/Catalog/Controller/Adminhtml/Category/Edit/Interceptor.php(26): Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor->___callPlugins('execute', Array, Array)
#29 /home/swimmers/public_html/vendor/magento/framework/App/Action/Action.php(102): Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor->execute()
#30 /home/swimmers/public_html/vendor/magento/module-backend/App/AbstractAction.php(226): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#31 /home/swimmers/public_html/vendor/magento/framework/Interception/Interceptor.php(74): Magento\Backend\App\AbstractAction->dispatch(Object(Magento\Framework\App\Request\Http))
#32 /home/swimmers/public_html/vendor/magento/framework/Interception/Chain/Chain.php(70): Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor->___callParent('dispatch', Array)
#33 /home/swimmers/public_html/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Catalog...', 'dispatch', Object(Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor), Array, 'adminAuthentica...')
#34 /home/swimmers/public_html/vendor/magento/module-backend/App/Action/Plugin/Authentication.php(143): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#35 /home/swimmers/public_html/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\Backend\App\Action\Plugin\Authentication->aroundDispatch(Object(Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#36 /home/swimmers/public_html/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Catalog...', 'dispatch', Object(Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor), Array, 'log')
#37 /home/swimmers/public_html/vendor/magento/module-logging/App/Action/Plugin/Log.php(69): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#38 /home/swimmers/public_html/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\Logging\App\Action\Plugin\Log->aroundDispatch(Object(Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#39 /home/swimmers/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Catalog...', 'dispatch', Object(Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor), Array, 'adminMassaction...')
#40 /home/swimmers/public_html/vendor/magento/module-backend/App/Action/Plugin/MassactionKey.php(33): Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#41 /home/swimmers/public_html/vendor/magento/framework/Interception/Interceptor.php(142): Magento\Backend\App\Action\Plugin\MassactionKey->aroundDispatch(Object(Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#42 /home/swimmers/public_html/var/generation/Magento/Catalog/Controller/Adminhtml/Category/Edit/Interceptor.php(39): Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor->___callPlugins('dispatch', Array, Array)
#43 /home/swimmers/public_html/vendor/magento/framework/App/FrontController.php(55): Magento\Catalog\Controller\Adminhtml\Category\Edit\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#44 /home/swimmers/public_html/vendor/magento/framework/Interception/Interceptor.php(74): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#45 /home/swimmers/public_html/vendor/magento/framework/Interception/Chain/Chain.php(70): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#46 /home/swimmers/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'install')
#47 /home/swimmers/public_html/vendor/magento/framework/Module/Plugin/DbStatusValidator.php(69): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#48 /home/swimmers/public_html/vendor/magento/framework/Interception/Interceptor.php(142): Magento\Framework\Module\Plugin\DbStatusValidator->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#49 /home/swimmers/public_html/var/generation/Magento/Framework/App/FrontController/Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#50 /home/swimmers/public_html/vendor/magento/framework/App/Http.php(135): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#51 /home/swimmers/public_html/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
#52 /home/swimmers/public_html/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#53 {main}

 

Module Download:  https://drive.google.com/open?id=0B7Wz9s91WvqDcno3Q19YQldXT1k