Jump to content

How to set language = active via API?


dragan
 Share

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)

  • Like 1
Link to comment
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 8
Link to comment
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 17
Link to comment
Share on other sites

  • 1 month later...
  • 2 years later...

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 6
Link to comment
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
Link to comment
Share on other sites

  • 10 months later...

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 3
  • Thanks 1
Link to comment
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
Link to comment
Share on other sites

  • 8 months later...

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
Link to comment
Share on other sites

  • 1 year later...
  • 3 years later...

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
 Share

×
×
  • Create New...