Jump to content


Photo

Create Pages & add to Fieldtype "Pages" via API

api pages

  • Please log in to reply
5 replies to this topic

#1 thomas

thomas

    Distinguished Member

  • Members
  • PipPipPipPip
  • 115 posts
  • 33

  • LocationKöln, Germany

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

#2 diogo

diogo

    Hero Member

  • Moderators
  • 1,995 posts
  • 1072

  • LocationPorto, Portugal

Posted 31 July 2012 - 03:55 AM

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

#3 thomas

thomas

    Distinguished Member

  • Members
  • PipPipPipPip
  • 115 posts
  • 33

  • LocationKöln, Germany

Posted 31 July 2012 - 04:31 AM

Hi Diogo,
seems easy enough! I couldn't quite get it work yet but I'm positive I'll figure it out sooner or later.
Thanks,
thomas

#4 thomas

thomas

    Distinguished Member

  • Members
  • PipPipPipPip
  • 115 posts
  • 33

  • LocationKöln, Germany

Posted 31 July 2012 - 05:14 AM

Hi Diogo,
one change was neccessary:

if($tagPage instanceof NullPage) { ... }


Other than that, spot on!

Thanks!
-t

#5 ryan

ryan

    Hero Member

  • Administrators
  • 5,771 posts
  • 3114

  • LocationAtlanta, GA

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

#6 diogo

diogo

    Hero Member

  • Moderators
  • 1,995 posts
  • 1072

  • LocationPorto, Portugal

Posted 01 August 2012 - 04:20 AM

true, sorry for those things. i will correct them on my code for reference





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users