Copied to clipboard

Flag this post as spam?

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


  • Jon 92 posts 166 karma points
    Nov 11, 2020 @ 16:49
    Jon
    0

    ContentPublishedEventArgs - get which Language Variant is being published

    Hi,

    I am in the process of setting up Indexing to an external API and I need to find a way to know which Language variant is currently being Published. I am using the following code and needed a way to pick out the current language - at the moment publishedPageCulture is saying it is the default language and not the correct language.

       private void IndexPage(ContentPublishedEventArgs e)
        {
            // Find the core settings page
            int thisPage = e.PublishedEntities.Select(x => x.Id).FirstOrDefault();
            var umbracoHelper = Umbraco.Web.Composing.Current.UmbracoHelper;
            IPublishedContent currentPage = umbracoHelper.Content(thisPage);
            string publishedPageCulture = PublishedContentExtensions.GetCultureFromDomains(currentPage);
    

    any ideas on how I can get the correct language variant?

    Thanks

    Jon

  • Malthe Petersen 68 posts 383 karma points c-trib
    Nov 12, 2020 @ 12:51
    Malthe Petersen
    0

    Hi Jon.

    If you loop over the AvailableCultures iin the IContent that is being sent to you in the ContentPublishedEventArgs you should be able to do as following:

    private void IndexPage(ContentPublishedEventArgs e) 
        {
          foreach(var content in e.PublishedEntities) 
          {
            foreach(var culture in content.AvailableCultures)
             {
               var isPublished = e.IsPublishedCulture(content, culture);
             }
          }
        }
    

    Remember that if you do access the umbracoHelper.Content(id) it wont be the newest version of your content as the cache has not yet been refreshed. So I suggest, depending on your need, to stick with just the IContent from the event args. :-)

    Hope it helped a little bit.

    Regards Malthe

Please Sign in or register to post replies

Write your reply to:

Draft