tm2000
March 8, 2021, 4:04pm
1
So I have two sites running in seperate installations of Modx. (They were supposed to be standalone so didn’t create them as contexts).
I now need to pull in a tv(image), pagetitle and URL from the latest resource from onesite, onto the home page of the other.
Is this at all possible?
Or would I have to move the sites ito a single install of Modx with contexts for each?
Thanks in advance!
Andy
you could create a REST Enpoint. In the simplest case, this could just be a resource with a snippet, which delivers the needed data.
tm2000
March 9, 2021, 10:56am
3
Thanks for the suggestion - I will need to read up on REST Endpoints as I’ve never used them.
in your case, really just needs to be a very simple snippet, which gets the latest resource - object and returns the needed fields as json.
Put this snippet into the content of a new resource with a blank template and maybe contenttype JSON
on the other site call the url of that resource with for example file_get_contents, decode that json and pass it to getChunk
tm2000
March 9, 2021, 10:47pm
6
Thanks - I will have a read and see if I can get my head round it!
tm2000
March 9, 2021, 11:02pm
7
Thanks again for the advice. I will have to see if I can find more info on this somewhere. I don’t understand this enough. I’ve read the ‘Loading the modx object’ article (thanks) but I don’t understand it enough to know where I go from there. I’m a visual designer and build out modx sites but don’t really stray from the built in templating etc.
As I said, you don’t need that. You just need a snippet, which outputs a JSON with the needed data. That’s all
tm2000
March 10, 2021, 7:35am
9
Ah right ok. Sorry, I thought that was all part of one solution using the api etc.
Ok, this sounds more manageable. Will have a play and see if I can get this working. Thanks.
tm2000
March 10, 2021, 7:40am
10
Presumably I could just use getResources then?
you could use pdoResources with &return=json
[[!pdoResources? &return=`json` &parents=`62`]]
tm2000
March 10, 2021, 12:07pm
12
Thanks Bruno.
So - I have it partially working with help from you and searching the forums.
In Site A, I have a PdoResources on a resource with no template like so:
[[!pdoResources?
&parents=3
&limit=1
&includeTVs=PDF
&tpl=latestLissueJSONTpl
]]
Then on Site B, I have a snippet on a page which ulls in the data that I need - but I’m not sure how to pass this to getChunk in order to format it.
<?php
$string=file_get_contents('https://www.pathToJsonResource.json');
$json_a=json_decode($string,true);
// associative array mode
echo $json_a[IssueNumber];
echo $json_a[image];
So it echos out the Issue Number and image URl which is great - but not sure how to pass this to a chunk so that I can format it.
I’m almost there!
Thanks for your help so far!
Andy
tm2000
March 10, 2021, 12:15pm
13
Although I just read you shouldn’t use echo in a snippet?
<?php
$output = '';
foreach ($json_a as $image) {
$output .= $modx->getChunk('latestLissueJSONTpl',array(
'image' => $image,
));
}
echo $oputput;
?>
*Untested
** Make sure you make the chunk on site B
tm2000
March 10, 2021, 12:20pm
15
Wow - thanks!! I’m trying it right now.
tm2000
March 10, 2021, 12:32pm
16
I assume I’m merging these togehre into one snippet, but not sure what to keep of mine?
<?php
$string=file_get_contents('https://www.pathToJsonResource.json');
$properties=json_decode($string,true);
if (is_array($properties)){
return $modx->getChunk('yourChunk',$properties);
}
return 'something went wrong';
1 Like
I think, you don’t need a tpl on your pdoResources - call, when using
&return=`json`
do you?
1 Like
tm2000
March 10, 2021, 1:54pm
19
I just tried without a tpl and it outputs a lot of data rather than just the two fields I require) - is that a performance hit?
I tried your snippet and it works wih the TPL - but not when i use &return: json - Th JSON is formatted differently.
tm2000
March 10, 2021, 1:56pm
20
This works great thank you.
But for some reason it’s trying to pull in two resources - the HTML output is:
<p>This is issue: 487</p>
<img src="https://www.domain.co.uk/image.jpg"/>
<p>This is issue: </p>
<img src=""/>
Not sure where the second instance is coming from?