Skip to content

Commit a777751

Browse files
authored
3773 graph ql related posts products
1 parent 7c81f84 commit a777751

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

Model/Post.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ public function initDinamicData()
955955

956956
/**
957957
* Prepare all additional data
958-
* @param null $fields
958+
* @param null|array $fields
959959
* @return array
960960
*/
961961
public function getDynamicData($fields = null)
@@ -977,30 +977,37 @@ public function getDynamicData($fields = null)
977977
];
978978

979979
foreach ($keys as $key) {
980-
$method = 'get' . str_replace(
981-
'_',
982-
'',
983-
ucwords($key, '_')
984-
);
985-
$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+
}
986988
}
987989

988-
if (array_key_exists('tags', $fields)) {
990+
if (null === $fields || array_key_exists('tags', $fields)) {
989991
$tags = [];
990992
foreach ($this->getRelatedTags() as $tag) {
991993
$tags[] = $tag->getDynamicData();
992994
}
993995
$data['tags'] = $tags;
994996
}
995997

998+
/* Do not use check for null === $fields here
999+
* this checks is used for REST, and related data was not provided via reset */
9961000
if (array_key_exists('related_posts', $fields)) {
9971001
$relatedPosts = [];
9981002
foreach ($this->getRelatedPosts() as $relatedPost) {
999-
$relatedPosts[] = $relatedPost->getDynamicData($fields);
1003+
$relatedPosts[] = $relatedPost->getDynamicData(
1004+
$fields['related_posts']
1005+
);
10001006
}
10011007
$data['related_posts'] = $relatedPosts;
10021008
}
10031009

1010+
/* Do not use check for null === $fields here */
10041011
if (array_key_exists('related_products', $fields)) {
10051012
$relatedProducts = [];
10061013
foreach ($this->getRelatedProducts() as $relatedProduct) {
@@ -1009,16 +1016,18 @@ public function getDynamicData($fields = null)
10091016
$data['related_products'] = $relatedProducts;
10101017
}
10111018

1012-
if (array_key_exists('categories', $fields)) {
1019+
if (null === $fields || array_key_exists('categories', $fields)) {
10131020
$categories = [];
10141021
foreach ($this->getParentCategories() as $category) {
10151022
$categories[] = $category->getDynamicData();
10161023
}
10171024
$data['categories'] = $categories;
10181025
}
10191026

1020-
if (array_key_exists('author', $fields) && $author = $this->getAuthor()) {
1021-
$data['author'] = $author->getDynamicData();
1027+
if (null === $fields || array_key_exists('author', $fields)) {
1028+
if ($author = $this->getAuthor()) {
1029+
$data['author'] = $author->getDynamicData();
1030+
}
10221031
}
10231032

10241033
return $data;

0 commit comments

Comments
 (0)