Skip to content

Add a language filter for ACF options pages #42

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# JetBrains IDE files
.idea/
61 changes: 61 additions & 0 deletions acf-compatibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

use WPGraphQL\Registry\TypeRegistry;
use WPGraphQL\Utils\Utils;

/**
* Registers a new "wpmlLanguage" input field on ACF options pages queries, and
* uses translated options page content based on the language code supplied
* @throws Exception
*/
function wpgraphqlwpml_action_add_options_pages_language_filter(TypeRegistry $type_registry)
{
foreach (acf_get_options_pages() as $options_page) {
// Check if the option page should be shown in GraphQL schema
if (!isset($options_page['show_in_graphql']) || false === (bool)$options_page['show_in_graphql']) {
continue;
}
$type_name = Utils::format_type_name($options_page['graphql_field_name'] ?? $options_page['menu_slug']);
// Register new options page field with the wpmlLanguage argument
$options_page['type'] = 'options_page';
$type_registry->register_field(
'RootQuery',
Utils::format_field_name($type_name),
[
'type' => $type_name,
'args' => [
'wpmlLanguage' => [
'type' => 'String',
'description' => 'Filter by WPML language code',
],
],
'description' => sprintf(__('%s options.', 'wp-graphql-acf'), $options_page['page_title']),
'resolve' => function ($unused, $args) use ($options_page) {
// If the wpmlLanguage argument exists in the arguments
if (isset($args['wpmlLanguage'])) {
$lang = $args['wpmlLanguage'];
global $sitepress;
// If WPML is installed
if ($sitepress) {
// Switch the current locale WPML
$sitepress->switch_lang($lang);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldnt we switch the locale back to what it was before after this resolver? Otherwise this will cause resolvers further down in the call chain to behave different, no?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something that seems to be happening, yes. I'm using it to my advantage by following it up with a query that has no language filter, but it would be better to reset the language to the default. Or maybe even better, save what it was before and set that back.

// Override ACF language explicitly, otherwise the output language doesn't change
acf_update_setting('current_language', $lang);
}
}
return !empty($options_page) ? $options_page : null;
}
]
);
}
}

function wpgraphqlwpml_init_acf()
{
add_action(
'graphql_register_types',
'wpgraphqlwpml_action_add_options_pages_language_filter',
10,
1
);
}
Loading