-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Language prefixes do not work in idType: URI
queries
#35
Comments
@esamattis Could you provide a pointer here, where to start/ fix that? 🙏🏽 |
@gustavpursche Hey, I ultimately use REST API altogether with GraphQL (yes, that means multiple network requests) to fetch page IDs. |
Thanks for the report! The Quick look at the wp-graphql code points the issue here: The frontpage logic is hard coded. We need to inject our own custom logic to detect the translated frontpages. Unfortunately the But @jasonbahl of wp-graphql has been very open to adding filters to wp-graphql to support things like this. For example a short circuiting filter like $front_page_post = get_option('page_on_front');
foreach (pll_languages_list('slug') as $lang_slug) {
if ($parsed_url['path'] === "/$lang_slug") {
$post = pll_get_post($front_page_post, $lang_slug);
if ( $post ) {
return $post;
}
}
} |
We might be able to wrap the existing page resolver function with the Here's an example how to override it #38 |
Ah, this would be nice. For now I will have to hard-code the frontpage slug in the code, but full support for the URI would be great. |
idType: URI
queries
There's an user land based workaround by @gorkalaucirica here: #46 (comment) Maybe we can build something like that in to this plugin. |
idType: URI
queriesidType: URI
queries
Any update on this ? |
A temporary workaround. This fixed it for my use-case: /**
* Bugfix: prevent fetching pages from other languages. Without below filter, /de/about-us would resolve
* to the english post called /en/about-us, which will give SEO issues. It also fixes an issue if two
* posts in two different languages had the same post_name, the wrong one would/could be resolved
* depending on what language you was requesting
*
* https://github.com/valu-digital/wp-graphql-polylang/issues/35
*/
add_filter( 'request', function ( $query_args) {
if(isset($query_args['uri']) && $query_args['uri'] && isset($query_args['lang'])) {
$explodedUri = explode('/', $query_args['uri']);
if($explodedUri[0] !== $query_args['lang']) {
// query was made without language slug in URI
return $query_args;
}
unset($explodedUri[0]); // remove language slug from URL
$uriWithoutLang = implode('/', $explodedUri); // rebuild the URI without the slug in front
if(function_exists('pll_get_post_language')) {
$post = get_page_by_path($uriWithoutLang);
if(!$post || $post && pll_get_post_language($post->ID) !== $query_args['lang']) {
$query_args = [];
}
}
}
return $query_args;
}, 10, 2); It's not the prettiest, but it does it's job. We had an issue if two pages was called the same, e.g. /de/about us and /en/about-us, when requesting the /de/about-us, it would resolve the english version. Also we had an issue where if you requested an english slug, on the german prefix, if a german page did not exist with that name, it would return the english page - on the german site. Above filter also fixes that. |
I am also experiencing this issue. It is not possible to request the translated homepage via a |
Needs some polish but I think this will work for querying <?php
add_filter('graphql_resolve_field', function ($result, $source, $args, $context, $info, $type_name, $field_key, $field, $field_resolver) {
if ($type_name === 'RootQuery' && $field_key === 'nodeByUri') {
// explode uri and get only the middle part
$exploded = explode('/', $args['uri']);
$lang = $exploded[1];
// if $exploded length is more than 3 it means that the uri is not a front page
if (count($exploded) > 3) {
return $result;
}
// if $exploded length is less than 2 it means that the uri is not a front page
if (count($exploded) < 2) {
return $result;
}
// check if language exists in pll
$valid_languages = pll_languages_list();
if (in_array($lang, $valid_languages)) {
// get translated post object
$homepage_id = get_option('page_on_front');
$frontpage = pll_get_post($homepage_id, $lang);
$page = get_post($frontpage);
// 💖
$result = new \WPGraphQL\Model\Post($page);
}
}
return $result;
}, 10, 9); |
I've improved @maximilliangeorge code, this way I was able to solve all my problems. To make this work you have to query using NodeByUri and pass the URL always with a leading slash ( / ). My use case was:
Also, after getting the basic infos I needed from the page, I've used it's ID to query all the other infos (I'm querying each components props with its own query), which allowed me to use the Page query type.
Edit: |
@assef Thanks, I was having trouble with my aforementioned solution after upgrading WPGraphQL. However, your altered solution above works! Thanks for saving me a bunch of time. |
I really love your solution: Thanks so much for sharing! 🙏 I sometimes get empty strings in the
|
Hey,
I think at the current time it is literally impossible to fetch home page of non default language. Everything works fine for pages. Take look at full report listed below.
Fetching default home page
Fetching home page for selected language
Disclaimer: Expected page exists, uses directory based permalinks and is available through WordPress through URL like: wordpress-instance.com/pl
Fetching normal page for selected language
Fetching normal page for selected language without language prefix
Unless I am doing something wrong, I think there could be a great benefit by exposing additional
language
parameter underpageBy
entity, alongside already existingid
,pageId
, anduri
so that individual page could be safely retrieved in expected language. This should also fix fetching home page for different language issue at the same time.What do you think?
PS. Excellent package by the way!
The text was updated successfully, but these errors were encountered: