Jump to content

Recommended Posts

I am about to create several pages via API. In another script I populate the fields per language. 

I found that each page generated via API, even when alternative languages are also populate, they are inactive by default.

Is there an API method to set it to active? Either when creating a page, or when updating it (setting field values).

(PW 2.3.2 setup)

Share this post


Link to post
Share on other sites

 Without knowing or testing

post-100-0-70430100-1377875172_thumb.png

it must be status1015=1 (where id is the language)

  • Like 1

Share this post


Link to post
Share on other sites

Thanks!

Just in case anybody else searches for similar things, here's a simple example

$en = 'status' . $languages->get('en');
$us = 'status' . $languages->get('us');
$fr = 'status' . $languages->get('fr');

$pag = $pages->find("parent=1072, template='product'");

foreach($pag as $p) {
    $p->setOutputFormatting(false); // not sure if necessary here - I made it a habit to always include it, just in case...
    $p->$en = 1;
    $p->$fr = 1;
    $p->$us = 1;
    $p->save();
} 
  • Like 5

Share this post


Link to post
Share on other sites

In case you want it a little more simpler :)

$pages->setOutputFormatting(false);
$pag = $pages->find("template=basic-page");
foreach($pag as $p) {
    foreach($languages as $lang) {
      if($lang->isDefault()) continue;
      $p->set("status$lang", 1);
      $p->save();
    }
} 
  • Like 12

Share this post


Link to post
Share on other sites

Just hitted this myself. Should all languages be active from API by default? That would be in line with how it functions in admin.

  • Like 5

Share this post


Link to post
Share on other sites

Just a little update because of new API additions, so now you could just do

foreach ($pag as $p) {
    foreach ($languages->find('name!=default') as $lang) {
        $p->setAndSave("status$lang", 1);
    }
}

Thanks @szabesz for mentioning I'm adding this here for completeness

Quote

"This method does not need output formatting to be turned off first, so make sure that whatever value(s) you set are not formatted values.", which is not the case with languages but worth mentioning nonetheless: https://processwire.com/api/ref/page/set-and-save/

 

Edited by Can
  • Like 5

Share this post


Link to post
Share on other sites
17 hours ago, Can said:

could just do

Thanks Can! We might want to add: "This method does not need output formatting to be turned off first, so make sure that whatever value(s) you set are not formatted values.", which is not the case with languages but worth mentioning nonetheless: https://processwire.com/api/ref/page/set-and-save/

  • Like 1

Share this post


Link to post
Share on other sites

took me some time today to find out why my page was not showing up...

i ended up creating a hook that does what apeisa had in his mind some years ago :D

On 26.10.2013 at 5:28 PM, apeisa said:

Just hitted this myself. Should all languages be active from API by default? That would be in line with how it functions in admin.

// set all languages active automatically
$wire->addHookAfter('Pages::added', function($event) {
  $page = $event->arguments(0);
  foreach ($this->wire->languages as $lang) $page->set("status$lang", 1);
  $page->save();
});

does anybody see any danger in that? i'm not so experienced with multilang yet...

edit: somehow it seems that ->setAndSave(...) does NOT work in this situation. @Can or @dragan can you confirm that?

  • Like 2

Share this post


Link to post
Share on other sites

I'm sorry, but I have never used hooks until now. When creating pages directly with the API, I made it a habit to simply add the necessary language actions in the same script.

Basically, you then use $page->save() twice.

1st step: add page, define parent, template, fill field values etc. save #1.

2nd step: with the new page id you get, add the lang. actions (status etc.). save #2.

  • Like 1

Share this post


Link to post
Share on other sites

If you want to do this just for a specific language, find the ID of this language and replace {id}, including the curly braces with this ID in the following code:

$pag = $pages->find("has_parent!=2");
foreach($pag as $p) {
   $p->setAndSave("status{id}", 1);
}

 

  • Like 1

Share this post


Link to post
Share on other sites
On 9/5/2016 at 3:36 PM, Can said:

 


foreach ($pag as $p) {
    foreach ($languages->find('name!=default') as $lang) {
        $p->setAndSave("status$lang", 1);
    }
}

 

There is now also:

$languages->findNonDefault()

 

  • Like 1

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