• Resolved dkurth

    (@dkurth)


    I have data stored in a different table that I retrieve, but how can I insert my stored values from the database. I do not use the postmeta for a variety of reason. I need to load all the fields, including the repeatable, with my independently stored data.
    Nice job overall.

    UPDATE, NEVER MIND. Found your Callback feature.

    • This topic was modified 5 years, 1 month ago by dkurth.
Viewing 9 replies - 16 through 24 (of 24 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Glad to see you got a working resolution for yourself, and hopefully others who may be in similar situations.

    I’m partially tempted to come up with a scenario myself that may or may not need such a thing, but first i need the idea 😀 Sometimes the best documented things come from real life support requests.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    You do not need to put the metagroup name in the function call or the callback type. Direct works just fine for storing fields and retrieving them.

    While this is true, what you’ll find is that you’re going to break every other cmb2 field, as this runs on EVERY field. You may not notice it now, but when you add other fields, or install a plugin which bundles/uses CMB2, you’ll find yourself banging your head because none of the fields are working as you expect. It’s always best to use the filter with the field id in it, as it limits the effects to JUST that field. If you’re going to use the generic filter, then you need to do some inline logic checks to make sure you’re not messing with the output of other fields.

    Thread Starter dkurth

    (@dkurth)

    Justin, I noticed that the callback being invoked for draw is called multiple times (every field plus 1). Why is that? Should it not be called only once, like the save call back??

    You refer to in-line logic. What exactly do you test on and for? There is nothing but values. So far, I have seen all the fields return as expected, with the test data.
    Can you elaborate what you were talking about?

    • This reply was modified 5 years, 1 month ago by dkurth.
    Plugin Author Justin Sternberg

    (@jtsternberg)

    It’s called every time the value is requested, which is for each entry in the repeatable group, + an empty hidden entry which is used as the JS template.

    re: inline logic, you can test on any number of things, the most common of which is the field id, which is why the field-id specific filter exists.

    So,

    add_filter( 'cmb2_override_yourprefix_group_alt_data_demo_meta_value', 'yourprefix_group_alt_data_demo_override_meta_value', 10, 4 );
    function yourprefix_group_alt_data_demo_override_meta_value( $data, $object_id, $args, $field ) {
    	return your_function_to_get_your_data();
    }
    

    Is the safer/shorter equivalent to:

    
    add_filter( 'cmb2_override_meta_value', 'yourprefix_group_alt_data_demo_override_meta_value', 10, 4 );
    function yourprefix_group_alt_data_demo_override_meta_value( $data, $object_id, $args, $field ) {
    	if ( 'yourprefix_group_alt_data_demo' === $args['field_id'] ) {
    		$data = your_function_to_get_your_data();
    	}
    
    	return $data;
    }
    
    Thread Starter dkurth

    (@dkurth)

    Justin, That makes sense given the number of times the function is called. However, I see your code sample, yet it is not specific to which field it is being “called for” during each function call. Can you elaborate on the specific items that one could test for?

    What I have observed is that if I push the data into the array, using test data, they display correctly. So I am unsure what I am to test for in a field-fill process.

    In a pull from field, I would understand.

    Thread Starter dkurth

    (@dkurth)

    Justin..addendum..
    I am not using JS to pull data from a database. Straight PHP on a single MySQL select search, where all the records in question are immediately available for insert into the fields. At this writing, fields are not empty and when I read them back, all the data is there…using the generic call.

    What is annoying is the multiple calls, which I am working on a way to “block” and only access the database once instead of nth number of times.

    So tell me what I don’t know…all ears.

    Thread Starter dkurth

    (@dkurth)

    As an example. This is my test code. Nothing is skipping:

    add_filter('cmb2_override_meta_value', 'mmd_get_methods_custom_data', 10, 4);
    function mmd_get_methods_custom_data( $dont_override, $object_id, $args, $cmb2_field_object )
    {
    $Records = array();  
    $Record[0] =  array( 'title' => 'This is a method for the US and AUS',
    	                     'SubsName' => '',
    						 'address' => 'TEST ADDRESS',
    						 'city'=> 'testing city',
    						 'postcode'=> '90065',
    						 'latitude'=> '001.01',
    						 'longitude'=> '001.01',
    						 'phone'=> '+1-818-919-2533',
    						 'email'=> '1@1.com',
    						 'payername'=> '1',
    						 'useraccid'=> '1',
    						 'paymentdate'=> '1997-10-21',
    						 'transactionid'=> '000',
    						 'link'=> 'linkedin1.com',
    						 'facebook'=> 'facebook1.com',
    						 'twitter'=> 'twitter1',
    						 'instagram'=> 'instagram1.com',
    						 'googleplus'=> 'googleplus1.com',
    						 'linkedin'=> 'linkedin1.com',
    						 'yelp'=> 'yelp1.com',
    						 'bizdesc'=> 'business descript 1',
    						 
    						  );
    $Record[1] =  array( 'title' => 'TITLE 2',
    	                     'SubsName' => '',
    						 'address' => 'TEST ADDRESS 2',
    						 'city'=> 'testing city 2',
    						 'postcode'=> '90065-2', 
    						 'latitude'=> '002.02',
    						 'longitude'=> '002.02',
    						 'phone'=> '+2-818-919-2533',
    						 'email'=> '2@2.com',
    						 'payername'=> '2',
    						 'useraccid'=> '2',
    						 'paymentdate'=> '1998-10-21',
    						 'transactionid'=> '002',
    						 'link'=> 'linkedin2.com',
    						 'facebook'=> 'facebook2.com',
    						 'twitter'=> 'twitter2',
    						 'instagram'=> 'instagram2.com',
    						 'googleplus'=> 'googleplus2.com',
    						 'linkedin'=> 'linkedin2.com',
    						 'yelp'=> 'yelp2.com',
    						 'bizdesc'=> 'business descript 2',						 
    						 );
    
    $Records[] = $Record[0];
    $Records[] = $Record[1];
    
    	
    return $Records;		
    }
    
    Thread Starter dkurth

    (@dkurth)

    Michael – as I promised you, here is a subset of the code that is working beautiful using independent tables and direct storage methods.

    I create a demo file for those who want to see how to do this process of independent table data and how to get around issues. Although, for security reasons, I removed a piece of the code that shows how not to hit the database over and over again on the data upload to fields process (cmb2_override_meta_save) and changed many of the variables names. It is for demonstration purposes anyway.

    Thanks for your assistance.

    https://gist.github.com/dskurth/d56744de4ae076c417f59b2a459eaf4b

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome.

Viewing 9 replies - 16 through 24 (of 24 total)
  • The topic ‘Set Field values from database with add_group_field’ is closed to new replies.