Jump to content

Cannot get localized data when boostrapping PW externally


DanielBlaBla
 Share

Recommended Posts

Hi guys, I didn't really want to bother until I tried everything, but I am stuck and cannot move on.

I am building an API for Gatsby website on Processwire and I cannot get l cannot get some of the localized data, no matter how am I trying to call it.
The page has two languages, default Czech and second English.

Even if I call directly for an English version of the page, I still get czech URL... ?

wire('pages')->get(1022)->url;

 

I also tried to set the context before getting the page, still getting czech URL.

$english = wire('languages')->get('en');
wire('user')->language = $english;
$page = wire('pages')->get(1022);
echo $page->url;

 

I am using this module https://processwire.com/modules/rest-api/ and this (simplified) code:

<?php

// Bootstrap processwire
namespace ProcessWire;

class Product {

    // Get all products
    public static function getAll() {

        // Empty response
        $response = new \StdClass();
        $response->products = [];

        // Getting all processwire pages
        $pages = wire('pages')->find("template=product");
        foreach ($pages as $page) {

            // For each of page's language
            $langs = $page->getLanguages();
            foreach ($langs as $lang) {

                // Fill product and add to response
                $product = [
                  	"lang" =>$lang->name, // Works fine, returns 'en'
                    "url" => $page->url($lang), //{"error":"Method Page::localUrl does not exist or is not callable in this context"}
                    "parent" => $page->parent->url($lang), //{"error":"Method Page::localUrl does not exist or is not callable in this context"}
                    "title" => $page->title->getLanguageValue($lang->id), // Works fine
                    "product_description" => $page->product_description->getLanguageValue($lang->id), // Works fine
                    "product_category" => $page->product_category->title->getLanguageValue($lang->id),  // {"error":"Call to a member function getLanguageValue() on string"}
                ];
                array_push($response->products, $product);
            }
        }
        return $response;
    }
}

My problems:

  1. I have a selectbox field product_category in my template and I cannot get translated version of it. All other field types work fine.
  2. I am always getting Czech URLS for both page and the parrent. I tried putting the language object or the ID as a parametere, but getting error above.

When I output the variable normally in the template, the field and the URL works fine. I don't know what I am doing wrong.

I will be glad for any help.
Thank you
Daniel

 
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...