I want to add a custom field (textbox) on the cms page admin side. Can anyone please guide me?
Like this: https://prnt.sc/1qfe0cf
I want to add a custom field (textbox) on the cms page admin side. Can anyone please guide me?
Like this: https://prnt.sc/1qfe0cf
If you have have already installed module then use UpgradeSchema.php to add new field in cms_page table.
Otherwise please use InstallSchema.php file for new module.
Step 1:- Please create file UpgradeSchema.php under path Vendor/Module/Setup/
<?php
namespace Vendor\Module\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
if(version_compare($context->getVersion(), '2.0.0', '<')) {
$this->addTextField($setup);
}
}
protected function addTextField(SchemaSetupInterface $setup)
{
$connection = $setup->getConnection();
$connection->addColumn(
$setup->getTable('cms_page'),
'sub_title',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Sub Title',
]
);
return $this;
}
}
Step 2:- Change module version in file module.xml under path Vendor/Module/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="Vendor_Module" setup_version="2.0.0"></module>
</config>
Step 3:- Please create file cms_page_form.xml under path Vendor/Module/view/adminhtml/ui_component/
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="general">
<field name="sub_title" sortOrder="30" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">page</item>
</item>
</argument>
<settings>
<validation>
<rule name="required-entry" xsi:type="boolean">true</rule>
</validation>
<dataType>text</dataType>
<label translate="true">Sub Title</label>
<dataScope>sub_title</dataScope>
</settings>
</field>
</fieldset>
</form>
You will see Sub title field in admin with the use of above code. Also if you write anything in this new field. It will save in database as well. You don't need to do anything to save text field value in "cms_page" tabel.
Cheers!
<?php
namespace Vendor\BlockCms\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Cms\Model\BlockFactory;
use Psr\Log\LoggerInterface;
class SaveCustomField implements ObserverInterface
{
protected $request;
/**
* @var BlockFactory
*/
protected $blockFactory;
/**
* @var LoggerInterface
*/
protected $logger;
public function __construct(
RequestInterface $request,
BlockFactory $blockFactory,
LoggerInterface $logger
) {
$this->request = $request;
$this->blockFactory = $blockFactory;
$this->logger = $logger;
}
public function execute(Observer $observer)
{
try {
// Get the CMS block object from the event
$cmsBlock = $observer->getEvent()->getObject();
$blockId = $cmsBlock->getId();
$loadedBlock = $this->blockFactory->create()->load($blockId);
// Check if the custom field data is present
if ($cmsBlock->enable_disable) {
$customFieldValue = $cmsBlock->enable_disable;
// Set the custom field value and save it
$loadedBlock->setData('enable_disable', $customFieldValue);
$loadedBlock->save();
// Log success
$this->logger->info("Custom field value saved successfully for CMS Block ID: {$blockId}");
}
} catch (\Exception $e) {
// Log any exceptions
$this->logger->error('Error saving custom field value: ' . $e->getMessage());
}
}
}