Jump to content

Create Pages & add to Fieldtype "Pages" via API


thomas
 Share

Recommended Posts

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

Link to comment
Share on other sites

you just have to assign the page object to the field.

//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

  • Like 2
Link to comment
Share on other sites

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).

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...