Skip to content

Deleting uploaded image from custom image upload field on CMS page in Magento 2.4

I’ve created a module to add a custom image upload field to a CMS page. The image displays in the admin and I can save the image but not able to delete it. I remove the image by clicking the bin/trash icon and save the CMS page but the image is still shown. I’ve got a plugin with a data provider afterGetData function and a page repository beforeSave function.

Example/CmsPageImage/Plugin/Cms/Model/Page/DataProvider/ModifyImageDataPlugin.php

    <?php

namespace ExampleCmsPageImagePluginCmsModelPageDataProvider;

use ExampleCmsPageImageModelImageUploader;
use MagentoCmsModelPageDataProvider;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkUrlInterface;
use MagentoStoreModelStoreManagerInterface;

class ModifyImageDataPlugin
{
    /**
     * @var StoreManagerInterface
     */
    private StoreManagerInterface $storeManager;

    /**
     * @param StoreManagerInterface $storeManager
     */
    public function __construct(StoreManagerInterface $storeManager)
    {
        $this->storeManager = $storeManager;
    }

    /**
     * @param DataProvider $subject
     * @param $loadedData
     * @return array
     * @throws NoSuchEntityException
     */
    public function afterGetData(
        DataProvider $subject,
                     $loadedData
    ): array
    {
        /** @var array $loadedData */
        if (is_array($loadedData) && count($loadedData) == 1) {
            $itemKey = 'image';

            foreach ($loadedData as $key => $item) {
                if (isset($item[$itemKey]) && $item[$itemKey]) {
                    unset($loadedData[$key][$itemKey]);

                    $loadedData[$key][$itemKey] = [
                        0 => [
                            'name' => $item[$itemKey],
                            'type' => 'image',
                            'url' => $this->storeManager->getStore()
                                    ->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) .
                                ImageUploader::IMAGE_PATH . DIRECTORY_SEPARATOR . $item[$itemKey]
                        ]
                    ];
                }
            }
        }

        return $loadedData;
    }
}

Example/CmsPageImage/Plugin/Cms/Model/PageRepository/SaveImagePlugin.php

    <?php

namespace ExampleCmsPageImagePluginCmsModelPageRepository;

use ExampleCmsPageImageModelImageUploader;
use MagentoCmsApiDataPageInterface;
use MagentoCmsModelPageRepository;
use MagentoFrameworkAppRequestInterface;
use MagentoFrameworkExceptionLocalizedException;

class SaveImagePlugin
{
    /**
     * @var ImageUploader
     */
    private ImageUploader $imageUploader;

    /**
     * @var RequestInterface
     */
    private RequestInterface $request;

    /**
     * @param ImageUploader $imageUploader
     * @param RequestInterface $request
     */
    public function __construct(
        ImageUploader $imageUploader,
        RequestInterface $request
    ) {
        $this->imageUploader = $imageUploader;
        $this->request = $request;
    }

    /**
     * @param PageRepository $subject
     * @param PageInterface $page
     * @return array
     * @throws LocalizedException
     */
    public function beforeSave(
        PageRepository $subject,
        PageInterface  $page
    ): array
    {
        $data = $page->getData();
        $key = 'image';

        if (empty($data[$key])) {
            unset($data[$key]);
            $data[$key]['delete'] = true;
        }

        if (isset($data[$key]) && is_array($data[$key])) {
            if (!empty($data[$key]['delete'])) {
                $data[$key] = null;
            } else if (isset($data[$key][0]['name']) && isset($data[$key][0]['tmp_name'])) {
                $data[$key] = $this->imageUploader->moveFileFromTmp($data[$key][0]['name']);
            } else if (isset($data[$key][0]['url'])) {
                $data[$key] = basename($data[$key][0]['url']);
            } else {
                unset($data[$key]);
            }
        }

        $page->setData($data);

        return [$page];
    }
}