Copied to clipboard

Flag this post as spam?

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


  • Keith 74 posts 240 karma points
    Dec 12, 2022 @ 13:49
    Keith
    0

    published and unpublished values in notification handlers

    Hi All,

    Just wondering if I should raise a bug for this or is there a reason for this behaviour.

    This happens on all properties, but to illustrate the issue, I have a property called "metaTitle" on one of my document types. When i edit the value and click "Save and Publish", the "saving" notification handler allows me to get the unpublished and published version of the property value. This is what I would expect. But the "publishing" handler does not give me the same values. it gives me the newly edited value for both. See the image below which explains the issue:

    enter image description here

  • Menno Mout 7 posts 78 karma points c-trib
    Dec 14, 2022 @ 12:10
    Menno Mout
    0

    Both the ContentSavingNotification and ContentPublishingNotification inherit from a generic class and implement it with the IContent type. IContent inherits from IContentBase that has the method GetValue<T>:

        public TValue GetValue<TValue>(string propertyTypeAlias, string culture = null, string segment = null, bool published = false)
        {
            if (!Properties.TryGetValue(propertyTypeAlias, out var property))
                return default;
    
            var convertAttempt = property.GetValue(culture, segment, published).TryConvertTo<TValue>();
            return convertAttempt.Success ? convertAttempt.Result : default;
        }
    

    This method then uses the Property implementation of IProperty GetValue which in turn uses a private method GetPropertyValue:

            public object GetValue(string culture = null, string segment = null, bool published = false)
        {
            // ensure null or whitespace are nulls
            culture = culture.NullOrWhiteSpaceAsNull();
            segment = segment.NullOrWhiteSpaceAsNull();
    
            if (!PropertyType.SupportsVariation(culture, segment)) return null;
            if (culture == null && segment == null) return GetPropertyValue(_pvalue, published);
            if (_vvalues == null) return null;
            return _vvalues.TryGetValue(new CompositeNStringNStringKey(culture, segment), out var pvalue)
                ? GetPropertyValue(pvalue, published)
                : null;
        }
    
        private object GetPropertyValue(IPropertyValue pvalue, bool published)
        {
            if (pvalue == null) return null;
    
            return PropertyType.SupportsPublishing
                ? (published ? pvalue.PublishedValue : pvalue.EditedValue)
                : pvalue.EditedValue;
        }
    

    This last method is the point where the variable published is finally used and determains what value is returned. So either SupportsPublishing is false and therefore returns the EditedValue or the value is changed between the saving and publishing notification. I could not figure out what was the case.

    Also I was able to replicate this issue in V10. And found another issue where unpublished content still returns a value when you call the method GetValue<> with the parameter published set to true.

    Since this still happens in V10 it's defenitly worth making a GitHub issue about this item.

Please Sign in or register to post replies

Write your reply to:

Draft