Sometimes Hiding Properties in One Block that are used in Different Pages

Vote:
 

I have a custom block created called CTA that has properties like DisplayText, Url, Description, etc. I'm using this block as a property on a few different types of Pages, but there are a few instances where I want to hide the Description property on the CTA block for one type of Page. Is it possible to pass some attribute to hide the Description field only one Page Type A but keep it on Page Type B and continue to use only the one CTA block?

I understand that if I just wanted to hide the Description property on the CTA block I could do something like this:

[ScaffoldColumnAttribute(false)]
public virtual string Description { get; set; }

But I don't know of a way to set that on a different class that uses the CTA block like:

public virtual Cta Link { get; set; }

Any thoughts would be appreciated - thanks!

#198292
Edited, Oct 24, 2018 18:15
Vote:
 

Not sure if it's possible to do what you want. An alternate way of solving it would be to have two block types, and one inherits the other.

#198297
Oct 24, 2018 22:20
Vote:
 

Hi Todd,

When I've needed to do this I've generally taken the approach described by Erik but it should be possible to do what you're trying. If you create a validator like the following, implementing IMetadataAware then use it to check against a list of content types you pass in, you can set whether the field should be visible in the editor:

    public class HidePropertyValidationAttribute : ValidationAttribute, IMetadataAware
    {
        private string[] _restrictedPageTypes;

        public HidePropertyValidator(string[] restrictedTypes)
        {
            _restrictedPageTypes = restrictedTypes;
        }

        public void OnMetadataCreated(ModelMetadata metadata)
        {
            var contentMetadata = metadata as ContentDataMetadata;
            if (contentMetadata?.FindOwnerContent() is PageData pageData && _restrictedPageTypes.Any(x => x.Equals(pageData.PageTypeName)))
            {
                metadata.ShowForEdit = false;
            }
        }
    }

You can then use that validator on your property like this, passing in the names of the page types which shouldn't show the property:

        [HidePropertyValidation(new[] { "MyPageType", "AnotherPageType" })]
        public virtual string Description { get; set; }
#198350
Oct 25, 2018 19:35
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.