Warning just once on content part edit

Topics: Writing modules
Mar 10, 2011 at 11:33 AM
Edited Mar 10, 2011 at 11:34 AM

Hi,

I have a content part and I want to warn the user if they've not filled in an input. It's not required, but the functionality will be limitted so I want to make sure the user knows they've done it.

I'm using:

_services.Notifier.Warning(T("This location is not part of a map list. Select a map list if you want it to appear on one."));

to do the notifying.

First I put the checking code and in the POST version of the Editor method in the driver. The problem is this method gets called when the URL for the title is being checked using AJAX. This means that my warning message gets printed twice on the page.

Then I put it in the get method, but this also shows the message multiple times so the method must be getting called more than once.

Lastly I tried using onCreated in the handler. This also showed it mutiple times.

 

Ideally I want this warning to be shown anytime someone looks at the edit screen. Is there a bit of code that will only get called once when the editor view is shown or is there a way to check if a message already exists in the notify store.

 

Thanks,

Richard.

Coordinator
Mar 10, 2011 at 8:57 PM

Have you tried INotificationProvider ? We use it on the admin side to provide banners, like when some settings are not correctly configured. You can also add a test inside to check when you want to trigger it. There are several examples in the core modules.


Mar 10, 2011 at 9:46 PM

Thanks Sebastien,

I'm almost there now. This is based on MissingSettingsBanner in Email. I just can't work out how to get hold of my content part. I've tried this, but I think I'm barking up the wrong tree:

public IEnumerable<NotifyEntry> GetNotifications()
{
var locationPart = _services.WorkContext.CurrentSite.As<BingLocationPart>();

if (locationPart != null && locationPart.BingMapList == null)
{
yield return new NotifyEntry { Message = T("This location is not part of a map list. Select a map list if you want it to appear on one"), Type = NotifyType.Warning };
}
}

Mar 11, 2011 at 8:06 PM

I don't think INotificationProvider is what I'm looking for. I think the problem I'm having with duplicate messages is down to this that I've found in

NotifyFilter.cs

Line 92: //todo: (heskew) probably need to keep duplicate messages from being pushed into the zone like the previous behavior

Until that's fixed (if that's what's causing it) is there a way I can check TempData from a driver to see if it already contains my warning message?

//todo: (heskew) probably need to keep duplicate messages from being pushed into the zone like the previous behavior

Mar 12, 2011 at 9:06 PM

Solved it in a slightly hacky, but reasonably neat way.

I noticed that all the ajax requests that were calling my Editor Driver code to check the slug were POST requests so I wrote this method that only adds the warning if it is a GET request:

private void warnOnce(String message)
{
	var method = _services.WorkContext.HttpContext.Request.RequestType;
	if (method == "GET")
	{
		_services.Notifier.Warning(T(message));
	}
}
This has sorted me out, so all is well.