carbon-fields
carbon-fields copied to clipboard
Theme options + Polylang + Association Field
Version
- Carbon Fields: 3.1.10
- WordPress: 5.3.2
- PHP: 7.3
Behavior
I should be able to save data in the association field.
If i add a page to the association it gets saved in the database.
On fetch it gets returned in the "preload json" (wp_add_inline_script( 'carbon-fields-vendor', sprintf( 'window.cf.preloaded ...
) correctly.
On render the same problem as https://github.com/htmlburger/carbon-fields/issues/682 happens, which means that I get an empty association, but the item is there (I can't add any more items).
On save, the association is emptied.
Container definition
Field::make('association', 'crb_master_page_list' . carbon_lang(), 'List')
->set_types(
[
[
'type' => 'post',
],
]
)
->set_max(1)
->set_min(1),
function carbon_lang() {
$suffix = '';
if (! defined('ICL_LANGUAGE_CODE')) {
return $suffix;
}
$suffix = '_' . ICL_LANGUAGE_CODE;
return $suffix;
}
Steps to Reproduce the Problem
- Create an association with carbon_lang in its key in the theme options
- Add a page to the association
- Association is correctly saved in the db
- On next render association is empty, but I can't add any more items
- If i click save, association becomes empty
Comments
The problem is similar to https://github.com/htmlburger/carbon-fields/issues/682
If i use the same association without carbon_lang it works correctly.
Example (the "add item" icon is not shown, but the right side is empty):
My understanding is that javascript cant bind the correct value (as the value is correctly inserted in the html via wp_add_inline_script( 'carbon-fields-vendor', sprintf( 'window.cf.preloaded ...
. I can't find any way to debug where javascript is binding the json values to the box
Update: problem is this request:
<WP-INSTALLATION>/wp-json/carbon-fields/v1/association/?container_id=carbon_fields_container_theme_options&options=68%3Apost%3Apage&field_id=crb_master_page_list_it
This checks the value for a field called crb_master_page_list_it
, but when function carbon_lang() {
is run, suffix is set to _all, so there's only a field called crb_master_page_list_all
instead of crb_master_page_list_it
.
I solved with a brutal hack this way:
function carbon_lang() {
$suffix = '';
if (! defined('ICL_LANGUAGE_CODE')) {
return $suffix;
}
if (isset($_GET['container_id'], $_GET['field_id']) && $_GET['container_id'] === 'carbon_fields_container_theme_options'
&& (endsWith($_GET['field_id'], '_it') || endsWith($_GET['field_id'], '_en'))) {
if (endsWith($_GET['field_id'], '_it')) {
$suffix = '_it';
}
if (endsWith($_GET['field_id'], '_en')) {
$suffix = '_en';
}
} else {
$suffix = '_' . ICL_LANGUAGE_CODE;
}
return $suffix;
}
function endsWith($haystack, $needle) {
return substr_compare($haystack, $needle, -strlen($needle)) === 0;
}
This is bad on multiple levels, but "works"
Has anyone solved this problem?