Copied to clipboard

Flag this post as spam?

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


  • Biagio Paruolo 1621 posts 1914 karma points c-trib
    Oct 01, 2022 @ 06:58
    Biagio Paruolo
    0

    How to create node name by some field values

    It's possibile to naming a node with data from fields without manually edit? That is I've two fields Firstname and Surname then the node must be named with Surname + Firstname

    enter image description here

  • Dennis 75 posts 400 karma points MVP 3x
    Oct 01, 2022 @ 11:15
    Dennis
    0

    Hi Biagio,

    Yes it is possible, though perhaps not as elegantly as you would like.

    There are two steps to solve this:

    Step 1: You need to prefill the name field with a non-empty string. Something you can distinguish later in your code. I usually fill the name with a random guid. You can do this, by listening to the SendingContentModelNotification using a notification handler.

    Step 2 You need to handle the ContentSavingNotification. Check if the name of the content is the name you specified in step 1 (a random guid in my example). If it is, read values from the content model and replace the name.

    Also don't forget to do a check on the content type so you only perform your logic on the correct content items

  • Biagio Paruolo 1621 posts 1914 karma points c-trib
    Oct 01, 2022 @ 17:30
    Biagio Paruolo
    100

    This is the code:

    using System;
    

    using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Umbraco.Cms.Core; using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Notifications; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Composing; using Umbraco.Cms.Core.DependencyInjection; using Umbraco.Extensions; using Umbraco.Cms.Core.Models.ContentEditing; using Umbraco.Cms.Core.Models.Membership;

    namespace UGWCareRedTechExtension { public class ComponentFamilyServices { }

    public class ComponentFamilyServices_SubscribeToContentServiceSavingComposer : IComposer
    {
        // initialize: runs once when Umbraco starts
    
        public void Compose(IUmbracoBuilder builder)
        {
            builder.AddNotificationHandler<ContentSavingNotification, ComponentFamilyServicesContentSavingNotificationHandler>();
            builder.AddNotificationHandler<SendingContentNotification, ComponentFamilyServicesSendingContentNotificationHandler>();
    
        }
    
    
    }
    
    
    public class ComponentFamilyServicesSendingContentNotificationHandler : INotificationHandler<SendingContentNotification>
    {
        private readonly ILogger<ComponentFamilyServicesSendingContentNotificationHandler> _logger;
        public ComponentFamilyServicesSendingContentNotificationHandler(ILogger<ComponentFamilyServicesSendingContentNotificationHandler> logger)
        {
            _logger = logger;
        }
        public void Handle(SendingContentNotification notification)
        {
    
            SimpleNotificationModel simpleNotificationModel = new SimpleNotificationModel();
    
            if (notification.Content.ContentTypeAlias== "componenteFamiliare")
            {
                simpleNotificationModel.AddInfoNotification("Notifica Componente in creazione", "In creazione");
    
                // Access the property you want to pre-populate
                // each content item can have 'variations' - each variation is represented by the `ContentVariantDisplay` class.
                // if your site uses variants, then you need to decide whether to set the default value for all variants or a specific variant
                // eg. set by variant name:
                // var variant = notification.Content.Variants.FirstOrDefault(f => f.Name == "specificVariantName");
                // OR loop through all the variants:
                foreach (var variant in notification.Content.Variants)
                {
                    // Check if variant is a 'new variant'
                    // we only want to set the default value when the content item is first created
                    if (variant.State == ContentSavedState.NotCreated)
                    {
                        // each variant has an IEnumerable of 'Tabs' (property groupings)
                        // and each of these contain an IEnumerable of `ContentPropertyDisplay` properties
                        // find the first property with alias 'publishDate'
                        var pubDateProperty = variant.Tabs.SelectMany(f => f.Properties)
                            .FirstOrDefault(f => f.Alias.InvariantEquals("publishDate"));
                        if (pubDateProperty is not null)
                        {
                            // set default value of the publish date property if it exists
                            pubDateProperty.Value = DateTime.UtcNow;
                        }
    
                        variant.Name = "Creating...";
                    }
                }
    
            }
        }
    }
    
    
    public class ComponentFamilyServicesContentSavingNotificationHandler : INotificationHandler<ContentSavingNotification>
    {
        private readonly ILogger<ComponentFamilyServicesContentSavingNotificationHandler> _logger;
        private INotificationService _notificationService;
        private IUser _user;
        public ComponentFamilyServicesContentSavingNotificationHandler(ILogger<ComponentFamilyServicesContentSavingNotificationHandler> logger )
        {
            _logger = logger;
    
        }
        public void Handle(ContentSavingNotification notification)
        {
    
            foreach (var content in notification.SavedEntities
                // Check if the content item type has a specific alias
                .Where(c => c.ContentType.Alias.InvariantEquals("componenteFamiliare")))
            {
                string cognome = content.GetValue<string>("cognome");
                string nome = content.GetValue<string>("nome");
                content.Name = cognome + " " + nome;
            }
        }
    }
    

    }

  • 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