What I need help with is for the first scenario, in my MIGX CMP formtab I can easily either hardcode in ALL the options that can be in that field… i.e.:
I can even see how I could write a snippet that scours all the data in the table and builds the input options dynamically.
In this case however what I don’t get is how I can have MIGX parse the column data and check the matching checkboxes. Please point me to some info on if this is doable.
The second scenario with the filenames… I’m less concerned about this as I don’t see these actually needing to be editable (they are images a user uploaded with their review) HOWEVER… for posterity, I really would like to see if there is a way to have them display in a sub-grid… so that perhaps a thumbnail could be shown and a delete option for individual images could be implemented.
Appreciate the help anyone can provide. I always find MIGX to be an absolutely amazing extra, but a bit of a dark art with no definitive source of complete documentation.
You have to convert your data into the format that MIGX/MODX can process. For an “Input TV type” of checkbox this is option1||option7||option9 instead of ["option1","option7","option9"].
There are two ways to do this: Either you use “aftergetfields” and “beforesave” hook snippets or custom processors.
In the “aftergetfields” hook you would convert your custom format ["option1","option7","option9"] to option1||option7||option9. In the “beforesave” you convert it back (before it gets saved to the database).
If you decide to use custom processors, you do the same conversion, but directly in the “fields” and “update” processors.
You can create nested grids, if you use “Input TV type” = migx.
In your case you most likely have to use the same hook-snippets or custom processors to (temporarily) convert your data ["66cc999949be2.jpg", "66cc999979d6b.jpg"] into a proper MIGX format [{"MIGX_id":"1","img":"66cc999949be2.jpg"},{"MIGX_id":"2","img":"66cc999979d6b.jpg"}] .
Awesome. As always @halftrainedharry you are a wizard and I appreciate your help.
I’ll google hook snippets and custom processors. If you know of any canonical sources of info for these, I’d be grateful to have them here ont he thread.
There is an example for a custom processor in this Youtube video (first 4 minutes).
Hook snippets are shown in this video (for a many-to-many relationship). It uses an “aftersave” hook snippet, but the “beforesave” hook should be similar.
@halftrainedharry Is there a basic example anywhere of the simplest of beforesave hook snippet examples?
I mean literally just something that changes the contents of a field to lowercase?
I have been bashing my head against the wall for hours trying to get this to work and there is zero documentation on this anywhere. How did you learn the methods? I can’t even find a list of the hook snippet events available
I think after scouring for info, that “aftersave” snippet use here is impossible.
According to this it implies the posted form values are NOT accessible by beforesave - which I find a bit odd… isn’t this EXACTLY where you would want to be able to access these values??
So… changing the submission values before saving is not possible here it seems.
Ok, so here is an example for the “beforesave” hook:
(It assumes that the field for your options has the name “myoptions”.)
<?php
$properties = &$scriptProperties['scriptProperties'];
$data = $properties['data']; // All form fields are in the property "data" as JSON
$data_obj = $modx->fromJson($data);
$options = $data_obj['myoptions']; // Read the value of the field "myoptions"
$options = json_encode($options); // Do something with the value. (Here we just convert the options-array to JSON.)
$data_obj['myoptions'] = $options; // Save the changed value of the field "myoptions"
$properties['data'] = $modx->toJson($data_obj); // Convert everything back to JSON
return ''; // return empty string (= no error)
The field values are available, but everything is still encoded as JSON in the property “data”. That makes it a bit cumbersome to handle, as you have to decode everything first, make the change and the encode it again.
If you want direct access to the field value, use a custom processor instead. Copy “update.php” and change the field value, before the item is saved here:
Usually, I just look at the code.
In the update processor (“update.php”), there are 3 hooks:
beforesave
aftersave
validate
In the fields processor (“fields.php”), only “aftergetfields”.
There are other hooks in the code (like e.g. “beforecreateform” or “getcustomconfigs”), but I don’t think I have ever used them (and I don’t know what they do exactly).
I am so confused. I got your example to work… I don’t understand why then the post above on Git suggests that the post values are NOT available to beforesave??
EDIT: I understand now as you posted the explanation above while I was writing this response. Thankyou
I had scoured the every single thing in the scriptProperties and I could find NO submission data in there double-pipe delimited. As I was looking for that to convert back to JSON I was hitting a dead end.
for the record I understand now why it would not be like that in there… you ever get to that point on something where your brain is just stuck in a bad loop? Yup!
I did end up getting this to work via an aftersave hook by loading the record by id, changing and saving via xpdo, but that seems bad juju doing it after a save from migx. This way is better for sure.
The code below works - thanks for the shove in the right direction:
@halftrainedharry I’m really sorry to bother you again, but I am hanging now again on the second half of my ask.
Your guidance on transforming the data for aftergetfields and beforesave I have rocking and have successfully transformed the image data into the proper migx format - even have the grid displaying.
My issue now is I cannot seem to get the data inside my transformed field data to display in the grid. It shows the matching number of rows for how many images are in a record, so I know it’s recieving the data, however I cannot get a config json to work. Any configs I attempt cause the formtab to fail to load. What you see below is with NO config present:
The “TEST” field you see I put in for debug purposes and have copied the images json into to confirm the format.
I’ve tried full migx configs… partial configs with just columns data. No avail. Just 500 errors thrown in connector.php on form load. Log says:
PHP Fatal error: Uncaught TypeError: json_decode(): Argument #1 ($json) must be of type string, array given in H:\2408\modx-core\xpdo\xpdo.class.php:2443
Any guidance on what the config should be for that data?
This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.