Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Angel 54 posts 111 karma points
    Dec 08, 2022 @ 16:40
    Angel
    0

    Last Updated Date for BlockListItem

    Hi there.

    Is there a property for a blocklistitem for when the item was last updated? I want to be able to display a "new" icon next to a blocklist item depending on when it was last updated, and this will need a compare with today's date.

    Any ideas? Thanks in advance.

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Dec 08, 2022 @ 17:58
    Søren Gregersen
    0

    Hi,

    There is no way do see on a given block, when it was inserted. If you want/need this, you should add a date to the blocks, and set it when the block is saved.

    If you need for it to be hidden, you may be able to find some inspiration here https://cornehoskam.com/posts/hiding-tabs-and-properties-from-backoffice-users-umbraco-9

    HTH :)

  • Angel 54 posts 111 karma points
    Dec 08, 2022 @ 18:01
    Angel
    0

    Thank you for your response Soren!

    Could you provide me with some help on how to do this on save of the block?

    TIA.

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Dec 08, 2022 @ 18:11
    Søren Gregersen
    0

    In umbraco 9+ you would create a notification handler for this.

    Read up on how notifications work here https://docs.umbraco.com/umbraco-cms/reference/notifications

    The notification you need to handle is https://docs.umbraco.com/umbraco-cms/reference/notifications/contentservice-notifications#saving

    This will allow you to check if the content you are saving may contain the property that you want to handle.

    public void Handle(ContentSavingNotification notification)
    {
        foreach (var entity in notification.SavedEntities)
        {
            // if entity is the right type
            var blocks = [GetBlocksFromEntity];
            foreach(var block in blocks.Where(x => [BlockNeedsADate](x))
            {
                // set date
            }
        }
    }
    

    You would need to implement the two methods with [...]

  • Angel 54 posts 111 karma points
    Dec 09, 2022 @ 16:23
    Angel
    0

    Would this be the same way for V8? or is this even possible for v8?

    Thanks Soren.

  • Menno Mout 7 posts 78 karma points c-trib
    Dec 14, 2022 @ 13:57
    Menno Mout
    0

    The notification system of V9+ replaced the event system that is in V8. However the example Soren showed is still valid. You are able to do something similar with the saving event in V8. Check this documentation for more details: https://our.umbraco.com/documentation/Reference/Events/ContentService-Events/

  • Angel 54 posts 111 karma points
    Dec 14, 2022 @ 14:19
    Angel
    0

    There is an event on when the node is saved, and therefore, we can set a date for "all" the blocks on that node, however - is there an event for when a single block is saved, such that we can set the date for the single block and not the other blocks on the node?

  • Menno Mout 7 posts 78 karma points c-trib
    Dec 14, 2022 @ 14:49
    Menno Mout
    0

    One thing you can do is check if a property is dirty (has been changed). In this case the property would be the block list. If it is dirty (has been changed) then you need to figure out what blocks in de block list have been changed. This is the part I'm not sure on how to do either. Maybe you can use the block list items Udi to check the contentService for when they were created. I've not done or tried this myself so this might not be possible.

    Adding a DateTime field to your blocklist components might make things a lot easier (like suggested before). You can check if a DateTime has been set and if it hasn't been set you can do so in the saving event. This way your block list items will all have a DateTime and it's then easy to display them in an order you want.

  • Angel 54 posts 111 karma points
    Dec 14, 2022 @ 15:18
    Angel
    0

    Yeah - the part of figuring out if a block item is 'dirty' is what I cannot figure out.

    If I do add a datetime field, I don't think there is a way to figure out when that single block has been updated (and not the other blocks) - as far as my research has gone. If the content node is saved, then you would essentially be blanket updating the datetime field for all the blocks on that node and not just the ones that have been updated, which is not what I am looking for.

    The key would be to determine if the block item is dirty. As far as I know, there is no history for blocks, such that we can do a compare with to see if it has changed.

    Thanks Menno for your input.

  • Menno Mout 7 posts 78 karma points c-trib
    Dec 14, 2022 @ 15:51
    Menno Mout
    0

    You should be able to get the DateTime value from the block item and check it against: DateTime.MinValue. If the person creating the block item forgets to fill the property of the block item then the default value of the field will be DateTime.MinValue. Since you can hook into the saving event you can check if that value isn't DateTime.MinValue and if it is change that value to DateTime.Now. That way every block item will have a filled DateTime property. This doesn't work for edited block items of course. For those items you'd be depending on the editor to fill the right DateTime.

  • Angel 54 posts 111 karma points
    Dec 14, 2022 @ 16:13
    Angel
    0

    Yeah - i'm trying to avoid having the editor updating any sort of datetime manually. It should all be done programmatically and updated each time there is an update to the block item.

    I want that datetime field to get populated/updated on save automatically as to not rely on the editor for this change.

    This should be the case for both new and edited blocks.

  • Menno Mout 7 posts 78 karma points c-trib
    Dec 14, 2022 @ 16:24
    Menno Mout
    0

    I can understand that. Then maybe this post (from a long time ago) https://our.umbraco.com/forum/developers/api-questions/40453-Get-stored-Content-item-to-compare-with-Content-item-on-saving-saved-events-603 can help. If you are able to get the old content before it's saved to the database and compare it you should be able to determine if it has changed. Mind you, I've no idea if this still works for normal content let alone block list items.

Please Sign in or register to post replies

Write your reply to:

Draft