Skip to content

Commit

Permalink
Merge remote-tracking branch 'davidfaber/menuLanguages'
Browse files Browse the repository at this point in the history
  • Loading branch information
esamattis committed Sep 30, 2020
2 parents c2a8101 + 18caf1c commit 1cc7832
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
node_modules
/.env
/.vscode/launch.json
Expand Down
47 changes: 33 additions & 14 deletions src/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ static function translate_menu_location(
string $location,
string $language
): string {
$language = strtolower($language);
return "${location}___${language}";
}

Expand All @@ -29,35 +28,55 @@ function init()
);

add_filter(
'graphql_menu_item_connection_args',
'graphql_connection_query_args',
[$this, '__filter_graphql_menu_item_connection_args'],
10,
1
2
);
}

function __filter_graphql_menu_item_connection_args(array $args)
function __filter_graphql_menu_item_connection_args(array $queryArgs, $resolver)
{
if (!is_a($resolver, '\WPGraphQL\Data\Connection\MenuItemConnectionResolver')) {
return $queryArgs;
}

$args = $resolver->getArgs();

if (!isset($args['where']['language'])) {
return $args;
return $queryArgs;
}

if (!isset($args['where']['location'])) {
return $args;
return $queryArgs;
}

// Required only when using other than the default language because the
// menu location for the default language is the original location
if (pll_default_language('slug') !== $args['where']['language']) {
$args['where']['location'] = self::translate_menu_location(
$args['where']['location'],
$args['where']['language']
);
}
$menuLocations = get_theme_mod('nav_menu_locations', []);
$locations = array_unique(array_values($menuLocations));
$location = self::translate_menu_location($args['where']['location'], $args['where']['language']);

// If the location argument is set, set the argument to the input argument
if (isset($menuLocations[$location])) {
$locations = [$menuLocations[$location]];
}

unset($args['where']['language']);
$queryArgs['tax_query'] = [
[
[
'taxonomy' => 'nav_menu',
'field' => 'term_id',
'terms' => $locations,
'include_children' => false,
'operator' => 'IN',
],
],
];
}

return $args;
return $queryArgs;
}

/**
Expand Down Expand Up @@ -87,7 +106,7 @@ function __action_graphql_register_types()
{
register_graphql_fields('RootQueryToMenuItemConnectionWhereArgs', [
'language' => [
'type' => 'LanguageCodeFilterEnum',
'type' => 'LanguageCodeFilterEnum',
'description' => '',
],
]);
Expand Down

0 comments on commit 1cc7832

Please sign in to comment.