Skip Navigation

[Resolved] Toolset edit form on submit count how many times has been changed.

This support ticket is created 5 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 30 replies, has 2 voices.

Last updated by stuartm-2 5 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#1338777
job view.png

I made the changes you commented but i guess im doing something wrong, the amount of files appreared on each child post of the view is the same with files uploaded by the form that adds the files, i guess because its an edit form and needs to be create childs.

Do i need to make some specific changes to relationships?
What i need is for each choice made Approve or Reject add a record on that view with that related file that was reviewed.

Many thanks for your help!

#1338877

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, one thing I notice is that you can have multiple files, not a single file. We can come back to that when we've confirmed the workflow and what's expected.

So, you have a form to publish the parent post which includes a file attachment.

You have a form to set the status, which creates a child post (and custom code copies the status back to the parent post).

At the moment you create this child post to record the status, the parent post exists and has the file field set.

So that seems the right moment to copy the file field from the parent to the child.

Now suppose an edit form is used to update the parent post and change the file field (to submit revised files).

So the parent post is updated and holds a different value for the file field than the child post.

The form to create a child post (recording an update of the status) is used.

The same code that runs when creating the first child post runs when creating this second child post, so that we then have two child posts, the first of which has the initial file field value, the second of which has the updated file field value (which will match the file field on the parent, currently).

That sounds like the workflow I expect, and what the code is intended for (ignoring multiple files, for now).

In which case the data would be correct, and you may have questions about presenting it.

So if you have a "log" View which displays the child posts and the statuses set at the time, it would also show the file field values belonging to the child posts which show the revisions to the files.

How am I doing?

#1338951

Seems like you nailed it.
Thats the workflow im aiming for.
A form creates the job --> a form inside the job adds files --> a reviewer approves or rejects --> a view logs the files & decisions made.

"Now suppose an edit form is used to update the parent post and change the file field (to submit revised files). "
So you are suggesting there should be another form that updates the parent field?

"The same code that runs when creating the first child post runs when creating this second child post, so that we then have two child posts" Which form will create the second child post?

#1339527

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Reading that, it sounds like

- you have a form to publish the initial job post
- you have an edit form to edit this job post which adds the files that belong to the job post, and presumably can be used again to change the file fields

That actually doesn't make much difference to what's required, because it is the form creating the child post that is the trigger for the custom code required.

Which form will create the second child post?

OK, now I'm slightly worried we are not understanding each other after all.

The whole logging of changes in status works like this, doesn't it?

The job posts are listed (using a View) and include a link to change the status which is actually a form to publish a child post. Every time the status is changed (by clicking that link) a new child post is created, using the same form. So if the status gets updated 3 times there will be 3 child posts. A separate View that displays those child posts is showing the log of changes.

So, to answer the question "which form will create the second child post", the answer is the same form that creates the first child post.

I thought that you already had this working, and we just needed to add the step whereby the child posts which form the log of changes include the version of the file field which existed at the time the status was updated...

#1340779

Hello and apologies for the misunderstanding, yes you are right and at the moment its already working the way you advised.
A form creates child reviews and the status field is updated based on your code.
Just trying to figure out how to add the file field to each child post created by the form.

This is the second code you provided me and i adjusted the fields.
My question is for every Review child form submitted the form that adds files should be unique correct?


add_action('cred_save_data', 'tssupp_form_submit', 10, 2);
function tssupp_form_submit($post_id, $form_data)
{
    // edit relationship slug and custom field slug
    $relationship = 'job-request-approval';
    $child_field = 'approval'; // (the field on the child to copy to the parent)
    $parent_field = 'files-for-approval'; // (the field on the parent to copy to the child)
  
    if (in_array($form_data['id'], array(19))) { // Edit form ID(s)
  
        if ( isset( $_POST['@'.$relationship.'_parent'] ) ){
             
            $parent_id = $_POST['@'.$relationship.'_parent'];
 
            // copy child field to the parent post
            if ( isset( $_POST['wpcf-'.$child_field] ) ){
                update_post_meta( $parent_id, 'wpcf-'.$child_field, $_POST['wpcf-'.$child_field] );
            }
 
            // copy the parent field to the child post
            $parent_value = get_post_meta( $parent_id, 'wpcf-'.$parent_field, true );
            update_post_meta( $post_id, 'wpcf-'.$parent_field, $parent_value );
 
        }
    }
}

Thank you

#1340909

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

We've been avoiding the complication of handling multiple files. Let's continue to do so until we get the basics right.

So can you clarify whether the code above works when there is just a single file added to the parent?

Re-reading the code it should be working.

When the child post is created (using the button to update the status) then the status gets copied from the child back to the parent so that it has the current status available for filtering, and the file field value gets copied from the parent to the child, so that it records what the file was at the time.

The different child posts will contain distinct values for the file field, depending on what the value of the file field was on the parent at the time the status was updated.

If your custom field group settings are such that the file field is available to both the parent and child posts, then you should be able to edit a child post in the back-end and see the value of the file field.

#1341009

I just changed the file field to single file upload and what it does is updates the file field of all child posts each time i upload a new file. Am i doing something incorect?

#1341183

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I just set up a test site to verify that the code works as expected, which it does, so I suspect there is another problem.

In my case I was editing the parent posts in the backend to replace the file field with a new file.

Then testing the button on the front end to generate the log entry post, and after a few iterations of updating the file attached to the parent post I found that each child post correctly had the value for the file field at the time the log entry was generated.

So, can you clarify how you are updating the file on the parent post, and can you double-check if there is any other custom code at work here that you may be using already for some reason?

#1341215

Could you take a look at my test website for a moment to see if im doing something wrong?

I can give you login credentials

#1341327

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Yes, I'll set up a private reply.

Can you clarify how you are updating the file field on the parent?

#1342003

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry, I was looking at your site but starting to get a bit lost.

So I've created an online demo site where you can see a simple version of this, which uses the same custom code, and where the logging of status changes and file attachments works as we've been describing.

You can log into the admin area here: hidden link

There is a Jobs CPT with a child Log entries CPT.

The custom fields Approval and File attachments are available to both.

If you visit the page hidden link you'll see this working.

A View lists the job posts (there is only one), and includes a form to update the status (which generates a child log-entry post) and another form to replace the file attachment. Below that a second View shows the log entries belonging to this job, which outputs the values of the approval and file attachment fields.

If you update the approval status and submit the form, it generates a log entry, copies the status from the log entry up to the parent job post, and copies whatever the current file attachment is to the new log entry post.

You can see the log entries where I've been doing that, and where the file attachment at the time the log entry was generated is preserved.

It's a pretty simple set up, and it works.

Can you please review that and see how to incorporate it into your own site.

Note that I still haven't updated the code to handle multiple file attachments, I want to get your site up to this position first.

#1342073

I can clearly see that is working on the link you provided.
We have the exact same workflow the only difference i see is that you have the forms inside the job list VIEW while i have them in a custom content template of a job not a View.
For example the Link i gave you above has the two forms and the view.

Do you think thats the issue?

#1342179

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I don't think so.

I just copied the markup from my outer View to a Content Template assigned to single job posts and if I visit the job post on the front end I see the same results as when displaying it in a View.

There must be some detail that you have set differently, and you know your site better than I do, I think you are best placed to compare the two and identify the difference.

#1349059

Hello Nigel,

Apologies for the late reply. I managed to make it work following your live test site and works pretty well, but how can this code work for multiple files?

Thanks a lot for looking into this!

#1349283

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Glad to hear you have it working.

I updated the test site and confirmed the code below works with multiple attachments:

add_action('cred_save_data', 'tssupp_form_submit', 10, 2);
function tssupp_form_submit($post_id, $form_data)
{
    // edit relationship slug and custom field slug
    $relationship = 'job-log-entry';
    $child_field = 'approval'; // (the field on the child to copy to the parent)
    $parent_field = 'file-attachments'; // (the field on the parent to copy to the child)
   
    if (in_array($form_data['id'], array(29))) { // Edit form ID(s)
   
        if ( isset( $_POST['@'.$relationship.'_parent'] ) ){
              
            $parent_id = $_POST['@'.$relationship.'_parent'];
  
            // copy child field to the parent post
            if ( isset( $_POST['wpcf-'.$child_field] ) ){
                update_post_meta( $parent_id, 'wpcf-'.$child_field, $_POST['wpcf-'.$child_field] );
            }
  
            // copy the parent field to the child post
            $parent_values = get_post_meta( $parent_id, 'wpcf-'.$parent_field, false );
            foreach ($parent_values as $parent_value) {
                add_post_meta( $post_id, 'wpcf-'.$parent_field, $parent_value );
            }
        }
    }
}