Qite / Umbraco-Inception

A code first approach for Umbraco (7)
MIT License
26 stars 9 forks source link

Recursive checking for data #3

Closed nojaf closed 10 years ago

nojaf commented 10 years ago

Use case:

On my homepage I want to have an background image that is selected by imagepicker. On a child page I can select the image again but if I didn’t fill it in I want to have one of the ancestors page background image.

If I look at the code now it only checks the current IPublishedContent but in some cases I need to have the ancestor IPublishedContent be checked if it is filled in.

JimBobSquarePants commented 10 years ago

Wouldn't you perform this kind of logic in your controller?

nojaf commented 10 years ago

I was thinking of modifing the ConvertToModel extension to

        private static void GetPropertyValueOnInstance(IPublishedContent content, object objectInstance, PropertyInfo propertyOnTab, UmbracoPropertyAttribute propertyAttribute, string alias = null)
        {
            if (alias == null) alias = propertyAttribute.Alias;
            //sugestion
            string umbracoStoredValue = content.GetPropertyValue<string>(alias, true);

            if (propertyAttribute.ConverterType != null)
            {
                TypeConverter converter = (TypeConverter)Activator.CreateInstance(propertyAttribute.ConverterType);
                propertyOnTab.SetValue(objectInstance, converter.ConvertFrom(null, CultureInfo.InvariantCulture, umbracoStoredValue));
            }
            else
            {
                propertyOnTab.SetValue(objectInstance, umbracoStoredValue);
            }
        }

Couldn't hurt I think.

nojaf commented 10 years ago

This is indeed something you would want to do in the controller and pass along in your custom RenderModel.