Skip to content

Commit c372cf1

Browse files
authored
Merge pull request #322 from magefan/3773-graph-ql-related-posts-products
3773 graph ql related posts products
2 parents 6cc95f9 + a777751 commit c372cf1

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

Model/Post.php

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -955,9 +955,10 @@ public function initDinamicData()
955955

956956
/**
957957
* Prepare all additional data
958+
* @param null|array $fields
958959
* @return array
959960
*/
960-
public function getDynamicData()
961+
public function getDynamicData($fields = null)
961962
{
962963
$data = $this->getData();
963964

@@ -976,28 +977,57 @@ public function getDynamicData()
976977
];
977978

978979
foreach ($keys as $key) {
979-
$method = 'get' . str_replace(
980-
'_',
981-
'',
982-
ucwords($key, '_')
983-
);
984-
$data[$key] = $this->$method();
980+
if (null === $fields || array_key_exists($key, $fields)) {
981+
$method = 'get' . str_replace(
982+
'_',
983+
'',
984+
ucwords($key, '_')
985+
);
986+
$data[$key] = $this->$method();
987+
}
988+
}
989+
990+
if (null === $fields || array_key_exists('tags', $fields)) {
991+
$tags = [];
992+
foreach ($this->getRelatedTags() as $tag) {
993+
$tags[] = $tag->getDynamicData();
994+
}
995+
$data['tags'] = $tags;
985996
}
986997

987-
$tags = [];
988-
foreach ($this->getRelatedTags() as $tag) {
989-
$tags[] = $tag->getDynamicData();
998+
/* Do not use check for null === $fields here
999+
* this checks is used for REST, and related data was not provided via reset */
1000+
if (array_key_exists('related_posts', $fields)) {
1001+
$relatedPosts = [];
1002+
foreach ($this->getRelatedPosts() as $relatedPost) {
1003+
$relatedPosts[] = $relatedPost->getDynamicData(
1004+
$fields['related_posts']
1005+
);
1006+
}
1007+
$data['related_posts'] = $relatedPosts;
9901008
}
991-
$data['tags'] = $tags;
9921009

993-
$categories = [];
994-
foreach ($this->getParentCategories() as $category) {
995-
$categories[] = $category->getDynamicData();
1010+
/* Do not use check for null === $fields here */
1011+
if (array_key_exists('related_products', $fields)) {
1012+
$relatedProducts = [];
1013+
foreach ($this->getRelatedProducts() as $relatedProduct) {
1014+
$relatedProducts[] = $relatedProduct->getSku();
1015+
}
1016+
$data['related_products'] = $relatedProducts;
9961017
}
997-
$data['categories'] = $categories;
9981018

999-
if ($author = $this->getAuthor()) {
1000-
$data['author'] = $author->getDynamicData();
1019+
if (null === $fields || array_key_exists('categories', $fields)) {
1020+
$categories = [];
1021+
foreach ($this->getParentCategories() as $category) {
1022+
$categories[] = $category->getDynamicData();
1023+
}
1024+
$data['categories'] = $categories;
1025+
}
1026+
1027+
if (null === $fields || array_key_exists('author', $fields)) {
1028+
if ($author = $this->getAuthor()) {
1029+
$data['author'] = $author->getDynamicData();
1030+
}
10011031
}
10021032

10031033
return $data;

0 commit comments

Comments
 (0)