# Track modification timestamps per field

**URL:** <https://discourse.getcockpit.com/t/track-modification-timestamps-per-field/1698>\
**Category:** Support\
**Created:** [November 2, 2020, 2:42pm UTC](https://discourse.getcockpit.com/t/track-modification-timestamps-per-field/1698 "2020-11-02T14:42:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kasperkamperman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/kasperkamperman/32/688_2.png) [@kasperkamperman](https://discourse.getcockpit.com/u/kasperkamperman)\
**Post date:** [November 2, 2020, 2:42pm UTC](https://discourse.getcockpit.com/t/track-modification-timestamps-per-field/1698/1 "2020-11-02T14:42:08Z")

</div>

Question. I’d made an add-on for [openingtimes](https://github.com/kasperkamperman/cockpitcms_addons/tree/main/openinghours). Now I’d like to add a field to track when it was last modified.

I was thinking in a more general add-on, that add a modification field to a field with a timestamp if that field is changed.

I’ve found the [triggers overview](https://github.com/raffaelj/cockpit-scripts/blob/master/cli/get-triggers_list-of-events.md) , but where can I find the information passed.

I’d like to check if a field has been changed. How do I get access to the previous entry, because I currently get the current entry.

This code is based on the slug field that is generated based on title from @raffaelj  
Time stamp is added correctly, but I only want to add it when the contents of the openinghours field are changed.

```auto
$col = "locations";
$field = "openinghours";

$trackModifications = function($name, &$entry, $isUpdate) use ($app, $col, $field, $slugName) { $entry[$field][modified] = time();
};

$app->on("collections.save.before.$col", $trackModifications);

```

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [November 3, 2020, 11:52am UTC](https://discourse.getcockpit.com/t/track-modification-timestamps-per-field/1698/2 "2020-11-03T11:52:49Z")

</div>

You can check the original entry inside the `collections.save.before` event. Something like this (not tested):

```php
<?php

$app->on('collections.save.before'), function ($name, &$entry, $isUpdate) {

    if (!$isUpdate) return;

    $collection = 'locations';
    $field = 'openinghours';

    if ($name !== $collection) return;

    if (!isset($entry[$field])) return;

    $filter = [
        '_id' => $entry['_id'];
    ];

    $origEntry = $this->module('collections')->findOne($collection, $filter);

    if ($entry[$field] !== $origEntry[$field]) {

        $entry["{$field}_modified"] = time();

    }

});

```

---

<div class="post-metadata">

**Author:** ![kasperkamperman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/kasperkamperman/32/688_2.png) [@kasperkamperman](https://discourse.getcockpit.com/u/kasperkamperman)\
**Post date:** [November 4, 2020, 9:24am UTC](https://discourse.getcockpit.com/t/track-modification-timestamps-per-field/1698/3 "2020-11-04T09:24:20Z")

</div>

Thanks @raffaelj for your helpful response. This did the track after some small bug fixes.

I’ve made a more generic add-on out of it, so it can be used for multiple collections and fields. It also distinguishes if a field is an object (so modified is added to that object) or just a value/array, so an additional field is added to the collection (thanks to that part of the example as well).

> **[kasperkamperman/cockpitcms\_addons](https://github.com/kasperkamperman/cockpitcms_addons/tree/main/add_modified_field)**
>
> Contribute to kasperkamperman/cockpitcms\_addons development by creating an account on GitHub.
