1

I wanted to duplicate the "page.main.title" element on the product pages, so I added this block to my catalog_product_view.ml

<block class="Magento\Theme\Block\Html\Title" name="title.additional2" template="Magento_Theme::html/title.phtml">
   <arguments>
      <argument name="css_class" xsi:type="string">product</argument>
      <argument name="add_base_attribute" xsi:type="string">itemprop="name"</argument>
   </arguments>
</block>

I want to add this block because I moved "page.main.title" elsewhere on the page.

<move element="page.main.title" destination="page.top" before="breadcrumbs" />

"page.main.title" displays the product name, but my new block (title.additional2) displays the content of the meta title and I don't understand why?

Thanks in advance if anyone can explain this result to me. Good day

1 Answer 1

0

This is happening because of your phtml calling this class Magento\Theme\Block\Html\Title.php in which phtml using the getPageHeading function, This function gives title of page only on the first time rendering and on second time it's give page title as empty which result to show the page meta title.

For Override this we need to be follow the below steps :

Create/Update the catalog_product_view.xml in your module or your theme. Add the lines in the file {vendor_module}/view/frontend/layout/catalog_product_view.xml

<block class="Magento\Theme\Block\Html\Title" name="title.additional2" template="Vendor_Module::html/title2.phtml"/>

Create your custom block in your Module {vendor_module}/Block/Productdata.php

<?php
namespace vendor\Module\Block;

class Productdata extends \Magento\Framework\View\Element\Template
{
    public $registry;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry,
        array $data = []
    ) {
        $this->registry = $registry;
        parent::__construct($context, $data);
    }

    public function getCurrentProduct()
    {
        return $this->registry->registry('current_product');
    }
}

Create a phtml file in {vendor_module}/view/frontend/templates/html/title2.phtml and add the below code in it

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * @var $block \Magento\Theme\Block\Html\Title
 */
$product= $block->getLayout()->createBlock('Vendor\Module\Block\Productdata');



$cssClass = $block->getCssClass() ? ' ' . $block->getCssClass() : '';
$titleHtml = '';
if (trim($block->getPageHeading())) {
    $titleHtml = '<span class="base" data-ui-id="page-title-wrapper" '
        . $block->getAddBaseAttribute()
        . '>'
        . $product->getCurrentProduct()->getName()
        . '</span>';
}
?>
<?php if ($titleHtml) : ?>
<div class="page-title-wrapper testing<?= $block->escapeHtmlAttr($cssClass) ?>">
    <h1 class="page-title"
        <?php if ($block->getId()) : ?> id="<?= $block->escapeHtmlAttr($block->getId()) ?>" <?php endif; ?>
        <?php if ($block->getAddBaseAttributeAria()) : ?>
            aria-labelledby="<?= $block->escapeHtmlAttr($block->getAddBaseAttributeAria()) ?>"
        <?php endif; ?>>
        <?= /* @noEscape */ $titleHtml ?>
    </h1>
    <?= $block->getChildHtml() ?>
</div>
<?php endif; ?>
2
  • Thank you but I don't understand your answer, it doesn't answer my question?
    – ljr95
    Nov 7, 2023 at 8:31
  • Updated the answer. Please review and let me know if issue still exists. Nov 8, 2023 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.