How to Replace an Upload Image Field with a Page Builder Area in Magento Admin

Table of Contents

  1. Introduction
  2. The Limitation of Magento’s Category Image Field
  3. Setting Up Your Custom Module
  4. Removing the Original Image Uploader
  5. Ensuring Content Saves Correctly
  6. Conclusion
  7. FAQ
Shopify - App image

Introduction

Do you find Magento's limited image field options restrictive, especially when you need different images for desktop and mobile views in categories? Many Magento users face the challenge of adapting the platform's capabilities to meet specific design requirements. In this blog post, we'll walk you through the process of replacing an upload image field with a Page Builder area in the Magento admin. This step-by-step guide is designed to help you add a new level of customization to your Magento store, enhancing user experience across various devices.

Our focus will be on:

  • Understanding the current limitations of Magento's category image options
  • Setting up a custom module to implement new features
  • Removing unwanted elements and ensuring proper functionality

By the end of this article, you’ll be equipped with the knowledge to modify your Magento admin section, allowing for greater flexibility in managing category images.

The Limitation of Magento’s Category Image Field

Current Scenario

Magento 2's default setup permits only one image upload for category images, which then applies across all screen sizes. This limitation can be particularly problematic for responsive web design, as images optimized for desktop may not look as good on mobile devices and vice versa.

Desired Solution

To overcome this, we aim to introduce a second image upload field specifically for mobile devices or provide a Page Builder area that allows for direct HTML input. This approach will enable you to customize images or any other content based on the device used, offering a more refined user experience.

Setting Up Your Custom Module

Initial Setup

To begin, ensure you have a custom module set up in Magento. Here’s a quick rundown of how to do this:

  1. Create the Module Directory Structure:

    app/code/YourVendor/YourModule
    
  2. Module XML Configuration: Create the module.xml file in app/code/YourVendor/YourModule/etc:

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="YourVendor_YourModule" setup_version="1.0.0"/>
    </config>
    
  3. Registration File: Create registration.php in YourVendor/YourModule:

    <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'YourVendor_YourModule',
        __DIR__
    );
    

Implementing the Page Builder Element

To add a Page Builder area rather than a simple image upload field, follow these steps:

  1. Modify category_form.xml:

    We need to modify the category_form.xml file within your own module. This file exists in the following path:

    app/code/YourVendor/YourModule/view/adminhtml/ui_component/category_form.xml
    

    Here’s a sample file structure that includes the Page Builder:

    <?xml version="1.0"?>
    <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Ui/etc/ui_configuration.xsd">
        <object name="category_form">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
                    <item name="deps" xsi:type="string">category_form.category_form_data_source</item>
                    <!-- Add other configurations here -->
                </item>
            </argument>
            <dataSource name="category_form_data_source">
                <argument name="dataProvider" xsi:type="configurableObject">
                    <argument name="class" xsi:type="string">Magento\Catalog\Model\Category\DataProvider</argument>
                    <argument name="name" xsi:type="string">category_form_data_source</argument>
                    <argument name="primaryFieldName" xsi:type="string">entity_id</argument>
                    <argument name="requestFieldName" xsi:type="string">id</argument>
                </argument>
            </dataSource>
            <fieldset name="general">
                <field name="Description">
                    <argument name="data" xsi:type="array">
                        <item name="config" xsi:type="array">
                            <item name="label" xsi:type="string" translate="true">Description</item>
                            <item name="formElement" xsi:type="string">wysiwyg</item>
                            <item name="wysiwygConfigData" xsi:type="array">
                                <item name="height" xsi:type="string">300px</item>
                            </item>
                        </item>
                    </argument>
                </field>
            </fieldset>
        </object>
    </form>
    

In this XML configuration, we replaced the simple image upload field with a WYSIWYG (What You See Is What You Get) editor powered by Page Builder. This area can now accept HTML content, including separate image tags for desktop and mobile views.

Removing the Original Image Uploader

Now that we have our Page Builder element, the next step is to remove the original image uploader that's still present. To achieve this:

  1. Open category_form.xml.
  2. Locate the field corresponding to the original image uploader.
  3. Remove or comment out that section.

Here is how you might update your XML:

<field name="image">
    <!-- Remove or comment out this section -->
    <argument name="data">
        <!-- Other configurations for the uploader -->
    </argument>
</field>

By commenting or removing this section, we ensure that the unused image uploader is no longer displayed in the admin panel.

Ensuring Content Saves Correctly

After setting up the Page Builder, the final step is to ensure that any content added via this area saves correctly. Follow these steps:

  1. Database Schema: Ensure the database schema can handle the new data. Modify the etc/db_schema.xml in your module:

    <table name="catalog_category_entity">
        <column name="custom_html" xsi:type="text" nullable="true" comment="Custom HTML for Page Builder"/>
    </table>
    
  2. Data Handling in the Model: Make sure the data handling logic in your model updates correctly. Depending on your module structure, you might need to modify or extend certain Magento models.

  3. Validation and Testing: Test your new configuration thoroughly to ensure that all data entered into the Page Builder area is saved correctly and rendered as expected on both desktop and mobile views.

Conclusion

Enhancing Magento's default capabilities can seem daunting, but with a clear step-by-step process, it becomes manageable. By replacing the standard image upload field with a versatile Page Builder area, you now have the flexibility to add tailored content based on the viewing device. This not only improves the front-end user experience but also provides a superior administrative interface.

Experiment with your Magento customization and take advantage of the vast possibilities offered by combining robust backend management with a dynamic frontend display. This guide aimed to set a foundation; now, it's your turn to build upon it and create a truly responsive e-commerce website.

FAQ

Q: Can I revert to the single image upload field if needed? A: Yes, you can revert by simply commenting out the Page Builder section and uncommenting or re-adding the original image upload field in the category_form.xml.

Q: Is it possible to add more than two image fields? A: Absolutely. You can extend the principle outlined here to add additional image fields or other content areas, depending on your requirements.

Q: Will my Magento update affect these changes? A: Custom modules should be designed to be as update-proof as possible. However, it's always a good idea to thoroughly test any customizations after an update to ensure compatibility.