Adding a Custom Form to Magento Admin Panel
Hello developers! In this post, we’ll guide you through the process of creating a simple custom form that can be managed within the Magento admin panel. We’ll use the example provided by the reader as inspiration for our case study.
Prerequisites
Before we begin, make sure you have:
- A working knowledge of PHP and OOP concepts.
- Familiarity with Magento architecture.
- Access to a local or remote Magento installation.
Understanding the Requirement
Our client wants to add a color-changing feature similar to this demo. They want to manage the images and color codes from the Magento admin panel. Since the reader has limited experience in Magento extension development, we’ll provide a step-by-step guide.
Step 1: Create the Custom Module
Firstly, create a new folder for your module under app/code/Vendor/ColorChanger. Make sure to replace ‘Vendor’ with your actual vendor name.
mkdir -p app/code/Vendor/ColorChanger/{etc,registration.php,Setup/InstallData.php}
Step 2: Define the Module
Open the registration.php file and add the following content:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_ColorChanger',
__DIR__
);
This registers your module within Magento.
Step 3: Create the Database Table
We need a table to store our color data. Create a Setup/InstallData.php file with the following content:
<?php
namespace Vendor\ColorChanger\Setup;
use Magento\Framework\SetUp\InstallSchemaInterface;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\App\ResourceConnection;
class InstallData implements InstallSchemaInterface
{
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$table = $setup->getConnection()->newTable($setup->getTable('vendor_colorchanger'))
->addColumn(
'entity_id',
Table::TYPE_INTEGER,
null,
[
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true
],
'Entity ID'
)
->addColumn(
'color_code',
Table::TYPE_VARCHAR,
255,
['nullable => false'],
'Color Code'
)
->addColumn(
'image_path',
Table::TYPE_TEXT,
null,
['nullable' => false],
'Image Path'
)
->setComment('Vendor Color Changer Table');
$setup->getConnection()->createTable($table);
$setup->endSetup();
}
}
This creates a simple table with an entity ID, color code, and image path.
Step 4: Create the Form
Now let’s create the actual form. To do this, we’ll use the Magento UI Component framework:
- Navigate to
app/code/Vendor/ColorChanger/view/adminhtml/ui_component/colorchanger_form.xmland add the following content:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<xsi:schemaLocation url="urn:magento:ui.schema:etc/ui_configuration.xsd">
<area code="vendor_colorchanger">
<form id="colorchanger_form" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/form.xsd">
<settings>
<namespace>admin</namespace>
<formElement>form_element</formElement>
<template>templates/admin/form/collapsible</template>
<storageConfig>
<indexField>entity_id</indexField>
<saveUrl path="colorchanger/save"/>
<deleteUrl path="colorchanger/delete"/>
<createUrl path="colorchanger/new"/>
</storageConfig>
</settings>
<resource name="vendor_colorchanger">
<collection name="vendor_colorchanger_collection" primaryField="entity_id" secondaryKey="image_path" entityType="vendor_colorchanger">
<filterGroup name="colorchanger_filter" conditionType="all">
<field name="image_path"/>
<field name="color_code"/>
</filterGroup>
</collection>
</resource>
<dataSource name="colorchanger_data_source" component="Magento_Ui/js/form/provider" class="Vendor\ColorChanger\Model\ResourceModel\ColorChanger\Collection">
<settings>
<searchUrl path="colorchanger/index"/>
<saveUrl path="colorchanger/save"/>
<validateUrl path="colorchanger/validate"/>
</settings>
</dataSource>
</form>
</area>
</xsi:schemaLocation>
</config>
- Next, create the form fields in
app/code/Vendor/ColorChanger/view/adminhtml/ui_component/colorchanger_form.xml:
<formElement name="vendor_colorchanger" code="Vendor\ColorChanger">
<field name="entity_id">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">ID</item>
<item name="visible" xsi:type="boolean">false</item>
<item name="dataType" xsi:type="string">text</item>
<item name="formElement" xsi:type="string">input</item>
</item>
</argument>
</field>
<field name="color_code">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Color Code</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="dataType" xsi:type="string">text</item>
<item name="formElement" xsi:type="string">input</item>
</item>
</argument>
</field>
<field name="image_path">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Image Path</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="dataType" xsi:type="string">text</item>
<item name="formElement" xsi:type="string">input</item>
</item>
</argument>
</field>
</formElement>
Step 5: Create the Controller
Finally, we need to create a controller to handle form submission and data management. You can follow the official Magento guide for this step.
Conclusion
In this post, we’ve shown you how to create a simple custom form that can be managed within the Magento admin panel. We’ve provided a thorough walkthrough of the process, including creating the database table, defining the form layout, and setting up the necessary files.
Remember, this is just a starting point. You’ll need to add additional functionality like image uploads, validation, and integration with your front-end logic.
If you’re looking for more advanced techniques or want to learn from experienced Magento developers, I highly recommend checking out Laravel Company. They offer comprehensive resources on Magento development, including detailed tutorials and professional services.
Happy coding!