Jump to content
EyeDentify

Example: Add page/sub-page via PW API

Recommended Posts

Hello Fellow PW fans.

I have since i started using PW allways been a bit afraid to use the API:s more advanced functions.

Like creating pages or subpages on the fly in my PHP code in the page template file.
So i read alot and finaly got a Eureka the other day. And have now tinkered around a bit and this simple example is the result.
It´s just the bare basics. You could make this more advanced in infinity.

I wanted to put this example somewhere for the future because i don´t think there is such a clean to the point example anywhere on the forums.
All the others are deep within discussion threads with advanced stuff that are not of concern for people who looking for a simple example to start from.

Notes:

You should log in as admin before trying to run this code in your page template.
I put the parent page i use in my testing as unpuplished for security.

As allways, the API reference:

https://processwire.com/api/ref/ 

is allways great to have when tinkering with the API.

Any ways, here it comes, and you have to change paths and such to your requirements.

<?PHP
/* some random strings and stuff for testing */
$randTitle = 'test_' . mt_rand(0, 1000000);
$randHash = 'test_' . md5(mt_rand(0, 100000) + time());

/* 
    Example: Creating a new sub page to a parent page via API
    Author: EyeDentify

    This example do not check if the page name allready exist it
    just tries to save it no mather what. so you have to create that
    check your self.
    
*/

/* get parent page object to add sub page object to */
$addParent = $pages->get('/api-test-start/');

/* Instantiate a new page object */
$newPage = new Page();

/* turn output formating of for property manipulation */
$newPage->of(false);

/* set the page objects parent so it is saved in the right place */
$newPage->parent = $addParent;

/* set the name of the template to be used wich we uploaded before hand. */
$newPage->template = 'tmp_api_test_post';

/* sanitize and set page name and also used in the path */
$newPage->setName($sanitizer->pageNameUTF8($randTitle));

/* the title */
$newPage->title = $sanitizer->text($randTitle);

/* set custom fields propertys, the ones we created ourselfs */
$newPage->api_test_hash = $randHash;

/* save the page object to database as child of the given parent */
$newPage->save();

/* turn on output formatting again, importent because it should be ON when outputting it in template files later on. */
$newPage->of(true);
?>

 

This is kind of a example and code template in one.

I hope this can get others started that are beginners like myself to get more advanced with the PW API.

I expect there is propably things that should be done a certain way but this is my way.

Good luck with your API adventures.

 

Update

created a gist for the github users of the PW community of this example.
https://gist.github.com/magnusbonnevier/4dbb3a28634a9b76bbf5855dd871606f
 

 

Edited by EyeDentify
Added the github Gist URL.
  • Like 2

Share this post


Link to post
Share on other sites

@EyeDentify Thanks for sharing!

May I ask why you turned on output formatting before saving? It is recommended to save first, then turn it on (if it needs to be ON, like normally in the case of the frontend).

See for example: https://processwire.com/blog/posts/processwire-2.6.9-core-updates-and-new-procache-version/#new-page-gt-setandsave-method

API doc: https://processwire.com/api/ref/page/of/

  • Like 1

Share this post


Link to post
Share on other sites
Just now, szabesz said:

@EyeDentify Thanks for sharing!

May I ask why you turned on output formatting before saving? It is recommended to save first, then turn it on (if it needs to be ON, like normally in the case of the frontend).

See for example: https://processwire.com/blog/posts/processwire-2.6.9-core-updates-and-new-procache-version/#new-page-gt-setandsave-method

API doc: https://processwire.com/api/ref/page/of/

I may have gotten things around about output filtering.

I know though that you should put it to Off when manipulating the field propertys and to ON when done doing so.

What difference it makes before saving or not i honestly dont know but i put it to ON so i don´t forget it.

But it should be ON before you output to a template with the page object.

Share this post


Link to post
Share on other sites
1 minute ago, LostKobrakai said:

You should probably take a look at this one, which does the same, but far less verbose: https://processwire.com/api/ref/pages/add/

Thank you LostKobrakai.

I have seen it, but i wanted to use this method for learning things a little more advanced but your example is certaintly a way to go.

Share this post


Link to post
Share on other sites
16 minutes ago, EyeDentify said:

But it should be ON before you output to a template with the page object.

Sure, so that we get the formatted attribute. However, I would do:

/* save the page object to database as child of the given parent */
$newPage->save();

/* turn on output formatting again, important before outputting in template files */
$newPage->of(true);

Note that it is called formatting and not filtering.

  • Like 1

Share this post


Link to post
Share on other sites

 

Just now, szabesz said:

Sure, so that we get the formatted attribute. However, I would do:


/* save the page object to database as child of the given parent */
$newPage->save();

/* turn on output formatting again, important before outputting in template files */
$newPage->of(true);

Note that it is called formatting and not filtering.

Allready ahead of you :)

  • Like 1

Share this post


Link to post
Share on other sites

Would this be a correct way of using your example Lostkobrakai ?
 

<?PHP
/* Shorter less verbose example, thanks to Lostkobrakai for the tip. */

/* array of propertys */
$pagePropertysArray['title'] = $sanitizer->text($randTitle);
$pagePropertysArray['name'] = $sanitizer->pageNameUTF8($randTitle);
$pagePropertysArray['api_test_hash'] = $randHash;

/* add to a page object with parent path and new pages template name */
$newPage = $pages->add('tmp_api_test_post', '/api-test-start/', $pagePropertysArray); 

/* save it */
$newPage->save();

/* turn output formatting on */
$newPage->of(true);
?>

 

Share this post


Link to post
Share on other sites

It can be as short as this: 

<?php
/* add to a page object with parent path and new pages template name */
$newPage = $pages->add('tmp_api_test_post', '/api-test-start/', $randTitle, array(
	'title' => $sanitizer->text($randTitle),
	'api_test_hash' => $randHash
))->setOutputFormatting(true); 

The longform function to enable output formatting does return the page instead of the current of of status, so you can simply append it, whereas ->of() would make your $newPage variable useless.

  • Like 2

Share this post


Link to post
Share on other sites
8 minutes ago, LostKobrakai said:

It can be as short as this: 


<?php
/* add to a page object with parent path and new pages template name */
$newPage = $pages->add('tmp_api_test_post', '/api-test-start/', $randTitle, array(
	'title' => $sanitizer->text($randTitle),
	'api_test_hash' => $randHash
))->setOutputFormatting(true); 

The longform function to enable output formatting does return the page instead of the current of of status, so you can simply append it, whereas ->of() would make your $newPage variable useless.

Oki. But my example was not wrong ?

Besides, i like my verbose first example :)

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By MateThemes
      Hello everyone!
      I am working with Processwire since some time. But some topics are quite hard for me.
      I have a Portfolio (Gallery) Page.
      I am build a template with Portfolio Index and pages with portfolio entries.
      Structure:
      Portfolio Index
      -- Portfolio Entry
      -- Portfolio Entry
      and so on.
      Portfolio Entry has an Image field with max 12 images and are accessible Templates. 
      Now I want to display the single Portfolio Entry on the Portfolio Index and Paginate them. In the index page all images of a single Entry page should be displayed (I should not be organized as albums, where a random image of the portfolio entry should be displayed). 
      I have no clue to achieve this. May someone could give me an advice.
      Thank you in advance!
    • By ngrmm
      I have a page with a table. Each table row has a page-reference field and a checkbox.
      The Page sends emails to all users (page-refrence->email-field) and change the value of the checkbox in a row to 1.
      It works with this:
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?> But i have to use a variable in my emailbody.inc which i'm able to get in the table-loop.
      So i do the including of the body inside my loop. But this doesn't work anymore. Page sends out the emails but is unable to change the value of the checkbox.
      I get no errors!
      I'm using ProTable
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?>  
    • By Vigilante
      In general, is there an easy way to know which method should be used to access the API?
      For example, when _ini.php is used in the theme, it would seem you have to use wire()->addHookBefore(...). But on other sites where they used ready.php, I've seen it go straight to just doing $this->addHookAfter() even though there is no class or namespace set up in the ready.php file.
      So how do I know when I can do $this, or $wire, or wire() to access things? And any other variables I'm not aware of.
      Thanks!
    • By VeiJari
      Hello forum,
      This is really a weird one, because front end editing works in a earlier website we did to a customer. 
      When I check the source code for current website it does initiate front end edit: 
      <span id=pw-edit-1 class='pw-edit pw-edit-InputfieldPageTitle' data-name=title data-page=1021 data-lang='1017' style='position:relative'><span class=pw-edit-orig>Tekijät</span><span class=pw-edit-copy id=pw-editor-title-1021 style='display:none;-webkit-user-select:text;user- select:text;' contenteditable>Tekijät</span></span>  But when I double click nothing happens (yes I'm 100% sure I'm superuser and logged in)
      I also tried to apply the front end with other methods than:
      $page->edit('title'); But didn't work either.
      We are using jquery 2.2.4, so it should not be a problem.
      Is this a bug related to current master or something else?
      Someone else having this problem as well?
    • By jom
      Hi everyone
      It seems that I don't fully understand the wireTempPath() function and I need some help.
      I use wireTempPath() to create a new location in assets/cache/WireTempDir and than copy a pdf from the assets/files/page folder to the new folder. I want the file to be accessible only for a limited time, that's why I use wireTempPath.
      The file seems to be copied to the right location, but gets deleted right afterwards, according to 
      As mentioned in the topic above, 
      $wireTempDir->setRemove(false); prevents the file to be deleted. But I like the file to be automatically deleted after a few days. So, how can I do that?
      My code so far (everything works, but the automatic removal of the tempDir folder):
      //generate and show download link $folder = time(); // timestamp as temporary folder $maxAge = (int) $settings->options_downloadlink_valid_hours * 3600; //tempDir wants maxAge as seconds $options = array( 'maxAge' => $maxAge ); $wireTempDir = wireTempDir($folder, $options); $wireTempDir->setRemove(false); $src_file = $page->ebook_download->filename; // Create a new directory in ProcessWire's cache dir if(wire('files')->mkdir($wireTempDir, $recursive = true)) { if(wire('files')->copy($src_file, $wireTempDir)){ //get subdirs from tempDir: $pos = strpos($wireTempDir, "WireTempDir"); $subdir = substr($wireTempDir, $pos, 100); $out .= "<p><a href='" . wire('pages')->get('template=passthrough')->httpUrl . "?file=" . $subdir . $page->ebook_download->basename . "' target='_blank'>$page->title</a></p>"; } } I appreciate any ideas - thanks!
      Oliver
×
×
  • Create New...