/** * Prepare posts collection * * @return void */ protected function _preparePostCollection() { $this->_postCollection = $this->_postCollectionFactory->create()->addActiveFilter()->addStoreFilter($this->_storeManager->getStore()->getId())->setOrder('publish_time', 'DESC'); if ($this->getPageSize()) { $this->_postCollection->setPageSize($this->getPageSize()); } }
/** * Get data * * @return array */ public function getData() { if (isset($this->loadedData)) { return $this->loadedData; } $items = $this->collection->getItems(); /** @var $post \Magefan\Blog\Model\Post */ foreach ($items as $post) { $data = $post->getData(); /* Prepare Featured Image */ $map = ['featured_img' => 'getFeaturedImage', 'og_img' => 'getOgImage']; foreach ($map as $key => $method) { if (isset($data[$key])) { $name = $data[$key]; unset($data[$key]); $data[$key][0] = ['name' => $name, 'url' => $post->{$method}()]; } } $data['data'] = ['links' => []]; /* Prepare related posts */ $collection = $post->getRelatedPosts(); $items = []; foreach ($collection as $item) { $items[] = ['id' => $item->getId(), 'title' => $item->getTitle()]; } $data['data']['links']['post'] = $items; /* Prepare related products */ $collection = $post->getRelatedProducts()->addAttributeToSelect('name'); $items = []; foreach ($collection as $item) { $items[] = ['id' => $item->getId(), 'name' => $item->getName()]; } $data['data']['links']['product'] = $items; /* Set data */ $this->loadedData[$post->getId()] = $data; } return $this->loadedData; }
/** * Retrieve post related posts * @return \Magefan\Blog\Model\ResourceModel\Post\Collection */ public function getRelatedPosts() { if (!$this->hasData('related_posts')) { $collection = $this->_relatedPostsCollection->addFieldToFilter('post_id', ['neq' => $this->getId()])->addStoreFilter($this->getStoreId()); $collection->getSelect()->joinLeft(['rl' => $this->getResource()->getTable('magefan_blog_post_relatedpost')], 'main_table.post_id = rl.related_id', ['position'])->where('rl.post_id = ?', $this->getId()); $this->setData('related_posts', $collection); } return $this->getData('related_posts'); }