Modifying the behavior of the Content Created workflow event

Before this change, the content item data would not be available when
ContentCreated is called, as the Update drivers haven't been called yet.
This change will only trigger the event when the item is first created and its data has been updated.
It also introduces a new ContentUpdated event.
This commit is contained in:
Sebastien Ros
2013-08-08 16:41:52 -07:00
parent afdd80126b
commit 639cfad7ec
4 changed files with 64 additions and 38 deletions

View File

@@ -68,6 +68,16 @@ namespace Orchard.Workflows.Activities {
} }
} }
public class ContentUpdatedActivity : ContentActivity {
public override string Name {
get { return "ContentUpdated"; }
}
public override LocalizedString Description {
get { return T("Content is updated."); }
}
}
public class ContentPublishedActivity : ContentActivity { public class ContentPublishedActivity : ContentActivity {
public override string Name { public override string Name {
get { return "ContentPublished"; } get { return "ContentPublished"; }

View File

@@ -1,37 +0,0 @@
using System.Collections.Generic;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Workflows.Services;
namespace Orchard.Workflows.Handlers {
public class RulePartHandler : ContentHandler {
public RulePartHandler(IWorkflowManager workflowManager) {
OnPublished<ContentPart>(
(context, part) =>
workflowManager.TriggerEvent("ContentPublished",
context.ContentItem,
() => new Dictionary<string, object> { { "Content", context.ContentItem } }));
OnRemoving<ContentPart>(
(context, part) =>
workflowManager.TriggerEvent("ContentRemoved",
context.ContentItem,
() => new Dictionary<string, object> { { "Content", context.ContentItem } }));
OnVersioned<ContentPart>(
(context, part1, part2) =>
workflowManager.TriggerEvent("ContentVersioned",
context.BuildingContentItem,
() => new Dictionary<string, object> { { "Content", context.BuildingContentItem } }));
OnCreated<ContentPart>(
(context, part) =>
workflowManager.TriggerEvent("ContentCreated",
context.ContentItem,
() => new Dictionary<string, object> { { "Content", context.ContentItem } }));
}
}
}

View File

@@ -0,0 +1,53 @@
using System.Collections.Generic;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Workflows.Services;
namespace Orchard.Workflows.Handlers {
public class WorkflowContentHandler : ContentHandler {
private readonly HashSet<int> _contentCreated = new HashSet<int>();
public WorkflowContentHandler(IWorkflowManager workflowManager) {
OnPublished<ContentPart>(
(context, part) =>
workflowManager.TriggerEvent("ContentPublished",
context.ContentItem,
() => new Dictionary<string, object> { { "Content", context.ContentItem } }));
OnRemoving<ContentPart>(
(context, part) =>
workflowManager.TriggerEvent("ContentRemoved",
context.ContentItem,
() => new Dictionary<string, object> { { "Content", context.ContentItem } }));
OnVersioned<ContentPart>(
(context, part1, part2) =>
workflowManager.TriggerEvent("ContentVersioned",
context.BuildingContentItem,
() => new Dictionary<string, object> { { "Content", context.BuildingContentItem } }));
OnUpdated<ContentPart>(
(context, part) => {
workflowManager.TriggerEvent("ContentUpdated",
context.ContentItem,
() => new Dictionary<string, object> { { "Content", context.ContentItem } });
// Trigger the ContentCreated event only when its values have been updated
if(_contentCreated.Contains(context.ContentItem.Id)) {
workflowManager.TriggerEvent("ContentCreated",
context.ContentItem,
() => new Dictionary<string, object> {{"Content", context.ContentItem}});
}
});
OnCreated<ContentPart>(
// Flag the content item as "just created" but actually trigger the event
// when its content has been updated as it is what users would expect.
(context, part) => _contentCreated.Add(context.ContentItem.Id)
);
}
}
}

View File

@@ -162,7 +162,7 @@
<Compile Include="Drivers\UserTaskDriver.cs" /> <Compile Include="Drivers\UserTaskDriver.cs" />
<Compile Include="Drivers\WorkflowDriver.cs" /> <Compile Include="Drivers\WorkflowDriver.cs" />
<Compile Include="Forms\WebRequestForm.cs" /> <Compile Include="Forms\WebRequestForm.cs" />
<Compile Include="Handlers\ContentHandler.cs" /> <Compile Include="Handlers\RulePartHandler.cs" />
<Compile Include="Handlers\WorkflowHandler.cs" /> <Compile Include="Handlers\WorkflowHandler.cs" />
<Compile Include="ImportExport\WorkflowsCustomExportStep.cs" /> <Compile Include="ImportExport\WorkflowsCustomExportStep.cs" />
<Compile Include="ImportExport\WorkflowsExportEventHandler.cs" /> <Compile Include="ImportExport\WorkflowsExportEventHandler.cs" />