XF 2.0 How to listen for successful settings save event and store a timestamp?

CyberAP

Well-known member
I have a custom options page where I want to store my settings. I also want to store a timestamp for the latest setting change. I believe this has to be done with event listeners, but I can't find such an event. How can I catch that event and what would be the best place to store that timestamp? Should I create my own table for that or add an additional column to xf_option_group? What would be the fastest way to access it without overhead if possible?
 
Personally I'd store the timestamp itself as an actual option within your option group. A bit of a hacky but reasonable way of doing it:
  1. Create a an option with an ID of, e.g. myAddOnTimestamp
  2. Edit type should be "Named template"
  3. Format options should be the name of an admin template, e.g. option_template_myAddOnTimestamp
  4. Data type should be Unsigned integer
  5. Create the admin template. Contents should be as follows:
HTML:
<xf:hiddenval name="{$inputName}" value="{$xf.time}" />

The value that gets saved will be the time the page was loaded (which is roughly correct). You could then access it in templates etc. {$xf.options.myAddOnTimestamp}.

Another way of doing it is similar but this time use an empty template but use a validation callback. That would be able to override the option value to the current time.
 
Thanks, Chris! This looks like an even better solution since it doesn't require any code, but I can't get it working unfortunately. I did exactly like you said, created a new option, used new admin template there with these contents:

Code:
<span>timestamp: {$xf.options.my_addon_timestamp}</span>
<xf:hiddenval name="{$inputName}" value="{$xf.time}" />

But it doesn't save the value to the database. When I refresh the page I don't get my timestamp value. It's not set in the DB:

1510514595054.webp

Another problem is that I can't delete that option anymore. There's no edit button or anything I can use to edit option settings. I tried adding more options or set my template like this: template=admin:my_addon_timestamp, but that didn't help either.

I have checked the value in the hidden input and it's correct.

1510514980531.webp
 
Even tried like this: <xf:textbox name="{$inputName}" value="{$xf.time}" />. Didn't help.

I have created a simple checkbox there and it's working as intended though.

1510516696760.webp
 
Still struggling with this, I've renamed my template to correspond to its purpose and that didn't help. I've also recorded the post request. It looks like this:

Code:
------WebKitFormBoundaryTQAZUvwheOKZ2HPi
Content-Disposition: form-data; name="options[my_addon_timestamp]"

1510593079
------WebKitFormBoundaryTQAZUvwheOKZ2HPi
Content-Disposition: form-data; name="_xfToken"

1510593079,5094f4a0c350399ee6094d7f482f2be5
------WebKitFormBoundaryTQAZUvwheOKZ2HPi
Content-Disposition: form-data; name="_xfRequestUri"

/admin.php?options/groups/DynamicAvatarExtension/
------WebKitFormBoundaryTQAZUvwheOKZ2HPi
Content-Disposition: form-data; name="_xfWithData"

1
------WebKitFormBoundaryTQAZUvwheOKZ2HPi
Content-Disposition: form-data; name="_xfToken"

1510593079,5094f4a0c350399ee6094d7f482f2be5
------WebKitFormBoundaryTQAZUvwheOKZ2HPi
Content-Disposition: form-data; name="_xfResponseType"

json
------WebKitFormBoundaryTQAZUvwheOKZ2HPi--

Clearly the data has been sent to the server but it has not been stored in the database. I've checked error logs and they are empty. How do I trace where it fails to save my data?
 
My bad. Some things with template options aren't automatically included. Your template should look like this:
HTML:
<xf:formrow hint="{$hintHtml}">
   <xf:hiddenval name="{$inputName}" value="{$xf.time}" />
   {$listedHtml|raw}
</xf:formrow>
This will ensure the template will work (the $listedHtml line marks the particular option as "listed", otherwise it would be ignored) and the {$hintHtml} hint will display the edit link.
 
My bad. Some things with template options aren't automatically included. Your template should look like this:
HTML:
<xf:formrow hint="{$hintHtml}">
   <xf:hiddenval name="{$inputName}" value="{$xf.time}" />
   {$listedHtml|raw}
</xf:formrow>
This will ensure the template will work (the $listedHtml line marks the particular option as "listed", otherwise it would be ignored) and the {$hintHtml} hint will display the edit link.
Thanks, Chris, this is working as expected now!
 
Back
Top Bottom