(動的なビュー)

Description

How to change the active view of a Plone content item programmatically

はじめに

Dynamic views are views which the content editor can choose for his/her content from Display... drop down menu in the green edit frame.

By default, Plone comes with dynamic views for

  • Folder listing
  • Summary
  • Photo album
  • etc.

Creating a dynamic view

Dynamic view can be skins template or BrowserView. Dynamic view machinery only cares about the path name coming after the context object.

Register a dynamic view menu item

Your content type must support dynamic views

  • Content type subclasses Products.CMFDynamicViewFTI.browserdefault.BrowserDefaultMixin

You need to register dynamic view menu item with the corresponding view in configure.zcml:

<browser:menuItem
      for="Products.ATContentTypes.interface.IATFolder"
      menu="plone_displayviews"
      title="Product listing"
      action="@@product_listing"
      description="List folder contents as product summary view"
      />

ノート

Products.ATContentTypes uses non-standard name for “interfaces” package. It is “interface” there, when all other packages use “interfaces”.

The view must be listed in portal_types for the content type. In this case we should enable it for Archetypes folder using the following GenericSetup XML profiles/default/types/Folder.xml.

Note that you don’t need to copy the whole Folder.xml / Topic.xml from Products/CMFPlone/profiles/default/types, but including the changed fields (view_methods) in XML code is enough.

You can also change this through portal_types in ZMI.

ノート

view_methods must not have @@ view signature in the method name.

<?xml version="1.0"?>
<object name="Folder"
   meta_type="Factory-based Type Information with dynamic views"
   i18n:domain="plone" xmlns:i18n="http://xml.zope.org/namespaces/i18n">
     <property name="view_methods">
      <element value="folder_summary_view"/>
      <element value="folder_tabular_view"/>
      <element value="atct_album_view"/>
      <element value="folder_listing"/>

      <!-- We retrofit these new views for Folders in portal_types info -->
      <element value="product_listing"/>

     </property>
</object>

Also if you want to Collections have this listing you need to add the following profiles/default/types/Topic.xml.

<?xml version="1.0"?>
<object name="Topic"
   meta_type="Factory-based Type Information with dynamic views"
   i18n:domain="plone" xmlns:i18n="http://xml.zope.org/namespaces/i18n">
 <property name="view_methods">
  <element value="folder_listing"/>
  <element value="folder_summary_view"/>
  <element value="folder_tabular_view"/>
  <element value="atct_album_view"/>
  <element value="atct_topic_view"/>

  <!-- We retrofit these new views for Folders in portal_types info -->
  <element value="product_listing"/>

 </property>
</object>

Checking that your view is available

Products.CMFDynamicViewFTI.browserdefault.BrowserDefaultMixin.getAvailableLayouts() returns the list of known layouts like following:

[('folder_summary_view', 'Summary view'),
('folder_tabular_view', 'Tabular view'),
('atct_album_view', 'Thumbnail view'),
('folder_listing', 'Standard view'),
('product_listing', u'Product listing')]
layout_ids = [ id for id, title in self.portal.folder.getAvailableLayouts() ]
self.assertTrue("product_list" in layout_ids)

Getting active layout

ノート

Need code example.

Changing default view programmatically

self.portal.folder.setLayout("product_listing")

Default page

Default page is the content chosen to display when the visitor arrives to URL without any subpages or views selected.

This is useful if you are doing folder listing manually and you want to filter out the default view.

default_page helper view can be used to manipulate default pages.

Getting default page

# Filter out default content
container = self.getListingContainer()
default_page_helper = getMultiAdapter((container, self.request), name='default_page')

# Return content object which is the default page or None if not set
default_page = default_page_helper.getDefaultPage(container)

Setting default page can be done as simple as setting default_page attribute of the folder to be the id of the default page:

folder.default_page = "my_content_id"

More information can be found in

Setting view using marker interfaces

If you need to have a view for few individual content items only it is best to do using marker interfaces.

  • Register a view against a marker interface
  • Assign this marker interface to a content item using Zope Management Interface

For more info

Default Folder dynamic view

This is what Plone Folder content views out of the box

  • Products.CMFPlone/skins/plone_content/folder_summary_view.pt (a non-view based old style Zope 2 page template)