OrchardCore icon indicating copy to clipboard operation
OrchardCore copied to clipboard

Add Cancel to CreateContentContext

Open hishamco opened this issue 8 months ago • 11 comments

I came up with a scenario where I must cancel creating a content item if something goes wrong. Today, PublishContentContext already has Cancel property, which is good

I can submit a PR if there's no objection

/cc @MikeAlhayek @gvkries

hishamco avatar May 19 '25 17:05 hishamco

I am not sure it's needed. Create saves the content item in the session (but does not commit the changes). So if you want to cancel the request, you can call _session.CancelAsync() which will cancel the request.

For example you can do this

// Stage the content item to be created
await _contentManager.CreateAsync(contentItem, VersionOptions.Draft);

// this will cancel the creation.
await _session.CancelAsync();

MikeAlhayek avatar May 19 '25 20:05 MikeAlhayek

I wrote my code in the ContentHandler, I don't want to publish or make the content type as draft

hishamco avatar May 19 '25 22:05 hishamco

_session.CancelAsync() will work anywhere, also in a handler. But I don't think it's too nice to cancel the transaction if we can better.

However, what's the scenario that this would solve that PublishContentContext.Cancel doesn't?

Piedone avatar May 20 '25 19:05 Piedone

_session.CancelAsync()

I did it as my last attempt, but I ended up with Page is not found

However, what's the scenario that this would solve that PublishContentContext.Cancel doesn't?

If I have such a Cancel property, I could easily revert the item creation

hishamco avatar May 20 '25 23:05 hishamco

Yeah, but what's the full scenario? What's the user need you can't address now? But additionally, why do you want to cancel the creation as opposed to the publishing, what happens during creation that you want to avoid?

Piedone avatar May 21 '25 00:05 Piedone

I have a manager who adds employees. Once the employee is created, a user is created behind the scenes. Sometimes a user is not created for some reason, for example email is already in use. What I want is to go to the edit page and show an alert when something goes wrong, but now the employee is created even the user is not

hishamco avatar May 21 '25 00:05 hishamco

That user creation should rather happen after publishing, in PublishedAsync. Create is too early and at that point it's by far not certain that the content item will be saved.

Piedone avatar May 21 '25 00:05 Piedone

The problem is when I cancel on publishing, the item will moved into a draft which is not expected

hishamco avatar May 21 '25 00:05 hishamco

I see, and yes, now I see why such a cancel would be useful.

Piedone avatar May 21 '25 11:05 Piedone

I will submit a PR for this

hishamco avatar May 22 '25 02:05 hishamco

We triaged this issue and set the milestone according to the priority we think is appropriate (see the docs on how we triage and prioritize issues).

This indicates when the core team may start working on it. However, if you'd like to contribute, we'd warmly welcome you to do that anytime. See our guide on contributions here.

github-actions[bot] avatar May 22 '25 17:05 github-actions[bot]