Posted on by

Dynamic Hidden Fields in the Admin

The Dynamic Hidden Field feature in Participants Database provides a way to capture dynamic values when a form is presented, such as the current user’s ID or the page the form is on. Normally the plugin only fills in the dynamic value when displaying a form on the frontend, but it is possible to capture these values in the backend when you’re creating a new record with the addition of a simple action handler.

The handler uses the ‘pdb-form_element_build_{$element}’ filter, which is fired when the named form element type is about to be shown in a form. In the backend, hidden fields are shown as text fields, so the filter we are using is ‘pdb-form_element_build_text-line’ The rest is explained in the code:

Capturing the User ID to an Administrative Field

This technique can’t be used to capture the value of a field that is not shown in the backend form, such as an administrative field when a non-admin user is creating a new record or editing a record. This is because such fields are not included in the form at all in that case.

There is a way to get his done, however. For instance, to record the ID of the user who added a record or the last person to update a record, you’d need to use a different technique: using the ‘pdb-after_submit_add’ or ‘pdb-after_submit_update’ actions to update the just-added or -edited record with the current user’s ID. Here is a very simple action handler to do that:

You will need to change the $user_id_field value to the name of the field you are using to store the user’s ID. Note that this will overwrite the value when a record is updated, so if you want to retain the data of who created the record, you’ll need to modify how this works.

Leave a Reply
You have to agree to the comment policy.

Leave a Reply
You have to agree to the comment policy.