ralberts Posted October 4, 2013 Share Posted October 4, 2013 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 More sharing options...
interrobang Posted October 4, 2013 Share Posted October 4, 2013 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 More sharing options...
ralberts Posted October 5, 2013 Author Share Posted October 5, 2013 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 More sharing options...
ralberts Posted October 7, 2013 Author Share Posted October 7, 2013 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 More sharing options...
ryan Posted October 12, 2013 Share Posted October 12, 2013 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 More sharing options...
ralberts Posted October 15, 2013 Author Share Posted October 15, 2013 Thanks for the response Ryan. I already figured out that moving the code to a template file solved my problem. Enalbing output formatting is a valuable hint though, thanks! 1 Link to comment Share on other sites More sharing options...
ryan Posted October 20, 2013 Share Posted October 20, 2013 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 More sharing options...
alexpi Posted April 8, 2014 Share Posted April 8, 2014 Is it possible to get the user language on a page called with AJAX, that has PW bootstrapped? I am using this code: $language = $wire->user->language; but it doesn't seem to work. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now