I am trying to create extra "custom heading field" in Magento 2.3.4 EE backend and trying to use in into frontend category pages to change the page heading from default category name to mentioned value in custom heading field.
Here is a code that is used to create a custom field called "Custom Category Heading" in the SEO section under each category in the backend of Magento.
<?php
namespace Dev123\CustomHeading\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Eav\Setup\EavSetupFactory;
class InstallData implements InstallDataInterface {
private $eavSetupFactory;
/**
* Constructor
*
* @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
*/
public function __construct(
EavSetupFactory $eavSetupFactory
)
{
$this->eavSetupFactory = $eavSetupFactory;
}
public function upgrade(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
// For saving fields of discover collection
if (!$eavSetup->getAttributeId(\Magento\Catalog\Model\Category::ENTITY, 'seo_custom_heading')) {
$eavSetup->addAttribute(
\Magento\Catalog\Model\Category::ENTITY,
'seo_custom_heading',
[
'type' => 'varchar',
'label' => 'Custom Heading',
'input' => 'text',
'sort_order' => 2,
'source' => '',
'global' => 0,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => null,
'group' => 'seo',
'backend' => ''
]
);
}
}
}
This is a field in the backend if we look into the source of it after generating from the above code
<input class="admin__control-text" type="text" data-bind="
event: {change: userChanges},
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr: {
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled,
maxlength: 255
}" name="seo_custom_heading" aria-describedby="notice-YKTXSC6" id="YKTXSC6" maxlength="255">
Here is my code that is acting in changing heading in frontend
<?php
namespace Dev123\Categoryheading\Block\Category;
class View extends \Magento\Catalog\Block\Category\View {
protected function _prepareLayout() {
$category = $this->getCurrentCategory();
if($category) {
$pageMainTitle = $this->getLayout()->getBlock('page.main.title');
if($this->getCurrentCategory()->getSeoCustomHeading()) {
$pageMainTitle->setPageTitle($this->getCurrentCategory()->getSeoCustomHeading());
}
}
return $this;
}
}
?>
This is frontend section of the category where I am trying to add the Custom Heading value from the created filed under SEO
<h1 class="page-title" id="page-title-heading" aria-labelledby="page-title-heading toolbar-amount">
<span data-ui-id="page-title-wrapper">Trick Books</span></h1>
After running module with "protected function _prepareLayout()" function the category page title gone for all category pages as well above H1 code as well. Not sure what I am making a mistake? if I do remove the "protected function _prepareLayout()" this complete code then category page title and h1 back to the template with default category name.