Jump to content


Photo

Addition of some items into a Pagefield field via api

Pagefield api

  • Please log in to reply
10 replies to this topic

#1 AlexV

AlexV

    Jr. Member

  • Members
  • PipPip
  • 21 posts
  • 1

  • LocationMoscow

Posted 11 June 2012 - 09:18 AM

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/"); // <img src='http://processwire.com/talk/public/style_emoticons/<#EMO_DIR#>/huh.gif' class='bbc_emoticon' alt='???' />
$p->save();
}


#2 diogo

diogo

    Hero Member

  • Moderators
  • 2,014 posts
  • 1092

  • LocationPorto, Portugal

Posted 11 June 2012 - 10:12 AM

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 http://processwire.c...api/#entry12628

#3 AlexV

AlexV

    Jr. Member

  • Members
  • PipPip
  • 21 posts
  • 1

  • LocationMoscow

Posted 11 June 2012 - 12:52 PM

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?

#4 diogo

diogo

    Hero Member

  • Moderators
  • 2,014 posts
  • 1092

  • LocationPorto, Portugal

Posted 11 June 2012 - 01:04 PM

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

#5 AlexV

AlexV

    Jr. Member

  • Members
  • PipPip
  • 21 posts
  • 1

  • LocationMoscow

Posted 11 June 2012 - 01:32 PM

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.

#6 diogo

diogo

    Hero Member

  • Moderators
  • 2,014 posts
  • 1092

  • LocationPorto, Portugal

Posted 11 June 2012 - 01:53 PM

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";


#7 ryan

ryan

    Hero Member

  • Administrators
  • 5,780 posts
  • 3125

  • LocationAtlanta, GA

Posted 11 June 2012 - 02:49 PM

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 '&amp;', and the like. Whereas, if you were setting values to the page, you'd want it to store '&' rather than '&amp;'. 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();


#8 AlexV

AlexV

    Jr. Member

  • Members
  • PipPip
  • 21 posts
  • 1

  • LocationMoscow

Posted 11 June 2012 - 03:02 PM

Thanks Ryan and Diogo for the clarification!
Now, the function of line $p->setOutputFormatting(false); became clear to me.

#9 Soma

Soma

    Hero Member

  • Moderators
  • 3,217 posts
  • 1764

  • LocationSH, Switzerland

Posted 12 June 2012 - 08:12 AM

My favorite type of links recently are like: $p->setOutputFormatting(false|true);

Simple to look on cheatsheet for reference. I recommend to have it open in your browser all the time ;)

@somartist | modules created | support me, flattr my work flattr.com


#10 diogo

diogo

    Hero Member

  • Moderators
  • 2,014 posts
  • 1092

  • LocationPorto, Portugal

Posted 12 June 2012 - 08:20 AM

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

#11 AlexV

AlexV

    Jr. Member

  • Members
  • PipPip
  • 21 posts
  • 1

  • LocationMoscow

Posted 12 June 2012 - 08:26 AM

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users