Jump to content


Photo

Minor API update for when saving a new page


  • Please log in to reply
5 replies to this topic

#1 ryan

ryan

    Hero Member

  • Administrators
  • 5,985 posts
  • 3386

  • LocationAtlanta, GA

Posted 03 November 2011 - 09:24 AM

This is relatively minor, but I realized a lot of us were repeatedly using the same code to populate a required 'name' field when creating new pages via the API. As a result, I figured I'd build it in. Here's the commit note:

Update $pages->save() so that it auto-generates a $page->name field when none is provided in the new page. It generates it from the 'title' field. This makes for more convenient API usage, especially when importing lots of pages. It also accounts for duplicate name fields and adds an incrementing number to the end when it finds duplicates.


Previously if you tried to save a page without a 'name' field it would throw an exception. Now it won't, as long as the page has a title. This should not affect any existing code. But it does mean that future page imports may be a little simpler since you don't have to consider the 'name' field.

#2 Sevarf2

Sevarf2

    Sr. Member

  • Members
  • PipPipPipPip
  • 301 posts
  • 13

  • LocationBratislava

Posted 07 November 2011 - 12:08 PM

Ryan, i tried with latest commit but i get a page name "0". No exception but name of the page is not the title...
simple code:
<?php
$s = new Page();
$s->template 	= $templates->get("submit");
$s->parent	= $pages->get($userid);
$s->title     = "Submissions";
$s->save();

Maybe because my parent page is an user?  ???

#3 adamkiss

adamkiss

    Master of the universe

  • Moderators
  • 1,088 posts
  • 292

Posted 07 November 2011 - 12:41 PM

Sorry for hijacking this post a little, but you can add page to user record???

#4 Sevarf2

Sevarf2

    Sr. Member

  • Members
  • PipPipPipPip
  • 301 posts
  • 13

  • LocationBratislava

Posted 07 November 2011 - 02:29 PM

@adamkiss: sure...

#5 ryan

ryan

    Hero Member

  • Administrators
  • 5,985 posts
  • 3386

  • LocationAtlanta, GA

Posted 08 November 2011 - 09:13 AM

Sevarf2, I can't seem to duplicate here. I started from a new install and pasted this into a template:

<?php
$p = new Page();
$p->template = $templates->get('basic-page');
$p->parent = $pages->get($user->id);
$p->title = 'Submissions';
$p->save();

Then ended up with this page:

/processwire/access/users/admin/submissions/

Can you think of any other factors or other modules that might be running? I'm also curious about the page name "0". I just committed an update that may fix this in your case–can you grab the latest commit and let me know if that fixes it?

Thanks,
Ryan


#6 Sevarf2

Sevarf2

    Sr. Member

  • Members
  • PipPipPipPip
  • 301 posts
  • 13

  • LocationBratislava

Posted 08 November 2011 - 09:59 AM

Tried now with latest commit. It works good now...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users