@@ -955,9 +955,10 @@ public function initDinamicData()
955
955
956
956
/**
957
957
* Prepare all additional data
958
+ * @param null|array $fields
958
959
* @return array
959
960
*/
960
- public function getDynamicData ()
961
+ public function getDynamicData ($ fields = null )
961
962
{
962
963
$ data = $ this ->getData ();
963
964
@@ -976,28 +977,57 @@ public function getDynamicData()
976
977
];
977
978
978
979
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 ;
985
996
}
986
997
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 ;
990
1008
}
991
- $ data ['tags ' ] = $ tags ;
992
1009
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 ;
996
1017
}
997
- $ data ['categories ' ] = $ categories ;
998
1018
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
+ }
1001
1031
}
1002
1032
1003
1033
return $ data ;
0 commit comments