getCollection(); if ($collection && $collection->getSize()) { $pager = $this->getLayout()->createBlock(Pager::class, 'mpblog.post.pager'); $perPageValues = (string) $this->helperData ->getDisplayConfig('pagination', $this->store->getStore()->getId()); $perPageValues = explode(',', $perPageValues ?? ''); $perPageValues = array_combine($perPageValues, $perPageValues); $pager->setAvailableLimit($perPageValues) ->setCollection($collection); $this->setChild('pager', $pager); } return $collection; } /** * find /n in text * * @param $description * * @return string */ public function maxShortDescription($description) { if (is_string($description)) { $html = ''; foreach (explode("\n", trim($description)) as $value) { $html .= '

' . $value . '

'; } return $html; } return $description; } /** * @return Collection */ protected function getCollection() { try { return $this->helperData->getPostCollection(null, null, $this->store->getStore()->getId()); } catch (Exception $exception) { $this->_logger->error($exception->getMessage()); } return null; } /** * @return string */ public function getPagerHtml() { return $this->getChildHtml('pager'); } /** * @return bool */ public function isGridView() { return $this->helperData->getPostViewPageConfig('display_style') == DisplayType::GRID; } /** * @return Listpost * @throws LocalizedException * @throws \Magento\Framework\Exception\NoSuchEntityException */ protected function _prepareLayout() { if ($breadcrumbs = $this->getLayout()->getBlock('breadcrumbs')) { $breadcrumbs->addCrumb('home', [ 'label' => __('Home'), 'title' => __('Go to Home Page'), 'link' => $this->_storeManager->getStore()->getBaseUrl() ]) ->addCrumb($this->helperData->getRoute(), $this->getBreadcrumbsData()); } $this->applySeoCode(); return parent::_prepareLayout(); } /** * @return array */ protected function getBreadcrumbsData() { $label = $this->helperData->getBlogName(); $data = [ 'label' => $label, 'title' => $label ]; if ($this->getRequest()->getFullActionName() !== 'mpblog_post_index') { $data['link'] = $this->helperData->getBlogUrl(); } return $data; } /** * @return $this * @throws LocalizedException */ public function applySeoCode() { $this->pageConfig->getTitle()->set(join($this->getTitleSeparator(), array_reverse($this->getBlogTitle(true)))); $object = $this->getBlogObject(); $storeId = $this->store->getStore()->getId(); $description = $object ? $this->getMetaFieldByStoreId($object->getMetaDescription(), $storeId) : null; $this->pageConfig->setDescription($description ?: $this->helperData->getBlogConfig('seo/meta_description', $storeId)); $keywords = $object ? $this->getMetaFieldByStoreId($object->getMetaKeywords(), $storeId) : null; $this->pageConfig->setKeywords($keywords ?: $this->helperData->getBlogConfig('seo/meta_keywords', $storeId)); $robots = $object ? $this->getMetaRobotsByStoreId($object->getMetaRobots(), $storeId) : null; $this->pageConfig->setRobots($robots ?: $this->helperData->getBlogConfig('seo/meta_robots', $storeId)); $url = $object ? $object->getUrl() : $this->helperData->getBlogConfig('seo/url_key', $storeId); if ($this->getRequest()->getFullActionName() === 'mpblog_post_view' && $url) { $this->pageConfig->addRemotePageAsset( $url, 'canonical', ['attributes' => ['rel' => 'canonical']] ); } $pageMainTitle = $this->getLayout()->getBlock('page.main.title'); if ($pageMainTitle) { $pageMainTitle->setPageTitle($this->getBlogTitle()); } return $this; } /** * Retrieve HTML title value separator (with space) * * @return string */ public function getTitleSeparator() { $separator = (string) $this->helperData->getConfigValue('catalog/seo/title_separator'); return ' ' . $separator . ' '; } /** * @param bool $meta * * @return array|Phrase */ public function getBlogTitle($meta = false) { $pageTitle = $this->helperData->getDisplayConfig('name') ?: __('Blog'); if ($meta) { $title = $this->helperData->getBlogConfig('seo/meta_title') ?: $pageTitle; return [$title]; } return $pageTitle; } /** * @param string|null $jsonValue * @param int $storeId * * @return string|null */ protected function getMetaFieldByStoreId(?string $jsonValue, int $storeId): ?string { if (empty($jsonValue)) { return null; } $decoded = json_decode($jsonValue, true); if (json_last_error() !== JSON_ERROR_NONE || !is_array($decoded)) { $decoded = ['0' => $jsonValue]; } return $decoded[$storeId] ?? $decoded[0] ?? null; } /** * @param $robots * @param $storeId * * @return mixed */ protected function getMetaRobotsByStoreId($robots, $storeId) { if (empty($robots)) { return null; } $robots = json_decode($robots, true); if (json_last_error() !== JSON_ERROR_NONE || !is_array($robots)) { $robots = ['0' => $robots]; } return $robots[$storeId] ?? ($robots[0] ?? null); } }