5 replies to this topic
#1
Posted 31 July 2012 - 03:00 AM
Hello Everyone,
I'm working on a module that pulls a JSON list of videos of a server and creates a page for each video. These videos come with several tags and I would like to include them. My plan is as follows:
- Create a page for the video with the neccessary data
- Seperate the tags for each video
- Check for each tag if a page with that title exists under /tags/
- if not, create it
- add that page to the "tags" field of the video page.
It's the last part that I can't figure out. Could you explain to me how to add a page to a field in an existing page via the API?
Thanks,
thomas
I'm working on a module that pulls a JSON list of videos of a server and creates a page for each video. These videos come with several tags and I would like to include them. My plan is as follows:
- Create a page for the video with the neccessary data
- Seperate the tags for each video
- Check for each tag if a page with that title exists under /tags/
- if not, create it
- add that page to the "tags" field of the video page.
It's the last part that I can't figure out. Could you explain to me how to add a page to a field in an existing page via the API?
Thanks,
thomas
#2
Posted 31 July 2012 - 03:55 AM
you just have to assign the page object to the field.
Voilá! Didn't test this, but should work
Edit: corrected the code according to what was said in the next two posts
//assuming that the video page was created before this
foreach ($tags as $tag){
// get the corresponding tag page
$tagPage = $pages->get("template=tags, title=$tag");
// if the page doesn't exist, create it
if(!$tagPage->id){
$tagPage = new Page();
$tagPage->template = $templates->get("tag");
$tagPage->parent = $pages->get("/tags/");
$tagPage->title = $tag;
$tagPage->save();
}
// assign the tag page to the video page
$videoPage->of(false);
$videoPage->tags = $tagPage;
$videoPage->save();
}Voilá! Didn't test this, but should work
Edit: corrected the code according to what was said in the next two posts
#5
Posted 31 July 2012 - 12:32 PM
if($tagPage instanceof NullPage) { ... }You can also do this to accomplish the same thing (at least, I prefer the syntax):
if(!$tagPage->id) { ... }Also wanted to mention that the call to $videoPage->of(true); should instead be $videoPage->of(false). That turns off output formatting for the page, putting it in a save state (vs. presentation state).
Also tagged with one or more of these keywords: api, pages
Community Support →
General Support →
Check if a given page has childrenStarted by benjaminE, 10 May 2013 |
|
|
||
Community Support →
General Support →
import / export of pages from one PW to anotherStarted by benbyf, 01 May 2013 |
|
|
||
Community Support →
API & Templates →
Using while loop with PW APIStarted by kongondo, 25 Apr 2013 |
|
|
||
Community Support →
General Support →
Select field values not getting edited in front-end formStarted by onjegolders, 23 Apr 2013 |
|
|
||
Community Support →
API & Templates →
$pages->get("/features")->find("field1<field2") possible?Started by jmartsch, 22 Apr 2013 |
|
|
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users













