0

I created a module that uses an Image Uploader UiComponent to upload an additional image to a category and save its path in the database.

The controller I use to do this is basically a copy of the out of the box Magento Upload.php controller with the parameter in the execute() method changed to the attribute ID of my custom attribute.

But when I upload images using both the out of the box image uploader for the Category Image and my additional uploader, for some reason the file paths change in unexpected ways after saving the category.

Here are some examples.

If I upload both images at the same time and save them, I get the expected result of:

[magento_category_image] => /media/catalog/category/magento_category_image.jpg
[my_category_image] => /media/catalog/category/my_category_image.jpg

But if I e.g. change one of the images afterwards and then save the category, for some reason the path of the other image in the database changes in unexpected ways, e.g.:

[magento_category_image] => /media/catalog/category/magento_category_image_1.jpg (this image is new)
[my_category_image] => pub/media/catalog/category/my_category_image.jpg (this one got affected too)

or something like this:

[magento_category_image] => /media/catalog/category/magento_category_image_1.jpg (this image is new)
[my_category_image] => pub/media/catalog/category//pub/media/catalog/category//my_category_image.jpg (this path changed in a weird way)

Here is my controller:

<?php

namespace Vendor\Controller\Controller\Adminhtml\Category\Image;

use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultFactory;

class Upload extends \Magento\Backend\App\Action implements HttpPostActionInterface
{
    protected $imageUploader;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Catalog\Model\ImageUploader $imageUploader
    ) {
        parent::__construct($context);
        $this->imageUploader = $imageUploader;
    }

    protected function _isAllowed()
      {
        return $this->_authorization->isAllowed('Magento_Catalog::categories');
      }

    public function execute()
    {
        $imageId = $this->_request->getParam('param_name', 'subcategory_image');

        try {
            $result = $this->imageUploader->saveFileToTmpDir($imageId);
        } catch (\Exception $e) {
            $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
        }
        return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
    }
}

di.xml

<virtualType name="Vendor\Module\CategoryImageUpload" type="Magento\Catalog\Model\ImageUploader">
        <arguments>
            <argument name="baseTmpPath" xsi:type="string">catalog/tmp/category</argument>
            <argument name="basePath" xsi:type="string">catalog/category</argument>
            <argument name="allowedExtensions" xsi:type="array">
                <item name="jpg" xsi:type="string">jpg</item>
                <item name="jpeg" xsi:type="string">jpeg</item>
                <item name="gif" xsi:type="string">gif</item>
                <item name="png" xsi:type="string">png</item>
            </argument>
            <argument name="allowedMimeTypes" xsi:type="array">
                <item name="jpg" xsi:type="string">image/jpg</item>
                <item name="jpeg" xsi:type="string">image/jpeg</item>
                <item name="gif" xsi:type="string">image/gif</item>
                <item name="png" xsi:type="string">image/png</item>
            </argument>
        </arguments>
    </virtualType>
    <type name="Vendor\Module\Controller\Adminhtml\Category\Image\Upload">
        <arguments>
            <argument name="imageUploader" xsi:type="object">Magento\Catalog\CategoryImageUpload</argument>
        </arguments>
    </type>

category_form.xml

<fieldset name="content">
        <field name="subcategory_image" formElement="imageUploader" sortOrder="3">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">category</item>
                </item>
            </argument>
            <settings>
                <elementTmpl>ui/form/element/uploader/image</elementTmpl>
                <dataType>string</dataType>
                <notice translate="true">This image will be displayed as a category menu item in the parent category</notice>
                <label translate="true">Category Menu Image</label>
                <componentType>imageUploader</componentType>
                <visible>true</visible>
                <required>false</required>
            </settings>
            <formElements>
                <imageUploader>
                    <settings>
                        <required>false</required>
                        <allowedExtensions>jpg jpeg gif png</allowedExtensions>
                        <maxFileSize>4194304</maxFileSize>
                        <uploaderConfig>
                            <param xsi:type="string" name="url">categorylist/category_image/upload</param>
                        </uploaderConfig>
                        <initialMediaGalleryOpenSubpath>catalog/category</initialMediaGalleryOpenSubpath>
                        <previewTmpl>Magento_Catalog/image-preview</previewTmpl>
                        <openDialogTitle>Media Gallery</openDialogTitle>
                    </settings>
                </imageUploader>
            </formElements>
        </field>
    </fieldset>

Does anyone know how to troubleshoot this or what might be the issue?

4
  • Are you able to share some of the code for the custom module so I can take a look? I recently added an image uploader so might be able to make some comparisons n spot what might be needed. – rushtoni88 Jan 20 at 11:40
  • @rushtoni88 Check my edited post! Thanks in advance. :) – Audio-Engineer Jan 20 at 12:03
  • Are you able to see if the image is being moved successfully out of the pub/media/tmp directory when you save? – rushtoni88 Jan 21 at 16:11
  • Hello, @rushtoni88. Yes, I can confirm that. The pictures always land where they belong in pub/media/catalog/category after saving. It's just the path in the database that gets messed up. – Audio-Engineer Jan 21 at 20:10

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.