Experience Manager (XPM) (user interface update for SDL Tridion 2011 SP1) lets administrators create Page Types, which have component presentations just like pages, but also add rules on how to create and allow additional content types.
For a given page type, I'd like to simplify an author's options by limiting content type choices.
I understand we can:
Content Types Already Used on the Page
Staging
to create a region:<div>
<!-- Start Region: {
title: "Promos",
allowedComponentTypes: [
{
schema: "tcm:2-42-8",
template: "tcm:2-43-32"
},
],
minOccurs: 1,
maxOccurs: 3
} -->
<!-- place the matching CPs here with template logic (e.g. TemplateBeginRepeat/ TemplateBeginIf with DWT) -->
</div>
Did I get them all? Any other ways in XPM functionality or possible extensions to consider on how to limit the allowed content for a given Page Type?
Alvin, you pretty much provided most of the options in your question. Another option, if a custom error message is desired or even finer level of control is to use the Event System. Subscribe to a Page's save event Initiated phase and write some validation code that throws an exception if an unwanted component presentation is on the page.
Since Page Types are really a combination of a page template, any metadata on the page and the types of component presentations on the page, we would need to check that we are dealing with the wanted page type and if encounter an CP that doesn't match what we desire, we can simply throw the exception. Here is some quick code:
[TcmExtension("Page Save Events")]
public class PageSaveEvents : TcmExtension
{
public PageSaveEvents()
{
EventSystem.Subscribe<Page, SaveEventArgs>(ValidateAllowedContentTypes, EventPhases.Initiated);
}
public void ValidateAllowedContentTypes(Page p, SaveEventArgs args, EventPhases phases)
{
if (p.PageTemplate.Title != "My allowed page template" && p.MetadataSchema.Title != "My allowed page metadata schema")
{
if (!ValidateAllowedContentTypes(p))
{
throw new Exception("Content Type not allowed on a page of this type.");
}
}
}
private bool ValidateAllowedContentTypes(Page p)
{
string ALLOWED_SCHEMAS = "My Allowed Schema A; My Allowed Schema B; My Allowed Schema C; etc"; //to-do put these in a parameter schema on the page template
string ALLOWED_COMPONENT_TEMPLATES = "My Allowed Template 1; My Allowed Template 2; My Allowed Template 3; etc"; //to-do put these in a parameter schema on the page template
bool ok = true;
foreach (ComponentPresentation cp in p.ComponentPresentations)
{
if (!(ALLOWED_SCHEMAS.Contains(cp.Component.Schema.Title) && ALLOWED_COMPONENT_TEMPLATES.Contains(cp.ComponentTemplate.Title)))
{
ok = false;
break;
}
}
return ok;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With