2

I want to trigger an event whenever editor removes a block (using "Remove" on the Content Area) and when a user click on "Move to Trash" on a block in the asset pane.

I find DataFactory.Instance.MovedContent event which is firing on every click on "Move to Trash"

But while clicking on Remove on the Content Area its not firing.

Update:-

I am doing these steps to achieve clicking on remove

  1. Register event handlers for saving event and saved event for the page.

  2. In saving event, get the page that is being saved, get the block ids from the content area by ContentArea.Items. Use the contentlink.ID property.

  3. Store those ids in a List, store it in memory somewhere, preferably in httpcontext.items collection since you only need it for the request but a short lived cache also works. Now you know the ids of all blocks before the change by editor.

  4. In saved event, get a new list of ids like above. Now you know the ids after editor change. Some block ids will be missing. Handle those by whatever you want to do...

    void Instance_SavingContent(object sender, ContentEventArgs e)
    {
        if (e.Content is ListPdfDocumentBlock)
        {
            var properties = e.Content.GetType().GetProperties().Where(i => i.PropertyType == typeof(ContentArea));
            if (properties != null)
            {
                List<int> ids = new List<int>();
                foreach (var property in properties)
                {
                    ContentArea contentArea = property.GetValue(e.Content) as ContentArea;
                    if (contentArea != null)
                    {
                        foreach (ContentAreaItem contentAreaItem in contentArea.Items)
                        {
                            IContent itemContent = contentAreaItem.GetContent();
                            ids.Add(itemContent.ContentLink.ID);
                        }
                    }
                }
                HttpContext.Current.Items.Add("pdfId", ids);
            }
        }
    }
    

But the problem with this code is,its always returning updated ids. then how i will compare from old to new to identify which block is removed.

Janmejay Kumar
  • 309
  • 2
  • 7

1 Answers1

2

When you remove something from a content area, the item reference is simply removed from that content area. The actual content (for example block/media) removed from the content area isn't actually moved/deleted (it will still exist where it was created, commonly in some asset folder).

So, the only thing happening when something is removed from a content area is that the content containing the content area is changed. The removed item is unchanged.

You could hook up the PublishingContent event and compare the content being published to the currently published version (if any) and compare the items in its content area to see if anything was added or removed.

Ted Nyberg
  • 7,001
  • 7
  • 41
  • 72
  • Can you share some code or link like how i can compare the content and items – Janmejay Kumar Feb 13 '19 at 10:47
  • You can simply iterate over the content area property for the content being published, and compare to the items in the published version's content area. – Ted Nyberg Feb 13 '19 at 11:27
  • Hi @Ted Nyberg, I have updated my question with more details.can you look into that – Janmejay Kumar Feb 13 '19 at 12:26
  • @JanmejayKumar Your current code simply returns all IDs from the content being saved. You would also need to retrieve the previous version of the block (for example the already published version) to compare the IDs of the items in the content area. – Ted Nyberg Feb 13 '19 at 12:30
  • Actually i do not want the published version, instead i want as soon its saving the content or saved the content. that means i do not need of published version. because before publish i can add more blocks and remove more blocks – Janmejay Kumar Feb 13 '19 at 12:52
  • I want to trigger an event as soon as i remove the block – Janmejay Kumar Feb 13 '19 at 12:54
  • You need the previous version for comparison to determine if a block has been removed... – Ted Nyberg Feb 13 '19 at 12:57