Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 43 additions & 29 deletions Block/Category/Listpost.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
* @license https://www.mageplaza.com/LICENSE.txt
*/

declare(strict_types=1);

namespace Mageplaza\Blog\Block\Category;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Phrase;
use Magento\Framework\View\Element\BlockInterface;
use Mageplaza\Blog\Helper\Data;
use Mageplaza\Blog\Model\Category;
use Mageplaza\Blog\Model\ResourceModel\Post\Collection;

/**
Expand All @@ -32,10 +37,7 @@
*/
class Listpost extends \Mageplaza\Blog\Block\Listpost
{
/**
* @var string
*/
protected $_category;
protected ?Category $category = null;

/**
* Override this function to apply collection for each type
Expand All @@ -52,52 +54,64 @@ protected function getCollection()
return null;
}

/**
* @return mixed
*/
protected function getBlogObject()
protected function getBlogObject(): Category|null
{
if (!$this->_category) {
$id = $this->getRequest()->getParam('id');
if ($id) {
$category = $this->helperData->getObjectByParam($id, null, Data::TYPE_CATEGORY);
if ($category && $category->getId()) {
$this->_category = $category;
}
}
if ($this->category instanceof Category === true) {
return $this->category;
}

$id = $this->getRequest()->getParam('id');
if (empty($id) === true) {
return null;
}

$category = $this->helperData->getObjectByParam($id, null, Data::TYPE_CATEGORY);
if (isset($category) === true
&& $category instanceof Category === true
&& $category->getId() !== null
) {
$this->category = $category;
}

return $this->_category;
return $this->category ?? null;
}

/**
* @inheritdoc
* @throws LocalizedException
*/
protected function _prepareLayout()
protected function _prepareLayout(): void
{
parent::_prepareLayout();
$breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
if ($breadcrumbs instanceof BlockInterface === false && is_bool($breadcrumbs) === false) {
return;
}

if ($breadcrumbs = $this->getLayout()->getBlock('breadcrumbs')) {
$category = $this->getBlogObject();
$categoryName = preg_replace('/[^A-Za-z0-9\-]/', ' ', $category->getName());
if ($category) {
$breadcrumbs->addCrumb($category->getUrlKey(), [
'label' => __($categoryName),
'title' => __($categoryName)
]);
}
$category = $this->getBlogObject();
if ($category instanceof Category === false) {
return;
}

$categoryName = preg_replace('/[^A-Za-z0-9\-]/', ' ', $category->getName());

$breadcrumbs->addCrumb(
$category->getUrlKey(), [
'label' => __($categoryName),
'title' => __($categoryName),
]);
}

/**
* @param bool $meta
*
* @return array|Phrase|string
*/
public function getBlogTitle($meta = false)
public function getBlogTitle(
$meta = false)
{
$blogTitle = parent::getBlogTitle($meta);
$category = $this->getBlogObject();
$category = $this->getBlogObject();
if (!$category) {
return $blogTitle;
}
Expand Down