0

I need to override the following file function in my custom module

File - Mageplaza\Blog\Block\Post\View Function Name - getBlogTitle()

As I need to remove '- Blog | Ideas & Advice | Carpet Call' from the meta title

  • When I override using preference method, I am getting following error
Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file: 'post/head.phtml' in module: 'Carpetcall_Blogs'

block's name: 'mpblog.post.head'

Please advice. Following is the new file changes, I made

<?php

namespace Carpetcall\Blogs\Block\Post;

class View extends \Mageplaza\Blog\Block\Post\View
{
    public function getBlogTitle($meta = false)
    {
        $blogTitle = parent::getBlogTitle($meta);

        $post = $this->getBlogObject();
        if (!$post) {
            return $blogTitle;
        }

        if ($meta) {
            if ($post->getMetaTitle()) {
                $blogTitle[] = $post->getMetaTitle();
            } else {
                $blogTitle[] = ucfirst($post->getName());
            }

            return $blogTitle;
        }

        return ucfirst($post->getName());
    }
}
2
  • What you want to remove exactly? Can you please share screen shot?
    – ZealousWeb
    Commented Jun 22, 2021 at 8:19
  • Have you checked added answer?
    – ZealousWeb
    Commented Jun 25, 2021 at 11:24

1 Answer 1

0

You need to setModuleName while overriding file. With the use of below code your error "invalid template file" will be resolved. Please add like below:-

protected function _toHtml()
{
    $this->setModuleName($this->extractModuleName('Mageplaza\Blog\Block\Post\View'));
    return parent::_toHtml();
}

In your post i can assume that you are trying to remove "- Blog" from your Meta title and you want to set just post name. Then let me tell you that if you want to change only for post view page then you can implement it by below code. You don't need to override getBlogTitle() function

Updated whole code:-

<?php

namespace Carpetcall\Blogs\Block\Post;

class View extends \Mageplaza\Blog\Block\Post\View
{
    protected function _toHtml()
    {
        $this->setModuleName($this->extractModuleName('Mageplaza\Blog\Block\Post\View'));
        return parent::_toHtml();
    }
    
    protected function _prepareLayout()
    {
        parent::_prepareLayout();

        $this->pageConfig->getTitle()->set($this->getPost()->getName());
    }
}

Like above code you can remove "- Blog".

Above code is practically tested. Please check and let me know.

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.