8

Has anyone successfully uploaded an image for use with a category? When going through the documentation @ http://devdocs.magento.com/swagger/index.html#/ I don't see any implementation that could support it.

Also when retrieving a category using Magento REST API you don't get the category image back.

In the admin page, you can add the category here: enter image description here

Gerard de Visser
  • 925
  • 1
  • 10
  • 27
emp
  • 304
  • 3
  • 17
  • Unfortunately we only had capacity to do the catalog extension objects at the time and haven't gotten to this yet. It would be great if someone can send in a PR. Thanks, Chuck – Chuck Jan 11 '16 at 17:36

2 Answers2

6

At the moment this is not possible without extending core functionality. However, category image save/get support can be added to category REST API using extension attributes mechanism:

  1. Declare extension attribute of string type for \Magento\Catalog\Api\Data\CategoryInterface (it will hold base64-encoded image)
  2. Write afterSave plugin for \Magento\Catalog\Api\CategoryRepositoryInterface::save to save image
  3. Write afterLoad plugin for \Magento\Catalog\Api\CategoryRepositoryInterface::get to load image

See more detailed instructions on declaring extension attributes in this answer

Alex Paliarush
  • 13,751
  • 5
  • 51
  • 55
  • Thanks, that will probably do the job but I don't have that much knowledge to write a PR. I guess I'm left without the functionality then. – emp Jan 12 '16 at 16:15
0

Here's an example how you can extend Magento functionality to import the category thumbnail image of a category using extension attribute with base64 string. You can adjust it per your own requirements.

Vendor/Module/etc/extension_attributes.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Catalog\Api\Data\CategoryInterface">
        <attribute code="thumbnail_name" type="string"/>
        <attribute code="thumbnail_base64_string" type="string"/>
    </extension_attributes>
</config>

Vendor/Module/etc/webapi_rest/di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\CategoryRepository">
        <plugin name="category_process_base64_thumbnail" type="Vendor\Module\Plugin\Model\CategoryRepositoryPlugin"/>
    </type>
</config>

Vendor/Module/Plugin/Model/CategoryRepositoryPlugin:

<?php

declare(strict_types=1);

namespace Vendor\Module\Plugin\Model;

use Magento\Catalog\Api\Data\CategoryExtension; use Magento\Catalog\Api\Data\CategoryInterface; use Magento\Catalog\Model\CategoryRepository; use Magento\Framework\Api\Data\ImageContentInterface; use Magento\Framework\Api\ImageProcessorInterface; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Exception\InputException;

class CategoryRepositoryPlugin { public function __construct( private readonly ImageContentInterface $imageContent, private readonly ImageProcessorInterface $imageProcessor ) { }

/**
 * @param CategoryRepository $subject
 * @param CategoryInterface  $category
 *
 * @return CategoryInterface[]|null
 */
public function beforeSave(CategoryRepository $subject, CategoryInterface $category): ?array
{
    /** @var CategoryExtension $categoryExtensionAttributes */
    $categoryExtensionAttributes = $category-&gt;getExtensionAttributes();

    $this-&gt;imageContent
        -&gt;setName($categoryExtensionAttributes-&gt;getThumbnailName())
        -&gt;setBase64EncodedData($categoryExtensionAttributes-&gt;getThumbnailBase64String())
        -&gt;setType('image/jpeg');

    try {
        $entityType = 'catalog/category';
        $uploadedFileName = $this-&gt;imageProcessor-&gt;processImageContent(
            $entityType,
            $this-&gt;imageContent
        );
    } catch (InputException $e) {
        return null;
    }

    /** @var \Magento\Catalog\Model\Category $category */
    $category-&gt;setData(
        'thumbnail',
        DIRECTORY_SEPARATOR . DirectoryList::MEDIA . DIRECTORY_SEPARATOR . $entityType . $uploadedFileName
    );

    return [$category];
}

}

Also at the Vendor/Module/registration.php and Vendor/Module/etc/module.xml files and run bin/magento setup:upgrade and bin/magento cache:flush.

After that you can do a POST request to categories endpoint with payload like:

{
  "category": {
    "parent_id": 2,
    "name": "Category Test",
    "is_active": true,
    "level": 1,
        "extension_attributes": {
            "thumbnail_name": "category-test-thumbnail.jpeg",
            "thumbnail_base64_string": "/9j/....."
        }
    }
}

The image will be saved as pub/media/catalog/category/category-test-thumbnail.jpeg and added as thumbnail image to the category.

Gerard de Visser
  • 925
  • 1
  • 10
  • 27