Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit Content Types for a given Page Type in Experience Manager

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:

  • Limit the content types, so that for a given page type authors can only create certain predefined content types
  • In the Dashboard, completely restrict the above to just predefined content types, by un-selecting Content Types Already Used on the Page
  • Use regions which specify combinations of schemas and templates along with quantity restrictions. Authors can only add (drag-and-drop) certain types and quantity of components to these region. For example, we can output the following on 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>
  • Authors may still see components they might want to insert (image below), but won’t be able to add them if regions control what’s allowed
  • However, folder permissions can reduce what components authors can see/use in the Insert Content library

Insert Content

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?

like image 998
Alvin Reyes Avatar asked Jan 24 '13 04:01

Alvin Reyes


1 Answers

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;
    }
}
like image 77
Nickoli Roussakov Avatar answered Nov 08 '22 16:11

Nickoli Roussakov