collection = $postCollectionFactory->create(); $this->dataPersistor = $dataPersistor; parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data); $this->meta = $this->prepareMeta($this->meta); } /** * Prepares Meta * * @param array $meta * @return array */ public function prepareMeta(array $meta) { return $meta; } /** * 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) { $post = $post->load($post->getId()); //temporary fix $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; } }