Copied to clipboard

Flag this post as spam?

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


  • Zoran Latinovic 6 posts 86 karma points
    Aug 16, 2020 @ 10:48
    Zoran Latinovic
    0

    Getting current published language/culture variant in ContentService events

    Hi,

    I have multi lingual site and it is implemented using language variants.

    Is there a way to get which language variant or Culture is published in ContentService events?

    ContentService events are implemented in custom component and when I'm trying to get culture using Thread.CurrentThread.CurrentCulture.Name it always gets 'en-Us'.

    IContent in event only has properties with all cultures.

    Is there a way to get this language variant when in ContentService events? https://prnt.sc/u0ewfy

  • Yakov Lebski 591 posts 2347 karma points
    Aug 17, 2020 @ 02:57
    Yakov Lebski
    101

    Did you mean in saved and Published Events? you can use PublishedCultures

      public void Initialize()
            {  
                ContentService.Saved += ContentService_Saved;
            }
    
            private void ContentService_Saved(IContentService sender, ContentSavedEventArgs e)
            {           
    
                foreach (var content in e.SavedEntities)
                {
                    foreach (var culture in content.PublishedCultures)
                    {
    
    
                        }
    
                    }
    
                }
            }
    
  • Patrick van Kemenade 101 posts 339 karma points
    Apr 26, 2021 @ 13:18
    Patrick van Kemenade
    1

    Don't think the accepted answer is an answer to the question raised, just tried it. Below are my findings, correct me if I'm wrong.

    And feel free to point out a better solution to this.

    The content.PublishedCultures gives back all cultures that were at some point in the past ever published, not just the one being Saved (or Published). And just gives an array of string. So if it has never been published before and you do a Save it's skipped.

    And it doesn't help in determing which of the cultures has any changes in the Save event. Currently looking if I can find something that does.

    The best solution that works that I have come up with is this (Still not very elegant when you have a large number of cultures or properties to monitor).

        private void ContentService_Saving(Umbraco.Core.Services.IContentService sender, Umbraco.Core.Events.ContentSavingEventArgs e)
        {
            if (e.SavedEntities?.Count() == 0)
            {
                return;
            }
    
        foreach(var savedNode in e.SavedEntities)
        {
            if (savedNode.ContentType.Alias== "PUT YOUR ALIAS HERE")
            {
                // Now let's find the culture that is recently changes
                var existingNode = sender.GetById(savedNode.Id);
    
                foreach (var culture in savedNode.AvailableCultures)
                {
                    string newTitle = savedNode.GetValue<string>("Title", culture);
                    string oldTitle = existingNode.GetValue<string>("Title", culture);
    
                    if (newTitle != oldTitle)
                    {
                        var dummy = $"There is a change in the title property for node {savedNode.Name} for culture {culture} ";
                    }
    
                    // Repeat this for every field that you want to monitor for changes in Save ...
                }
            }
        }
    }
    

    And of course don't hardcode the "Put Your Alias here" and "Title" but use strongly typed objectmodel and then use GetModelPropertyType + ModelTypeAlias, but that's another discussion.

  • Marco Graziotti 40 posts 166 karma points c-trib
    May 06, 2021 @ 17:37
    Marco Graziotti
    1

    Hi Patrick, I agree with you. I think that the question was about how to retrieve the languages of a web page modified from the BO. Indeed, inside the concrete "IComponent" object, that you have created to intercept some BO events, you need the language culture to perform some "SetValue()" actions.

    I said languages, because from the BO, for the single web page, you can save and publish more than a language per time.

    To intercept for which languages the user performed an update in the BO, I used the method "event.IsSavingCulture(...)" as described in the "Saving" chapter, in the events reference manual.

    Below, in the "savingCultures" list of strings you will get only the language code of the updated page, for example if you have a web page in two languages, and you will update only the Italian, and not the English, you will get a list of one "it-it" element.

    Here the implementation:

    // register our component with Umbraco using a Composer
    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class WhenAPageIsSavedComposer : ComponentComposer<WhenAPageIsSavedComponent>
    {
        // nothing needed to be done here!
    }
    
    public class WhenAPageIsSavedComponent : IComponent
    {
        // initialize: runs once when Umbraco starts
        public void Initialize()
        {
            // do something as Umbraco starts up
            // for example subscribe to an event
            // Event List: https://our.umbraco.com/documentation/reference/events/contentservice-events
    
            // subscribe to content service saving event
            ContentService.Saving += ContentService_Saving;
        }
    
        private void ContentService_Saving(IContentService sender, Core.Events.ContentSavingEventArgs e)
        {
            foreach (IContent savedEntity in e.SavedEntities)
            {
                List<string> savingCultures = savedEntity.AvailableCultures.Where(f => e.IsSavingCulture(savedEntity, f)).ToList();
    
                //Cicle every modified culture
                foreach (string savedCulture in savingCultures)
                {
    
                    //Do your stuff HERE with "savedEntity" and "savedCulture"
    
                }
            }
        }
    
        // terminate: runs once when Umbraco stops
        public void Terminate()
        {
            // do something when Umbraco terminates
            ContentService.Saving -= ContentService_Saving;
        }
    }
    

    I hope that this can help other Umbraco users.

    Thank you

Please Sign in or register to post replies

Write your reply to:

Draft