Is there a way to perform url/slug validation on product creation/saving to check if that url does not already exist in the CMS?
E.g.: I created a product that has slug /abc/ but I already have a content node called ABC which also has a url /abc/. If a visitor navigates to /abc/ they are returned the content node (first find wins) and not the product.
So what would be great is if there was a way to validate that a url is already in use and display a error/warning in Merchello when a product is created / updated which (will) have the same url.
What I am currently able to do is to prevent the product from being saved by hooking into the ProductServices event:
ProductService.Saving += OnProductServiceSaving;
Then in that method, I check for an existing node using the ContentCache:
private void OnProductServiceSaving(IProductService sender, SaveEventArgs<IProduct> e)
{
foreach (var savedEntity in e.SavedEntities)
{
foreach (var content in savedEntity.DetachedContents)
{
var slug = content.Slug.EnsureStartsWith('/').EnsureEndsWith('/');
var node = UmbracoContext.Current.ContentCache.GetByRoute(slug);
if (node != null)
{
var msg = new EventMessage("Category", $"The url '{content.Slug}' already exists as a Content Node called '{node.Name}'. Please change that content's node name, or this product's 'slug' property to something else.",EventMessageType.Warning);
e.CancelOperation(msg);
//TODO: add error message to back office here warning about existing url. How?
}
}
}
}
This works as expected (prevents product from being saved) but I have no idea how to add the warning alert to the back office.
slug & node url validation?
Using Merchello v2.6
Is there a way to perform url/slug validation on product creation/saving to check if that url does not already exist in the CMS?
E.g.: I created a product that has slug /abc/ but I already have a content node called ABC which also has a url /abc/. If a visitor navigates to /abc/ they are returned the content node (first find wins) and not the product.
So what would be great is if there was a way to validate that a url is already in use and display a error/warning in Merchello when a product is created / updated which (will) have the same url.
Is this possible?
So I am making some progress on this....
What I am currently able to do is to prevent the product from being saved by hooking into the ProductServices event:
Then in that method, I check for an existing node using the ContentCache:
This works as expected (prevents product from being saved) but I have no idea how to add the warning alert to the back office.
I see someone else has a similar issue too
@rustyswayne any help?
is working on a reply...