Jump to content

Localized URL for page in bootstrapped external script


skidr0w
 Share

Recommended Posts

Hello Processwire community,

I try to build an API which works with PW pages as datastore. A reduced example script looks like this:

<?php

include 'index.php'; // PW bootstrap

$sanitizer = wire('sanitizer');
$id = $sanitizer->selectorValue(wire('input')->get->id);
$page = wire('pages')->get("id=$id");

$lang_code = $sanitizer->selectorValue(wire('input')->get->lang);
$language = wire('languages')->get($lang_code);

$page->of(false);

$output = array(
	'content' => $page->content->getLanguageValue($language),
	'url'    => $page->localUrl($language),
);

header('Content-Type: application/json');
echo json_encode($output);

But this does not work as expected. I get the following error, when I call this script:

Exception: Method Page::localUrl does not exist or is not callable in this context

The "Languages Support - Page Names" module is installed and loaded. I verified this by calling assert(class_exists('LanguageSupportPageNames', false) === true); in the script, which worked.

How can I make this script output the localized URL for a page? I hope you can help me.

Link to comment
Share on other sites

Hi skidr0w and welcome to PW.

I am not a multi-language guy, but have a read from this post onwards:

https://processwire.com/talk/topic/2979-multi-language-page-names-urls/?p=35403

I skimmed quickly, so not sure how much it helps.

Thanks adrian,

I already read this thread. The solution to set the $user->language variable temporary and just use $page->url does not work in my case. I'm not sure why, but $user->language is completely ignored in an external script. All fields display in the default language. Setting the $user->language variable does not change this effect.

Link to comment
Share on other sites

In a bootstrap the Module isnt loaded but you can load it manually in your script.

$user doesn't exist in a bootstrap. Use wire("user") instead.

Thanks for your reply Soma,

I tried loading the module with wire('modules')->get('LanguageSupportPageNames'); but this does not change the error.

Even though I wrote $user, in my script I used wire('user'). The language change is not adopted by the fields.

Link to comment
Share on other sites

Had to take a quick look to remember how I've done it. Thing is the modules is loaded but the ready() is never triggered in bootstrap and those language aware API is only added via hooks. So I did add those hooks in my script and delegate the event to the module manually.

<?php

include("index.php");

$lang = wire("languages")->get("default");

// manually add hook to use localUrl
wire()->addHook("Page::localUrl", null, function($event){
    wire("modules")->LanguageSupportPageNames->hookPageLocalUrl($event);
});

// now works
echo wire("pages")->get(1001)->localUrl($lang);

// -------

// manually add hook to use user language
wire()->addHookAfter("Page::path", null, function($event){
    wire("modules")->LanguageSupportPageNames->hookPagePath($event);
});

// now works
wire("user")->language = $lang;
echo wire("pages")->get(1001)->url;
  • Like 1
Link to comment
Share on other sites

Had to take a quick look to remember how I've done it. Thing is the modules is loaded but the ready() is never triggered in bootstrap and those language aware API is only added via hooks. So I did add those hooks in my script and delegate the event to the module manually.

<?php

include("index.php");

$lang = wire("languages")->get("default");

// manually add hook to use localUrl
wire()->addHook("Page::localUrl", null, function($event){
    wire("modules")->LanguageSupportPageNames->hookPageLocalUrl($event);
});

// now works
echo wire("pages")->get(1001)->localUrl($lang);

// -------

// manually add hook to use user language
wire()->addHookAfter("Page::path", null, function($event){
    wire("modules")->LanguageSupportPageNames->hookPagePath($event);
});

// now works
wire("user")->language = $lang;
echo wire("pages")->get(1001)->url;

That approach works, but is very ugly in my opinion. Is there a reason, why modules ready() method is never triggered?

Link to comment
Share on other sites

  • 3 weeks later...

Hi there,

somehow I cannot get the localization work in an external script...

I don't need to verify which language is actually set - just need to tell the script that it should display one specific language and tried it as follows:

include("/home/xyz/public_html/pw/index.php"); 
$lang = wire("languages")->get("en");
wire()->addHook("Page::localUrl", null, function($event){
    wire("modules")->LanguageSupportPageNames->hookPageLocalUrl($event);
});
$referenzen = wire("pages")->get("/referenzen/")->localUrl($lang);
	$references = $referenzen->children;
	foreach($references as $reference) {
		echo "<li class='references-item'>";
		echo "<p>{$reference->title}</p>";
		echo "</li>";
	}

Any idea what I made wrong?

Cheers, Christian

Link to comment
Share on other sites

You're mixing up things.

This should be more like

include("/home/xyz/public_html/pw/index.php");
wire("user")->language = wire("languages")->get("en");

$referenzen = wire("pages")->get("/referenzen/");
    $references = $referenzen->children;
    foreach($references as $reference) {
        echo "<li class='references-item'>";
        echo "<p>{$reference->title}</p>";
        echo "</li>";
    }
  • Like 1
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...