Phil_TheAverage
Phil_TheAverage

Reputation: 69

Restricting block to be used only in a ContentArea that is inherited from a certain Interface in EpiServer

I have been thinking for a while about this. Imagine i have a block that is supposed to access properties from the pages where it has been dropped in a ContentArea. I have multiple pages, where each and everyone of the pages that are supposed to be compatible with this block, has to inherit from a interface(Lets call it IHaveProperties) and implement those properties so that i can cast "currentPage" into this interface and thereby access the properties within.

My only problem tho is that the users have a tendency in edit mode to drop this block to a contentarea where the block do not belong. This is my main concern. If the ContentArea on a Page is a implementation of the IHaveProperties interface, can i then restrict the block so that it only can be dropped onto a ContentArea that is a implementation of the interface?

Or would another way of doing this be to have a contentreference in the interface that is of this BlockType and have it rendered without a contentarea?

I was hoping for the ContentArea solution...

Upvotes: 1

Views: 694

Answers (2)

Johan Petersson
Johan Petersson

Reputation: 1027

You can use the attribute AllowedTypes on the content area property, and if that's not enough you can create your own validation by implementing IValidate<T>, please see http://world.episerver.com/documentation/developer-guides/CMS/Content/Validation/

Upvotes: 1

Andreas
Andreas

Reputation: 1355

Yes, you can do this by adding an attribute to the ContentArea property like this:

[AllowedTypes(typeof(IHaveProperties))]
public virtual ContentArea SomeContentArea { get; set; }

There is a great post about this from EPiServer here:

http://world.episerver.com/blogs/Ben-McKernan/Dates/2015/2/the-new-and-improved-allowed-types/

Upvotes: 2

Related Questions