Support Forum
Hi there
I'm using MB for the first time but did some digging in its capabilities so far I like it a lot!
I have the following problem:
I created a custom field "ploegen". It's a select that gets data via the REST API from an external source:
<?php
if ( class_exists( 'RWMB_Field' ) ) {
class RWMB_Ploeg_Field extends RWMB_Field {
public static function html( $meta, $field ) {
$club_key = rwmb_meta( 'club-key', ['object_type' => 'setting'], 'PSD Integrate' );
$api_key = rwmb_meta( 'api-key', ['object_type' => 'setting'], 'PSD Integrate' );
$api_secret = rwmb_meta( 'api-secret' , ['object_type' => 'setting'], 'PSD Integrate' );
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://clubapi.prosoccerdata.com/teams/all",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: $api_secret",
"x-api-club: $club_key",
"x-api-key: $api_key"
),
)
);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$response = '{"content":' . $response . '}';
}
$data = json_decode($response);
$dropdown = '<select name="Ploegkeuze" id="Ploegkeuze">';
foreach ($data->content as $i=>$value) {
$dropdown .= '<option value="' . $value->id . '">' . $value->id . ' ' . $value->name . '</option>';
//echo $value->name . '</BR>';
}
$dropdown .= '</select>';
//return sprintf($dropdown, $field['Ploeg'], $field['ploegid'], $meta);
return sprintf($dropdown, $field['field_name'], $field['id'], $meta);
}
}
}
?>
I have added this field to my Gutenberg block:
add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
$meta_boxes[] = [
'title' => 'Wedstrijdkalender',
'id' => 'wedstrijdkalender-inhoud',
'description' => 'Dit blok geeft de komende 10 wedstrijden weer voor de geselecteerde ploeg.',
'type' => 'block',
'icon' => 'fas fa-futbol',
'category' => 'layout',
'context' => 'side',
//'render_template' => get_template_directory() . '/page-templates/kalender_block.php',
'render_callback' => 'toon_kalender_block',
'enqueue_style' => get_template_directory_uri() . '/blocks/hero/style.css',
'supports' => [
'align' => ['wide', 'full'],
'customClassName' => true,
],
// Block fields.
'fields' => [
[
'type' => 'ploeg',
'id' => 'ploegid',
'name' => 'Ploeg',
],
[
'type' => 'text',
'id' => 'hans',
'name' => 'Hanzo',
],
],
];
return $meta_boxes;
} );
but in the callback function, I can't read the field value (needed to get more external data via REST API:
$ploegid = mb_get_block_field('ploegid');
Due to this, the curl query can't be executed an no data is retrieved.
I hope anyone here can shine some light on this issue...
It appears my custom field isn't saved in the block:
<!-- wp:meta-box/wedstrijdkalender-inhoud {"id":"mb-block-aed81d9a-f2b7-4e60-8bc8-4aa917b68c70","data":{"hans":"TESTJE"}} /-->
Only a regular text field is saved. Tried with a custom html block but that's not saved in there either.
When I add a "normal" select, it is saved in the block:
[
'name' => 'Select Advanced',
'id' => 'select_advanced',
'type' => 'select_advanced',
// Array of 'value' => 'Label' pairs
'options' => array(
'java' => 'Java',
'javascript' => 'JavaScript',
'php' => 'PHP',
'csharp' => 'C#',
'objectivec' => 'Objective-C',
'kotlin' => 'Kotlin',
'swift' => 'Swift',
),
// Allow to select multiple value?
'multiple' => false,
// Placeholder text
'placeholder' => 'Select an Item',
],
Saved block:
<!-- wp:meta-box/wedstrijdkalender-inhoud {"id":"mb-block-bf398a9f-2c92-4b22-b00a-6678cac348ca","data":{"hans":"TESTJE","select_advanced":"csharp"}} /-->
OK, perhaps it's still a bug what happened but I got things sorted out...
Instead of creating an entirely new field type, I just created a regular select, filled it dynamically with the JSON data and now all is good!