Dynamically change which fields are seen
-
Is there a way, with the override meta value call (the call that the fields are being stuffed with values) that I can disable whether a field is displayed? I was looking through the tips and tricks and I need some kind of functionality now for this.
Suggestions?
-
I basically want to switch the field to hidden, based on the type of data record.
Something like https://github.com/jcchavezs/cmb2-conditionals ?
I was looking at that earlier. That is conditionally saving data and modifying the attributes. I want to change whether a field is hidden or not.
Right now I am wading through a mountain of protected arrays looking for the field type, so I can switch it on the fly (based on $cmb2_field_object ). Any reason why that would not work?
Last i knew and checked, that plugin allowed for conditionally showing fields based on the conditions of a different field. I know I’ve used it for a client project to do just that.
Perhaps I am missing something. Can you tell me where in the code you see that feature?
I did get access to the data field type. But changing it, is not doing anything at the moment.$array = $cmb2_field_object->args; $metabox = $array['render_row_cb'][0]->meta_box; $MMDListsRecord = $metabox['fields']['MMDListsRecord']; $fields = $MMDListsRecord['fields']; $fields['bizdesc']['type'] = 'hidden';Honestly the best thing to do is just try things out after installing it and tinkering with the example fields
https://github.com/jcchavezs/cmb2-conditionals/blob/master/example-functions.php
I just did so myself and had some of the example fields only showing visually if a different field has a value at all.
From them:
CMB2 Conditionals is a plugin for CMB2 which allows developers to relate fields so one of them could only appear when one other have an specific value or when is not empty.
Here is the solution, for anyone using their own custom table of data and using that data to determine what fields should be displayed.
Add the following to your field definition. Does not matter if it is a group field or single.
'show_on_cb' => 'YourPrefix_set_field_data_attr',then customized the call back to your particular conditions:
function YourPrefix_set_field_data_attr($field) { $Fieldid = $field->args( 'id' ); $CMB2Fieldname = $field->args( '_name' ); $Prefix = 'YourPrefix'; if($Prefix == $Fieldid) // Whole table Id, skip it return 1; $SearchPrefix = 'YourPrefix' . '_'; $pieces = explode($SearchPrefix, $Fieldid); $pieces = explode('_', $pieces[1]); $RowId = $pieces[0]; $CMB2Leader = $SearchPrefix . $RowId . '_'; $pieces = explode($CMB2Leader, $Fieldid); $StandardFieldName = $pieces[1]; // This should match the fields table name // These are my conditions, use your own conditions. $current_user = wp_get_current_user(); $DatabaseData = FindListing($current_user->user_email); if($DatabaseData[$RowId]['SubscriptionName'] == "YOUR DATABASE KEY") { // Get the input fields $input_fields = GetTemplateFields(1); // Table of fields for($i=0; $i<sizeof($input_fields); $i++) { $Field = $input_fields[$i]; // CMB2 Field Name without prefix $FieldId = $Field['id']; $bDisplayText = $Field['textdisplay']; if($FieldId == $StandardFieldName && $bDisplayText == 0) { unset($DatabaseData); return 0; // Turn off the field } } } unset($DatabaseData); return 1; // Display the field } <code></code>definitely one way and the
show_on_cbparameter is meant for things like this as well. I still stand by the library i linked to as that doesn’t require saving and refreshing to have the fields hidden. it’s more on the fly.Regardless, you got something working for you, and I’m happy to hear that.
Thank you for your help. Much appreciated. It works wonderfully, with no saving or refreshing..that is what I like about it. The only piece of code that “blinks” ironically, is the Jquery code to rename the rows. But I can live with that.
Thanks again.
…although you method would work very well if used as the user enters data.
The topic ‘Dynamically change which fields are seen’ is closed to new replies.