Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CourseSendingContentNotificationHandler : INotificationHandler<SendingContentNotification>
- {
- private readonly IContentService _contentService;
- private readonly IPublishedContentQuery _publishedContentQuery;
- public CourseSendingContentNotificationHandler(IContentService contentService, IPublishedContentQuery publishedContentQuery)
- {
- _contentService = contentService;
- _publishedContentQuery = publishedContentQuery;
- }
- public void Handle(SendingContentNotification notification)
- {
- var content = notification.Content as ContentItemDisplay;
- if (content == null)
- return;
- if (content.ContentTypeAlias == "course")
- {
- var settingsContent = _contentService.GetById(GetSettingsContentId());
- if (settingsContent != null)
- {
- var masterCoursePublished = _publishedContentQuery.ContentAtRoot().FirstOrDefault(x => x.ContentType.Alias == "settings")?.DescendantOfType("courseMaster");
- if (masterCoursePublished != null)
- {
- var masterCourse = _contentService.GetById(masterCoursePublished.Id);
- if (masterCourse != null)
- {
- foreach (var variant in notification.Content.Variants)
- {
- // Check if variant is a 'new variant'
- // we only want to set the default value when the content item is first created
- if (variant.State == ContentSavedState.NotCreated)
- {
- // each variant has an IEnumerable of 'Tabs' (property groupings)
- // and each of these contain an IEnumerable of `ContentPropertyDisplay` properties
- // find the first property with alias 'publishDate'
- var blockList = variant.Tabs.SelectMany(f => f.Properties)
- .FirstOrDefault(f => f.Alias.InvariantEquals("content"));
- if (blockList is not null)
- {
- var masterBlockList = masterCourse.GetValue<string>("content");
- BlockValue blockValue = JsonConvert.DeserializeObject<BlockValue>(masterBlockList);
- // set default value of the publish date property if it exists
- blockList.Value = masterBlockList;
- }
- }
- }
- }
- }
- }
- }
- }
- private int GetSettingsContentId()
- {
- return 1071;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment