`ComposeBefore` member saving
# help-with-umbraco
b
Due a UI/UX issue in Umbraco when creating a new member, we want to show a notification when saving member. https://github.com/umbraco/Umbraco-CMS/issues/14676 But this doesn't hit as Umbraco validates the model. Can we compose it before Umbraco is saving the entity/member?
Copy code
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
//[ComposeBefore(typeof(Something))]
public class SubscribeToEditorModelEventsComposer: ComponentComposer<SubscribeToEditorModelEvents>
{
    // This automatically adds the component to the Components collection of the Umbraco composition
}

public class SubscribeToEditorModelEvents : IComponent
{
    // Initialize: runs once when Umbraco starts
    public void Initialize()
    {
        MemberService.Saving += MemberService_Saving;
    }

    // Terminate: runs once when Umbraco stops
    public void Terminate()
    {
        // Unsubscribe during shutdown
        MemberService.Saving -= MemberService_Saving;
    }

    private void MemberService_Saving(IMemberService sender, SaveEventArgs<IMember> e)
    {
        foreach (var member in e.SavedEntities)
        {
            if (member.Id > 0)
                return;

            if (string.IsNullOrEmpty(member.Username) || string.IsNullOrEmpty(member.Email))
            {
                e.Messages.Add(new EventMessage("Error", "Remember to fill username and email.", EventMessageType.Error));
            }
        }
    }
}
6 Views