Hello,
I'm working on some updates for the Juicebox HTML5 Responsive Image Galleries module (which I maintain) and have a targeted question about the proper use of hook_update_n when re-structuring existing user configuration data that's been populated through the fields and views APIs.
This integration module implements a field formatter and view display plugin that allows imagefield data to be presented with a popular 3rd party javascript library (Juicebox). It implements custom field formatter and views display settings through the fields and views APIs, and therefore does not directly manage the way this settings data is dealt with in the database. From what I can see, both fields and views just store custom formatter and display data in a "schemaless" structure as serialized data arrays (inside the field_config_instance and views_display tables). Furthermore, the structure/hierarchy of this serialized data directly mirrors that of any fieldsets used on the configuration form itself (unfortunately it seems '#tree' = TRUE must be enforced on the form fieldsets).
One of my updates changes the way some configuration options are nested in fieldsets on the configuration forms, which therefore impacts the way this data is structured in the serialized data arrays noted above. So in order to support existing user data during the update it's looking like I need to leverage hook_update_n in such a way that directly changes these serialized data arrays but without using any fields or views APIs (as that seems to be very bad practice within hook_update_n). I'm slightly nervous about this though as those data structures are core fields and views concepts (not concepts of my module) and if I do something wrong I could inadvertently damage entire field instances or view displays for hundreds of my users.
So as a sanity check, I'm hoping someone could help me confirm if I'm approaching this the right way. I can share some example code that I'm testing, but my strategy is:
- Fetch a bunch of rows that contain the individual field formatter or view display data directly with db_query (inside the field_config_instance or views_display tables).
- Loop though these rows and unserialize the info in the "data" column (that may contain my module's config settings).
- Loop though the unseralized data and search for indicators that this is a field formatter or views display that my module handles. If I find cases where my module is involved, then:
- Restructure the data array to align with the the new nested fieldset structure that will be used on the configuration form itself.
- Serialize the new data array.
- Write the updated seralized data back to the database with db_update.
Does this all seem correct, or is there necessary logic that I might be missing?
Any info and guidance is hugely appreciated!
Comments
Example code
Here's an example chunk of hook_update_n code that I've been testing to specifically update the Juicebox view displays (the code that deals with the field formatters is fairly similar). Note that _juicebox_update_7100_process_settings() is the function that actually changes the settings structure (just re-nests some parts of the settings array).
I just wanted to note that I
I just wanted to note that I also posted a similar question to this one in the "views" issue queues and got some great responses from merlinofchaos there. See:
#1965786: Methods to alter display data structures when views integration modules are updates (hook_update_n vs runtime checks, etc.)
Just in case anyone comes across this and is interested in the outcome.