Copied to clipboard

Flag this post as spam?

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


  • Jonathan L Folland 35 posts 197 karma points
    Aug 03, 2023 @ 11:59
    Jonathan L Folland
    0

    Block List - Programmatic Allowed List and Validation

    I have a block list. I would like to set the initial allowed block types to be one of five. Once they add the first block type to the list, I would like to restrict the subsequent block types to be the same block type. I would also like to change the minimum and maximum number based upon the first added block type.

    Is there a programmatic way to override the block list configuration for a specific block list?

  • Scott 4 posts 94 karma points
    Sep 13, 2023 @ 09:06
    Scott
    100

    I had a similar issue recently, where out of 4 possible types, only type A could be added once. You can put the following in a ContentSavingNotification handler ( https://docs.umbraco.com/umbraco-cms/reference/notifications/contentservice-notifications ) and tweak to your needs, hopefully will be enough to get you going

     foreach (var content in notification.SavedEntities.Where(c => c.ContentType.Alias == "yourNodeAlias"))
     {
         var itemsJson = content.GetValue("items").ToString();
         var blockValue = Newtonsoft.Json.JsonConvert.DeserializeObject<BlockValue>(itemsJson); //umbraco uses newtonsoft not system.text
         var amountOfYourTypeOfBlock = 0;
         foreach (var item in blockValue.ContentData)
         {
             var contentType = _contentTypeService.Get(item.ContentTypeKey);
             if (contentType != null)
             {
                 if (contentType.Alias == "imageLink")
                 {
                     amountOfYourTypeOfBlock++;
                 }
             }
    
             if (amountOfYourTypeOfBlock > 1)
             {
                 break;
             }
         }
    
         if (amountOfYourTypeOfBlock > 1)
         {
             notification.CancelOperation(new EventMessage("Error", "Please select 1 type only", EventMessageType.Error));
         }
     }
    
  • Jonathan L Folland 35 posts 197 karma points
    Sep 13, 2023 @ 12:22
    Jonathan L Folland
    0

    I can confirm this works for my needs. I also needed to know about that whole section in the documentation. This was a big help. Thank you!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies