AI OnAI Off
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.
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; }
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!