Add Dropdown select of saved data using functions.php
Support › MB Builder › Add Dropdown select of saved data using functions.phpResolved
- This topic has 6 replies, 2 voices, and was last updated 1 year, 2 months ago by
leebazCDS.
-
AuthorPosts
-
January 18, 2024 at 7:38 PM #44334
leebazCDS
ParticipantHi,
I'm familiar with creating custom meta boxes using php, but for a recent project I've been using the builder, and it's great!
However, I would like to add a dropdown to a clonable field, top be populated from saved information in another custom field ... and I'm struggling. Any pointers?So I have:
Field group: id= 'maintenance-and-faults'
Inside that a clonal group, id: fault-reportingI want to allow the user to select an item of faulty equipment which is in another field group which contains a clonable list of equipment.
Field group: 'instument-details'
Clonable group: 'group_j7gfnvh6y4t'
Field: id 'serial_no'I'm pretty sure I could generate that code in functions.php but how can I hook it into the editor to add it to the bottom of the field group?
January 18, 2024 at 10:37 PM #44340leebazCDS
ParticipantI'm trying a different tack using the Select Advanced field and a callback function.
This is my function, but it's not populating the dropdown yet...function populate_my_select_field() { global $post; $options = []; // Check if we are in the admin area and a post object exists if (is_admin() && !empty($post)) { $post_id = $post->ID; // Fetch the meta values from the specified group for the current post $groups = rwmb_meta('group_j7gfnvh6y4t', '', $post_id); foreach ($groups as $group) { // Assuming 'height' and 'serial_no' are keys in your group $height = $group['height'] ?? ''; $serial_no = $group['serial_no'] ?? ''; if ($height && $serial_no) { $key = $serial_no; // Using serial_no as the value $options[$key] = "Height: $height, Serial No: $serial_no"; } } } return $options; }
January 19, 2024 at 12:02 AM #44349leebazCDS
Participantfound my way through this, in case anyone else needs it:
function populate_my_select_field() { //global $post; $options = []; $post_id = null; // Check if we are in the admin area and a post object exists if (is_admin()) { $post_id = $_GET['post'] ?? get_the_ID(); error_log('Current post ID: ' . $post_id); // Fetch the meta values from the specified group for the current post $groups = rwmb_meta('group_j7gfnvh6y4t', '', $post_id); error_log(print_r($groups, true)); foreach ($groups as $group) { // Assuming 'height' and 'serial_no' are keys in your group $height = $group['height'] ?? ''; $serial_no = $group['serial_no'] ?? ''; if ($height && $serial_no) { $key = $serial_no; // Using serial_no as the value $options[$key] = "Height: $height, Serial No: $serial_no"; } } } return $options; }
January 19, 2024 at 5:49 PM #44353leebazCDS
ParticipantWhile I can see my options in the select dropdown, the chosen item is not saved when the post is updated.
I tested with this simplified version:
function populate_my_select_field() { return [ 'option1' => 'Option 1', 'option2' => 'Option 2', ]; }
But when I populate using fields from the post, the select box is refreshed each time. How do I get it to commit? One way would be to check through the entries each time in the function and compare to the current value of the dropdown... but is there a more efficient way?
function populate_my_select_field() { return [ 'option1' => 'Option 1', 'option2' => 'Option 2', ]; }
And the dynamically populated version:
function populate_my_select_field() { $options = []; $post_id = null; if (is_admin()) { $post_id = $_GET['post'] ?? get_the_ID(); } if ($post_id) { // Define the groups and their corresponding prefixes $group_ids = [ 'group_j7gfnvh6y4t' => 'Anem', // Anemometers 'vanes' => 'Vane', // Wind Vanes 'miscelaneou' => '' // Miscellaneous ]; foreach ($group_ids as $group_id => $prefix) { $groups = rwmb_meta($group_id, '', $post_id); // Check if $groups is an array before iterating if (is_array($groups)) { foreach ($groups as $group) { $height = $group['height'] ?? ''; $serial_no = isset($group['serial_no']) ? trim($group['serial_no']) : ''; // Trim the serial number $type = $group_id === 'miscelaneou' ? ($group['type'] ?? 'Unknown') : ''; if ($height && $serial_no) { // Construct a unique key and label for the option //$key = $group_id . '_' . $serial_no; //$label = ($prefix ?: $type) . " " . $height . "m, Serial No ". $serial_no; $key = $serial_no; $label = $serial_no; $options[$key] = $label; } } } } } return $options; }
January 23, 2024 at 10:59 PM #44371leebazCDS
ParticipantCan anyone help me with getting the dynamically populated values to save?
January 24, 2024 at 9:22 PM #44378Peter
ModeratorHello,
If you use the simplified version
function populate_my_select_field() { return [ 'option1' => 'Option 1', 'option2' => 'Option 2', ]; }
then the field still doesn't save value. Please check if the setting
Save field value
is enabled, screenshot https://imgur.com/rdIYYyyJanuary 24, 2024 at 11:10 PM #44379leebazCDS
ParticipantHi,
I've tested with the simplified version and it works as expected; the values are saved and I'm able to retrieve them on the front end.
Lee
-
AuthorPosts
- You must be logged in to reply to this topic.