Magento – How to Add A Widget Instance to Specific CMS Pages

backendwidgets

In the backend under Widget instances it is possible to add a widget to certain pages, for example to specific categories.

How can I add one instance to some specific CMS pages?

In the Layout Updates section I choose Page -> Specific Page, but I can only choose "CMS Pages (All)" – there is no option to choose – for example – certain page identifiers.

Best Answer

Short answer: You cannot.

Workaround 1:
In the 'Page' dropdown are listed all the layout handles that have a <label> tag inside them. For example:

<cms_page translate="label">
    <label>CMS Pages (All)</label> 
    ...
</cms_page>

What you can do is to create a new layout handle, it can be empty, just make sure it has a label. Then add a new column on the cms/page entity (yes/no). Something like 'Use my new handle'. Then implement an Observer that loads your handle for the pages that have that attribute set to 'Yes' (since you cannot use <update handle=".." /> in the admin custom layout xml for pages). Now all your widgets linked to that layout handle will be displayed on the pages that use it.
If you want to have more than one of these custom handles then just replace the 'yes/no' dropdown with a select (or multiselect) with the layout handle you created and modify the observer to apply the handles selected for that page.
I know it's a bit 'ugly' but it should work.

edit

Possibile Implementation (as in categories)

For categories is a little different. If you want to add a similar process for cms pages, start with adding the cms pages option here: Mage_Widget_Block_Adminhtml_Widget_Instance_Edit_Tab_Main_Layout::_getDisplayOnOptions(). Then in the same class add a new option in getDisplayOnContainers(). Then see what js you can add in app/design/adminhtml/default/default/template/widget/instance/edit/layout.phtml. I would go with the additional layout handle approach. Seams cheaper.