Jump to content

Accessing PageTitleLanguage when bootstrapped


ralberts
 Share

Recommended Posts

Hi folks,

I'm looking to return results from PW in an Ajax call, but I'm not sure if I'm going about this the right (or preferred) way.

I created a script in /htdocs/public/ajax/affiliates.php, which is called using Ajax.

The script itself bootstraps Processwire and returns JSON for all affiliates. It looks something like this:

<?php

require_once '../../index.php';

$data['markers'] = array();

$markers = array();
$affiliates = wire()->pages->find("template=affiliate");
foreach ($affiliates AS $affiliate)
{
	$marker['latitude'] = $affiliate->mapmarker->lat;
	$marker['longitude'] = $affiliate->mapmarker->lng;
	$marker['title'] =  $affiliate->title;	

	$data['markers'][] = $marker;
}

print(json_encode($data));

The problem here is that $affiliate->title returns an object (LanguagesPageFieldValue), instead of the actual string I would expect it to do.

I'm not sure where I'm going wrong here. I may have to manually set the language, call the title in a another manner, or just use a different approach alltogether. Any help is much appreciated :-)

Link to comment
Share on other sites

title->getLanguageValue is the method you are looking for. Something like this should work:


<?php

require_once '../../index.php';

$data['markers'] = array();

$markers = array();

$affiliates = wire()->pages->find("template=affiliate");

foreach ($affiliates AS $affiliate)

{

$marker['latitude'] = $affiliate->mapmarker->lat;

$marker['longitude'] = $affiliate->mapmarker->lng;

foreach (wire('languages') as $language) {

$marker['title_' . $language->name] = $affiliate->title->getLanguageValue($language);;

}

$data['markers'][] = $marker;

}

print(json_encode($data));

Link to comment
Share on other sites

Hi Interrobang,

Thanks for your response. That might work, assuming I can fetch the language of the current user in my bootstrapped script. I cannot test this right now, but wire()->user->getLanguage() combined with your suggestion should do the trick.

Thanks again!

Link to comment
Share on other sites

Made a small workaround by adding the users language to the ajax request to get this to work:

<?php

require_once '../../index.php';

$data['markers'] = array();

$markers = array();
$affiliates = wire()->pages->find("template=affiliate");

$language = wire()->languages->get("default");
if (isset($_GET['language'])) {
	$language = wire()->user->language = 1010;
}

foreach ($affiliates AS $affiliate) {
	$marker['latitude'] = $affiliate->mapmarker->lat;
	$marker['longitude'] = $affiliate->mapmarker->lng;
	$marker['title'] = $affiliate->title->getLanguageValue($language);

	$data['markers'][] = $marker;
}

print(json_encode($data));

It feels a bit hacky and I guess there is a better way to achieve this. So any input is more then welcome, but for now I'm happy with this solution :)

Link to comment
Share on other sites

You might want to move this into a PW template file (the exact code, minus the require_once at the top). This would ensure things are output formatted and you can take advantage of PW determining the language for you and such. Though the way you are doing it is just fine, but since you are bootstrapping PW output formatting is not enabled by default. You can enable output formatting for your $affiliate pages, so that you are dealing with formatted values. In your case, that would mean $title would be a string rather than an object. 

$affiliate->of(true); 

You may also want to add a header before outputting your JSON:

header("Content-Type: application/json"); 
Link to comment
Share on other sites

Btw, most objects in ProcessWire can also just be typecast to strings, which usually gives you the intended value. This would be the case for a LanguagePageFieldValue object too. 

$value = (string) $object; 

Link to comment
Share on other sites

  • 5 months 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

  • Recently Browsing   0 members

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