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...