Porchy Web Development

Using custom tables with CMB2

This is more straightforward than I expected (yay thank you @jtsternberg). The short of it is to add a filter to cmb2_override_meta_value or cmb2_override_{$a['field_id']}_meta_value to get the data and cmb2_override_meta_save to save it. But what if I have a repeatable group huh? What then?

Getting the values

To get the values, it’s not necessary to return every individual field itself, I just need to return the group values in the proper format. What’s the proper format you ask? My process for this was:

  1. Set up the metaboxes without the filter.
  2. Put some stuff in them and save the post.
  3. Look at the get_data function in cmb2/includes/CMB2_Field.php. See those two filters there? They are what will be used to return the data early before going through the usual steps.
  4. Now look at the if statement directly after them, `if ( ‘cmb2_field_no_override_val’ === $data )` – this is what will be skipped when a custom value is returned.
  5. Look at the value of $data after it’s been assigned in the if statement. That’s what the value returned from the function which filters cmb2_override_meta_value needs to look like.
  6. I could’ve also taken a peek a the entry on the postmeta table but I find it easier to parse looking at the variables.

Now an example. It’s in a gist which has all the stuff in it but doesn’t do much. I’m going to continue on.

Saving the values

Saving the values is also handled in cmb2/includes/CMB2_Field.php – it’s the appropriately named update_data function as it uses update_metadata to create and update. I’m not so interested in update_metadata right now though.

The value comes in the same format as above which makes it pretty easy for me to think about, phew. But, BUT how am I going to know if one method has been deleted? The thing about CMB2 is that it saves repeatable group fields as a serialized array in the group id metakey so it’s not an issue.

I think there are a few ways of dealing with this. Because I’m ok with one extra query, I’m simply going to delete all of the previous methods then add back in the ones I have. Oh this is so churny, let me have another look. So with a repeatable group, all I seem to have available is the update_data filter. Churn it is. No wait, there has to be a way, c’mon. Ooooh are there hidden fields in CMB2 anywhere? LET’S INVESTIGATE.

Why yes, yes there are. Good-o. I can add in the value of the “ID” column in my table to the hidden field and then I’ll be able to compare what’s in my table and what’s in my edit page and update the ones that are still there and delete the ones which are not. Let me try writing that.

No, that built in field doesn’t work but hey, it’s easy enough to write my own. Now that that’s done, I can tell the ID of each method’s row and update or delete accordingly.

Once I do that, I need to return something other than null to tell update_data that I’ve already updated / inserted / deleted and it can just stop now.

And that’s about it! I mean, I’m done. Here’s the gist again for illustrative purposes only.


Join the newsletter

Subscribe to get the latest content by email.

Powered by ConvertKit. If you give me your email address, you may receive emails from me (JJ) about posts on this site. You can unsubscribe at any time.

Porchy Ltd is a company registered in England, no. 12035925

VAT Registration no. 331196421

Built with WPGraphQL and Gatsby

© 2019 - 2021