0
votes

I have created a content item "Company" that has List Part of items "Offer". When I delete Company all of it's Offers children are NOT being removed and leave no way to do it later (possibly manually in DB, but that's not the way I'd like to achieve it). Those items are still accessible in the customer user interface (Razor Page).

All of the above happens manually in the Orchard admin panel. I'm using decoupled cms mode.

Is there a possibility to get Orchard to do cascade delete on those items?

1

1 Answers

1
votes

I’ve just prepared some fragment of code. Maybe it would be appropriate in your context. Firstly, you should create ContentPartHandler with ListPart as a generic type and override the RemoveAsync method. Then you can get all list items and remove them (as in the example below).

public class MyListPartHandler : ContentPartHandler<ListPart>
{
    private readonly IOrchardHelper _orchardHelper;
    private readonly IServiceProvider _serviceProvider;

    public MyListPartHandler(IOrchardHelper orchardHelper, IServiceProvider serviceProvider)
    {
        _orchardHelper = orchardHelper;
        _serviceProvider = serviceProvider;
    }

    public override async Task RemovedAsync(RemoveContentContext context, ListPart instance)
    {
        var items = await _orchardHelper.QueryListItemsAsync(instance.ContentItem.ContentItemId);
        var contentManager = _serviceProvider.GetService<IContentManager>();
        var tasks = items.Select(item => contentManager.RemoveAsync(item));
        await Task.WhenAll(tasks);
    }
}

Finally, you should register your handler in the ConfigureServices method of the Startup class.

services.AddContentPart<ListPart>().AddHandler<MyListPartHandler>();