Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Louis Ferreira 69 posts 265 karma points
    Dec 19, 2017 @ 13:53
    Louis Ferreira
    0

    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?

  • Louis Ferreira 69 posts 265 karma points
    Dec 20, 2017 @ 11:04
    Louis Ferreira
    0

    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:

    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.

    I see someone else has a similar issue too

    @rustyswayne any help?

Please Sign in or register to post replies

Write your reply to:

Draft