Jump to content
AlexV

Addition of some items into a Pagefield field via api

Recommended Posts

Hi guys!

I want to import into multiple pages at once the same value of the field, which is Pagefield. I make a selection of pages, and how to specify the page in Pagefield do not know.

Likely this question already rose, but at a forum I didn't find the answer.

Example: Pagefield is called «subway_station» in it I have to choose two out of a hundred pages, called «green» and «orange», as it be?

foreach($pages->get("/district/")->find("district_region=UZAO") as $p) {
echo "<a href='{$p->url}'>{$p->title}</a> " ;
$p->subway_station->add("/subway_station/sub_st_0111/"); // 
$p->save();
}

Share this post


Link to post
Share on other sites

To save a field you have to use either $page->set("field", $value) or $page->$field = $value.

You should also make $page->setOutputFormatting(false) before making the changes to the page.

I'm not completely sure that this is what you want, but for the interpretation that I make from your code and assuming that all those pages have a template that contains the "subway_station" field, you should be doing this:

// this code will find all the UZAO pages that are descendent of /district/, and add the page "/subway_station/sub_st_0111/" to their "subway_station" field

foreach($pages->get("/district/")->find("district_region=UZAO") as $p) {
  echo "<a href='{$p->url}'>{$p->title}</a> " ;
  $p->setOutputFormatting(false); // this should be after the echo
  $p->subway_station = $pages->get("/subway_station/sub_st_0111/");
  $p->save();
}

I didn't understand where «green» and «orange» were on your code, so interpreted only the code, and left this detail out.

Edit: edited the code above by Ryan's suggestion

  • Like 2

Share this post


Link to post
Share on other sites

Thank you Diogo!

After your edition the code works!

«Green» and «orange» — two pages that had to be chosen in the Pagefield.

For an example: page title «green», and its address "/subway_station/sub_st_0111/", but how to specify the «orange» (with address "/subway_station/sub_st_0222/"), I didn't know, therefore in a code only one, «green».

Tell please, what syntax of record $value. It can be "title" or "id" or "address" of several pages?

Share this post


Link to post
Share on other sites

Again, I'm not sure I understand what you want.

If you want to add more than one page to the "subway_station" field, make sure this field accept multiple pages in it's settings, and then do it simply like this:

$p->subway_station = $pages->get("/subway_station/sub_st_0111/");
$p->subway_station = $pages->get("/subway_station/sub_st_0222/");
$p->save();
Tell please, what syntax of record $value. It can be "title" or "id" or "address" of several pages?

What $value? I don't understand :)

Share this post


Link to post
Share on other sites

Thank you again, Diogo!

That's right, I want to add a few pages in Pagefield "subway_station".

To save a field you have to use either $page->set("field", $value) or $page->$field = $value.

$value — I took out the one you specify an example.

I thought that is possible to define a set of pages in shorter way, to list id or title.

Share this post


Link to post
Share on other sites

Oh, that $value!

No, In this case $value must be a page object or anything that returns one, just like in the code examples above $pages->get("something"), because this is what the pageType field expects.

Only If you would change the value of another kind of field, like "title" for example, you could use a string, or anything that returns one

$p->title = $pages->get("something")->title;
//or
$p->title = "new title";

Share this post


Link to post
Share on other sites

Just a quick note about output formatting. You always want it on before outputting anything. This ensures that when you output $page->title (for instance) that characters like '&' will get entity encoded to '&', and the like. Whereas, if you were setting values to the page, you'd want it to store '&' rather than '&'. So updating an earlier example, you'd want to do this:

echo "<a href='{$p->url}'>{$p->title}</a> " ; // output formatting should be ON here since you are echoing output. 
// now you want output formatting OFF, since you are setting values and saving a page: 
$p->of(false); // same as setOutputFormatting(false), shorter syntax.
$p->subway_station = $pages->get("/subway_station/sub_st_0111/");
$page->save();
  • Like 2

Share this post


Link to post
Share on other sites

Thanks Ryan and Diogo for the clarification!

Now, the function of line $p->setOutputFormatting(false); became clear to me.

Share this post


Link to post
Share on other sites
My favorite type of links recently are like: $p->setOutputFormatting(false|true);

It's true that that's a very nice feature on the cheatsheet. Well done Soma ;)

Share this post


Link to post
Share on other sites

Thanks for the tip, Soma!

I often there look.

My experience does not allow to use it so effectively, so as not to ask questions to more experienced users ;)

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 quickjeff
      Hi Guys, 
      I have been debugging a site for the last 2 hours and cannot solve the issue. 
      I have a site running on 3.0.148. 
      I installed the Kongondo Blog module and was updating the templates to include the website style. 
      Once everything was set and done, I checked the page tree to see an error appear. 
      Template must be assigned a name before 'filename' can be accessed
      The same error appears in templates. 
      Debugging Steps
      I checked the templates in the server to ensure I didnt accidentally delete the namespace.  Deleted cache in browser and server under assets Still no go. 
      Any help is appreciated. 
      Thanks! 
    • By Spyros
      Hello
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      PS 
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
    • By Guy Incognito
      This short script loops through some images from an XML feed and pushes new ones to an image field. It all works perfectly, except for some reason the last image (only) in the loop each time doesn't receive the image description... can everyone spot why? TIA! 🙂 
      foreach ($propertyImages as $img) { $fileName = trim($img[0]); if ( !empty($fileName) ) { $imgPath = '../property_data/'.$fileName; if(file_exists($imgPath) && !in_array(strtolower($fileName),$currentImages)) { $p->property_images->add($imgPath); $p->save(); $newImg = $p->property_images->last(); $newImg->description = $img[1]; $p->save(); } } }  
    • 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'); } } ?>  
×
×
  • Create New...