cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 Block returns value of 1

Magento 2 Block returns value of 1

I have inherited a project that has the following code:
       return $this->getLayout()
            ->createBlock(CategoryBlock::class)
            ->setData([
                'title' => $title ? sprintf($title, $category->getName()) : null,
               'category_id' => $category->getId(),
                'description' => $description ? sprintf($description, $category->getName()) : null,
            ])->toHtml();
The description is working by using $block->getDescription() as I would expect. The $block->getTitle() is not returning the value expected, instead it is return the number 1. Can anyone spot the problem as I have tried various things but it is not working. The project has to use 2.2.7 so need to debug in this version.
6x certified Magento Specialist.
Nigel Boulton, Magento Solution Specialist
2 REPLIES 2

Re: Magento 2 Block returns value of 1

Hello @pinkdigital 
 
Can you use directly class name instead of 
CategoryBlock::class
e.g
Vendor\Module\Block\Name
Thanks
Binod
If my answer is useful, please Accept as Solution & give Kudos

Re: Magento 2 Block returns value of 1

Thanks for your help. It didn't resolve the problem so I had a deeper look in to the code.

 

The problem was caused by the previous developer returning $this->hasData('title') instead of $this->getData('title'); hence the value of 1 (true).

 

 

 

 

6x certified Magento Specialist.
Nigel Boulton, Magento Solution Specialist