Jump to content

Recommended Posts

Posted

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
Posted

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
Posted

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
  • Thanks 1
  • 1 month later...
Posted

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 7
  • 2 years later...
Posted (edited)

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
  • 10 months later...
Posted

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 2
Posted

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
  • 8 months later...
Posted

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
  • 1 year later...
Posted
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
  • 3 years later...
  • 1 year later...
Posted

Just to add to this topic, Page class has a function to enable language, a bit cleaner than using "status$lang":

foreach (languages()->findNonDefault() as $language) {
    $page->setLanguageStatus($language, true);
}

 

  • Like 1

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
×
×
  • Create New...