Warning

This document hasn't been checked for compatibility with current versions of Plone. Use at your own risk.

Implement edit-view modes

by Tom Cameron last modified Dec 30, 2008 03:01 PM
Make it possible for users to show and hide the editable border and content tabs from a standard Plone site.

One of the common complaints I get from new users of Plone is the fact that once authenticated you are unable to remove the editable border and content tabs at will. This means that you cant easily see what the anonymous viewers are seeing or effectively preview your work.

This how-to implements a simple edit/view mode to Plone 2 and allows you to toggle between them quickly. (it may well work on earlier versions of Plone but Ive not tried it) It uses cookies to store the current mode status, it could also use sessions but I found that to be problematic.

You will need to create two new python scripts that are available at all times in your skins - probably in your /portal_skins/custom/ skin folder.

New python script 'getEditMode':

  ## Script (Python) "getEditMode"
  ##bind container=container
  ##bind context=context
  ##bind namespace=
  ##bind script=script
  ##bind subpath=traverse_subpath
  ##parameters=
  ##title=
  ##
  REQUEST = context.REQUEST

  if REQUEST.cookies.has_key('edit_mode'):
    if REQUEST.cookies.get('edit_mode') == '1':
      ret = 1
    if REQUEST.cookies.get('edit_mode') == '0':
      ret = 0
  else:
    ret = 0

  return ret

new python script 'toggleEditMode':

  ## Script (Python) "toggleEditMode"
  ##bind container=container
  ##bind context=context
  ##bind namespace=
  ##bind script=script
  ##bind subpath=traverse_subpath
  ##parameters=
  ##title=
  ##
  # Set vars
  REQUEST = context.REQUEST
  RESPONSE = REQUEST.RESPONSE
  url = context.absolute_url()
  qs = context.create_query_string(REQUEST.get('QUERY_STRING', ''), )

  #already set
  if REQUEST.cookies.has_key('edit_mode'):
    if REQUEST.cookies.get('edit_mode') == '1':
      RESPONSE.setCookie('edit_mode','0',expires='Wed, 19 Feb 2020 10:00:00 GMT',path="/")
      #print 'switched off'
    if REQUEST.cookies.get('edit_mode') == '0':
      RESPONSE.setCookie('edit_mode','1',expires='Wed, 19 Feb 2020 10:00:00 GMT',path="/")
      #print 'switched on'
  else:
    RESPONSE.setCookie('edit_mode','1',expires='Wed, 19 Feb 2020 10:00:00 GMT',path="/")
    #print 'set on'

  #return printed
  return RESPONSE.redirect('%s?%s' % (url, qs))

Now you need to customise the existing showEditableBorder method. This method has changed a little in several versions of plone so you need to make sure you customise the the current code on your installation. You will find the current script in the /portal_skins/plone_scripts/ folder.

Find the lines that currently look like:

  ...
  if REQUEST.has_key('disable_border'): #short circuit
      return 0
  if REQUEST.has_key('enable_border'): #short circuit
      return 1
  ...

Now replace them with:

  ...
  if REQUEST.has_key('disable_border'): #short circuit
      return 0
  #check for view mode
  if context.portal_membership.isAnonymousUser() or not context.getEditMode(): #short circuit
      return 0
  if REQUEST.has_key('enable_border'): #short circuit
      return 1
  ...

Now you have all the code you need, by accessing any URL with the /toggleEditMode method you can switch between modes. i.e. www.yourportal.com/path/to/page/toggleEditMode

Naturally, this is not going to be practical so I usually add an action into the personal menu to make this easy to do. You can of course add an action or just a link elsewhere if that makes more sense in your design.

In the portal_membership tool add a new action like this:

  Name       : Toggle Mode
  Id         : togglemode
  Action     : string:${object_url}/toggleEditMode
  Condition  : member
  Permission : View
  Category   : user

Now you will see a new item Toggle Mode in your personal menu, clicking on this will toggle modes for you while leaving you on the current page.

I've done this on quite a few sites now and it has been working fine for some time. I've set the cookies to be persistant forever but you may want to change it so the cookies expire after each session. Let me know if you discover any bugs.

Tom Cameron - tom at mooball.com


Contribute

Something wrong or out of date? Anybody can edit or create a new article in the knowledge base. Simply create an account on this site, log in, and click the Edit button to contribute.