Jump to content

How to set page name in different languages through the api?


lpa
 Share

Recommended Posts

I have tried to find an answer to my problem without luck:

How do I set the different page names with api?

When trying this: 

$lalias = "the-name-of-the-page-in-english";
$english =  wire("languages")->get("english");
$lalias_name = wire("sanitizer")->pageName($lalias);
$page->set("status$english",1);
$page->save();
$page->name->setLanguageValue($english,$lalias_name);
$page->save();

I get an error like this:

 
Fatal error: Call to a member function setLanguageValue() on a non-object in import.php on line 6 (the second last line)
 
I tried also this:
$page->name1013->setLanguageValue($english,$lalias_name); // $english = 1013;
 
but it didn't work either.
 
And do I have to set the status$english = 1 for each page that has a different name as the default name?
 
 
 
Link to comment
Share on other sites

Thanks, adrian!

My actual problem is not with setting the status = 1 but with setting the alternate $page->name in the other language. I have tried $page->name$english and $page->name->setLanguageValue($english,$lalias_name), but they don't work. 

The scope is not the problem either, because I am using $page = new Page(); and it works with all the other stuff before those lines. 

Link to comment
Share on other sites


$lalias = "the-name-of-the-page-in-english";

$english = wire("languages")->get("english");

$lalias_name = wire("sanitizer")->pageName($lalias);

$page->set("status$english",1);

$page->set("name$english",$lalias_name);

$page->save();

  • Like 4
Link to comment
Share on other sites

$lalias = "the-name-of-the-page-in-english";
$english = wire("languages")->get("english");
$lalias_name = wire("sanitizer")->pageName($lalias);
$page->set("status$english",1);
$page->set("name$english",$lalias_name);
$page->save();

Thanks, Soma!

It still doesn't set the name1013 field at all. $english = 1013. I just don't understand why. 

And why couldn't I use this: 

  1. $page->name->setLanguageValue($english,$lalias_name);
Link to comment
Share on other sites

The way Soma mentioned is correct. Double check that you are up-to-date on the PW dev branch and that you have the LanguageSupportPageNames module installed (I'm assuming you do, but just double checking). Next, is $english your default language? If so, then you should set it with the 'name' property (without any language appended to it). Your code doesn't indicate it's setting the values for the default language, and that would be a requirement for a new page. 

And why couldn't I use this: 
$page->name->setLanguageValue($english,$lalias_name);

setLanguageValue() is a function of custom multi-language fields, and it's what is returned by FieldtypeTextLanguage, FieldtypeTextareaLanguage, FieldtypePageTitleLanguage. You'll only use this with multi-language fields of those types you would see in your admin under Setup > Fields.

Both 'name' and 'status' are not fields -- they are properties native to all pages an they are simple types: name is a string and status is an integer. Any multi-language properties like this are set by appending the language ID to the property name, i.e. $page->set("name$spanish", "la-latina"); But if you are just setting the default language value, then no need to append the language, as you would just do $page->set("name", "something");  

Link to comment
Share on other sites

I just ran into this myself. I can set the language page names when creating a page or in a Pages:added hook, but when using a Pages::saveReady hook it doesn't work.

Simplifyed example:

in a Pages::added hook it works setting the page names.
 
...
foreach($this->languages as $lang){

    $lname = $lang->isDefault() ? '' : $lang->id;
    $default = $this->languages->get("default");
    $time = time();
    
    if($page->title->getLanguageValue($lang)){
        $page->set("name$lname", $time . "_" . $page->title->getLanguageValue($lang));
    } else {
        $page->set("name$lname", $time . "_" . $page->title->getLanguageValue($default));
    }
}

$page->save(); 

After page is created and in a Pages:saveReady hook the following only sets the default page name, the alternatives stay untouched.

...
$page->of(false);
foreach($this->languages as $lang){

    $lname = $lang->isDefault() ? '' : $lang->id;
    $default = $this->languages->get("default");
    
    if($page->title->getLanguageValue($lang)){
        $pageName = $prefix . "_" . $page->title->getLanguageValue($lang);
        $pageName = $this->getUniquePageName($page, $pageName);
        $page->set("name$lname", $pageName);
    } else {
        $pageName = $prefix . "_" . $page->title->getLanguageValue($default);
        $pageName = $this->getUniquePageName($page, $pageName);
        $page->set("name$lname", $pageName);
    }
}
$page->skip = true;
$page->save();
 

The values for the $pageName are all correct but nothing seems to work.

Edit: just changed the hook to Pages::save, and it works now... no idea why

Link to comment
Share on other sites

Most likely you are competing with a hook running from LanguageSupportPageNames. I'm guessing your hook is running and doing something, and the hook in LanguageSupportPageNames is undoing what you did. Since LanguageSupportPageNames is using "saveReady" rather than "save", by you switching to "save" I think that's why your results are more predictable. If you really needed to use saveReady, you may also be able to resolve the issue by setting a hook priority value or attaching your hook at a later point than LanguageSupportPageNames does (perhaps in a ready() rather than an init, or a render() rather than a ready(), etc.). But if the "save"hook accomplishes what you need, I'd stick with that solution. 

Link to comment
Share on other sites

Thanks Ryan! This still doesn't work for me:

I am running this, where english is not my default language:

#!/usr/bin/php
<?php
require('./index.php'); 
$english = wire("languages")->get("english");
echo "Language: $english\n";

$p = wire("pages")->get("/koulutus/");
echo "Page: $p\n";
$p->set("status$english",1);
echo "status:".$p->status1013."\n";
echo "Name $english before:".$p->name1013."\n";
$p->set("name$english","education");
echo "Name $english after:".$p->name1013."\n";
$p->save();
echo "Name $english saved:".$p->name1013."\n";

What I get is:

Language: 1013
Page: 1163
status:1
Name 1013 before:
Name 1013 after:education
Name 1013 saved:education
 
But after running this again, Name 1013 before is empty again and in the database it is like this:
 
1163    1    29    koulutus    1    2013-10-26 23:53:00    40    2013-09-29 16:05:32    40    1    NULL    1
 
Where name1013 is the second last field with NULL.
 
And I am running the latest dev-version 2.3.5. loaded just today from GitHub.
Link to comment
Share on other sites

@lpa: LanguageSupportPageNames works by hooking several things, including items involved in rendering pages. In your case, you are bootstrapping ProcessWire yourself and so none of the ProcessPageView / PageRender hooks would be executed. I'm wondering if that might be the problem here. While I still think what you are doing should work (ideally) I'm wondering if moving your code to a template file, and then viewing a page using that template makes any difference?

Link to comment
Share on other sites

Thanks, ryan! It works when running the same code from a template-file. BUT I think there is a bug if it doesn't work when bootstrapping. None of the functions I use should have anything to do with PageRender or ProcessPageView. There is only some get, set and save methods used.

Link to comment
Share on other sites

  • 2 months later...

Any news on this? I just ran into the same issue. When bootstrapped I cloud not set/save a new pagename for non-default languges. The same code in a template file works without problems. I am running the latest dev.

include("./index.php");
foreach (wire('pages')->get('/projects')->children as $p) {
 
    $p->of(false);
 
    foreach (wire('languages') as $lang) {
        $title_translated = $p->title->getLanguageValue($lang);
        if ($lang->isDefault() || !$title_translated) {
            continue;
        }
 
        $pagename_field = "name$lang";
        $pagename_translated = wire('sanitizer')->pageName($title_translated, Sanitizer::translate);
 
        // only set new translated pagename when empty
        if (strlen($p->get($pagename_field)) > 0) {
            continue;
        }
 
        $p->set("status$lang", 1);
        $p->set($pagename_field, $pagename_translated);
        $p->save();
    }
}
Link to comment
Share on other sites

As vagely mentioned the LanguageSupportPageNames module is the problem here, as it doesn't load in a Bootstrap it seems. There's no page rendering happening so ready() method of it will never get called.

I'm using heavy page creation importing with multilanguage and also ran into this, and also need a solution sooner or later, so i tried again.

A solution I found is to add a hook yourself in the script on Pages::saveReady and delegate that to the LanguageSupportPageNames which usually seem to handle this part of page name saving.

So if you add this code above your bootstrap, it will work as if in a page template.

wire()->addHookAfter("Pages::saveReady", null, "hookPageSaveReady");
function hookPageSaveReady($event){    
    wire("modules")->LanguageSupportPageNames->hookPageSaveReady($event);
}
  • Like 6
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...