3

I've created a custom module to get an additional image uploader on my categories using the following tutorial.

https://belvg.com/blog/how-to-add-image-attribute-to-a-category-in-magento-2.html

It was missing some files which I needed which I've mentioned below but I've got this working.

routes.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="categorythumbnailimage" frontName="categorythumbnailimage">
            <module name="Vendor_CategoryThumbnailImage" before="Magento_Backend" />
        </route>
    </router>
</config>

Data.php (Helper)

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\CategoryThumbnailImage\Helper;

use Magento\Catalog\Api\CategoryRepositoryInterface;

/**
 * Catalog Thumbnail Image data helper
 * @SuppressWarnings(PHPMD.TooManyFields)
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * Store manager
     *
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

     public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager
        ){
        $this->_storeManager = $storeManager;
        parent::__construct($context);
    }


    public function getCategoryThumbUrl($category)
    {

        $image = $category->getData('image_thumb');

        if (!empty($image)) {
            return $this->getImagePath() . $image;
        } else {
            throw new \Magento\Framework\Exception\LocalizedException(
                    __('Something went wrong while getting the image url.')
            );
        }

    }

     public function getImagePath()
    {
        return $this->_storeManager->getStore()->getBaseUrl(
                        \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
                ) . 'catalog/category/';

    }
}

Everything is working as expected, the attribute is created and saves to the database and I am able to get the image displayed on the frontend.

The issue I am having is that I need two additional image fields on my categories. I duplicated the module and set up exactly the same as my other module but for some reason I receive the following error when both modules are enabled, although they are both working correctly when either one is enabled with the other disabled.

The error appears when trying to load a category in the admin that has had an image uploaded for both of the image fields?

Uncaught TypeError: value.map is not a function
    at UiClass.setInitialValue (http://domain.dev/products/pub/static/version1498826880/adminhtml/Magento/backend/en_US/Magento_Ui/js/form/element/file-uploader.js:69:27)
    at UiClass.initialize (http://domain.dev/products/pub/static/version1498826880/adminhtml/Magento/backend/en_US/Magento_Ui/js/form/element/abstract.js:70:18)
    at UiClass.initialize (http://domain.dev/products/pub/static/version1498826880/adminhtml/Magento/backend/en_US/mage/utils/wrapper.js:109:34)
    at new UiClass (http://domain.dev/products/pub/static/version1498826880/adminhtml/Magento/backend/en_US/Magento_Ui/js/lib/core/class.js:49:32)
    at Object.initComponent (http://domain.dev/products/pub/static/version1498826880/adminhtml/Magento/backend/en_US/Magento_Ui/js/core/renderer/layout.js:66:25)
    at fire (http://domain.dev/products/pub/static/version1498826880/adminhtml/Magento/backend/en_US/jquery.js:3232:32)
    at Object.fireWith [as resolveWith] (http://domain.dev/products/pub/static/version1498826880/adminhtml/Magento/backend/en_US/jquery.js:3362:8)
    at Object.deferred.(anonymous function) (http://domain.dev/products/pub/static/version1498826880/adminhtml/Magento/backend/en_US/jquery.js:3461:37)
    at fire (http://domain.dev/products/pub/static/version1498826880/adminhtml/Magento/backend/en_US/jquery.js:3232:32)
    at Object.fireWith [as resolveWith] (http://domain.dev/products/pub/static/version1498826880/adminhtml/Magento/backend/en_US/jquery.js:3362:8)

I really have no idea why these are not working correctly when you have an image uploaded for both image attributes?

Could anyone please advise where I am going wrong and how it is possible to have multiple image attributes assigned to the category?

Any help would be much appreciated :)

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.