gergibs
gergibs

Reputation: 23

Orchard CMS: Create OnPublished event on existing content type

I have a content type called Article. We created the part inside the CMS admin console, so I do not have a corresponding ArticlePart and ArticlePartRecord in the module. Now I need to run an operation when an article is published. I'm having a hard time finding out where to intercept the publishing of an item. I would normally do this in a Handler, but I don't know how to create a handler in this scenario (not having the part and part record objects).

Upvotes: 2

Views: 282

Answers (2)

Rushabh Master
Rushabh Master

Reputation: 490

Just for those who are new, create a class and add below code.

Make sure to inherit from ContentHandlerBase class.

also add services.AddScoped<IContentHandler, MyCustomPublishHandler>(); to startup class.

And Done!

public class MyCustomPublishHandler : ContentHandlerBase
{
    private readonly IContentDefinitionManager _contentDefinitionManager;
    private readonly ILogger _logger;

    public MyCustomPublishHandler(
        IContentDefinitionManager contentDefinitionManager,
        ILogger<ContentPartHandlerCoordinator> logger)
    {
        _contentDefinitionManager = contentDefinitionManager;
        _logger = logger;
    }

    public override async Task PublishedAsync(PublishContentContext context)
    {
        // do something
        var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(context.ContentItem.ContentType);
        _logger.LogInformation($"published {contentTypeDefinition.DisplayName}");
    }
}

Upvotes: 0

devqon
devqon

Reputation: 13997

I think you can just override the Published method, like this:

protected override void Published(PublishContentContext context) {
    if (context.ContentType == "Article") {
        // do something
    }
}

Upvotes: 3

Related Questions