public void OnActionExecuted(ActionExecutedContext context) { if (context.Result == null) { return; } var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); var currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser; if (currentUser == null) { return; } if (context.Result is ObjectResult objectContent) { // Support both batch (dictionary) and single results IEnumerable models; if (objectContent.Value is IDictionary modelDictionary) { models = modelDictionary.Values; } else { models = new[] { objectContent.Value }; } foreach (var model in models) { switch (model) { case ContentItemDisplay content: _eventAggregator.Publish(new SendingContentNotification(content, umbracoContext)); break; case MediaItemDisplay media: _eventAggregator.Publish(new SendingMediaNotification(media, umbracoContext)); break; case MemberDisplay member: _eventAggregator.Publish(new SendingMemberNotification(member, umbracoContext)); break; case UserDisplay user: _eventAggregator.Publish(new SendingUserNotification(user, umbracoContext)); break; case IEnumerable <Tab <IDashboardSlim> > dashboards: _eventAggregator.Publish(new SendingDashboardsNotification(dashboards, umbracoContext)); break; case IEnumerable <ContentTypeBasic> allowedChildren: // Changing the Enumerable will generate a new instance, so we need to update the context result with the new content var notification = new SendingAllowedChildrenNotification(allowedChildren, umbracoContext); _eventAggregator.Publish(notification); context.Result = new ObjectResult(notification.Children); break; } } } }
public void OnActionExecuted(ActionExecutedContext context) { if (context.Result == null) { return; } IUmbracoContext umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); IUser? currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser; if (currentUser == null) { return; } if (context.Result is ObjectResult objectContent) { // Support both batch (dictionary) and single results IEnumerable models; if (objectContent.Value is IDictionary modelDictionary) { models = modelDictionary.Values; } else { models = new[] { objectContent.Value }; } foreach (var model in models) { switch (model) { case ContentItemDisplay content: _eventAggregator.Publish(new SendingContentNotification(content, umbracoContext)); break; case ContentItemDisplayWithSchedule contentWithSchedule: // This is a bit weird, since ContentItemDisplayWithSchedule was introduced later, // the SendingContentNotification only accepts ContentItemDisplay, // which means we have to map it to this before sending the notification. ContentItemDisplay?display = _mapper.Map <ContentItemDisplayWithSchedule, ContentItemDisplay>(contentWithSchedule); if (display is null) { // This will never happen. break; } // Now that the display is mapped to the non-schedule one we can publish the notification. _eventAggregator.Publish(new SendingContentNotification(display, umbracoContext)); // We want the changes the handler makes to take effect. // So we have to map these changes back to the existing ContentItemWithSchedule. // To avoid losing the schedule information we add the old variants to context. _mapper.Map(display, contentWithSchedule, mapperContext => mapperContext.Items[nameof(contentWithSchedule.Variants)] = contentWithSchedule.Variants); break; case MediaItemDisplay media: _eventAggregator.Publish(new SendingMediaNotification(media, umbracoContext)); break; case MemberDisplay member: _eventAggregator.Publish(new SendingMemberNotification(member, umbracoContext)); break; case UserDisplay user: _eventAggregator.Publish(new SendingUserNotification(user, umbracoContext)); break; case IEnumerable <Tab <IDashboardSlim> > dashboards: _eventAggregator.Publish(new SendingDashboardsNotification(dashboards, umbracoContext)); break; case IEnumerable <ContentTypeBasic> allowedChildren: // Changing the Enumerable will generate a new instance, so we need to update the context result with the new content var notification = new SendingAllowedChildrenNotification(allowedChildren, umbracoContext); _eventAggregator.Publish(notification); context.Result = new ObjectResult(notification.Children); break; } } } }