7

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.

1

3 Answers 3

3

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.

6
  • Oh my ... any clue how they made that work for categories? There you can select ids? I think I have to look into it...
    – Alex
    Jun 20, 2013 at 8:17
  • edit But: I would go with the additional layout handle approach. Seams cheaper.
    – Marius
    Jun 20, 2013 at 9:06
  • The point is that I want to manage the pages in detail in the backend - with the handle approach there would be custom code needed, which can not be modified by the shop admin. Will try to implement & share.
    – Alex
    Jun 20, 2013 at 10:31
  • 2
    Short answer: You cannot. You don't mind, do you, if I say, that lacking such a basic CMS feature is another reason, why magento really sucks, when you work with it? Jan 10, 2014 at 13:37
  • 1
    @MészárosLajos. If this is the only reason you say that Magento sucks I think you need to dig deeper in what Magento can do. of course it has its limitations, some of them frustrating, but I don't think this is a reason to say "it sucks". When you are used to only one platform you thing that everything else sucks. When you know more of them you realize they all suck because of various reasons. So if the main focus of your app is widgets on specific pages, I say don't use Magento. But you want an e-shopt and this is just a "minor need" you should use it.
    – Marius
    Jul 16, 2014 at 6:19
5

As others have mentioned you cannot do this out of the box, however in Magento 2, you can add custom page types and individually add the pages you want to be able to insert widgets to, eg:

Vendor/Module/etc/frontend/page_types.xml

<?xml version="1.0"?>
<page_types xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_types.xsd">
    <type id="cms_page_view_id_custom-page" label="Custom Page"/>
</page_types>

Where custom-page is the CMS page identifier (URL Key).

This will make it show in the "Page" dropdown when adding new widgets:

enter image description here

You'll need to add each page individually but that might be acceptable depending on your use case.

1

Just to suggest a possible workaround that can be achieved without extensions:

  • Create a block, and insert the widget into that via the WYSIWYG editor's widget button.
  • On your CMS page, add that block via the layout XML section.

This isn't ideal - it requires some low-level coding knowledge for the XML, blocks by default do the annoying thing of adding <p> tags around widgets, and I'm not sure if adding a widget via the WYSIWYG editor is always going to add the widget's assets correctly (although my test in Magento 2 seems to add my widget's CSS assets fine). However, it is a solution that an admin can do without writing an extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.