Attention

This document was written for an unsupported version of Plone, Plone 2.0.x, and was last updated 1240 days ago.

For more information, see the version support policy.

To learn how to upgrade to the current version of Plone, read the upgrade manual.

Creating Link Types that Take You Directly to the Link

by Alan Runyan last modified Dec 30, 2008 03:01 PM
Contributors: Joel Burton, dancam
How to create new content types based on the built-in link type that behave slightly differently, taking you directly to a link, rather than taking you to a page about the link.

NOTE: This kind of functionality is included in Plone 2.1.3 and 2.5 or later. If you create a link, people will get sent directly to its target when clicked. If you are the Owner of the item, you will be sent to the view page so you can edit it, though. — Alexander Limi

What are those link objects for in CMF/Plone? You can use them to point to internal or external sources. You can specialize the link into being a simple symlink or even a HTTP redirect. Here is a quick cookbook on how to do it with your current plone.

  • First we repurpose the Link object
    • goto portal_types
    • in the Select type to add... box
      • select Factory-based Type Info
      • id: Symlink
      • use default type information: CMFDefault: Link
    • in the Select type to add... box
      • select Factory-based Type Info
      • id: Redirect
      • use default type information: CMFDefault: Link

Now you have created your new content classes based off of Link class and its default Factory Type Information.

  • click on newly created Symlink type
    • change Initial view name to link_edit_form (then Save Changes)
    • click actions tab
    • change View action. action: symlink_view (then Save)
  • go back to portal_types contents tab
  • click on Redirect type
    • change Initial view name to link_edit_form (then Save Changes)
    • click actions tab
    • change View action. action: redirect_view (then Save)

Now we have changed it so their default view actions will goto different methods. As you saw the original action went to link_view which is a template in portal_skins/plone_content/link_view. Now we will create 2 python scripts that will perform like a symlink or redirect based on the info we enter as the url attribute for the newly created content types.

  • click on portal_skins and then into the custom folder
  • in the Select type to add... box
    • select (Script) Python
    • id: symlink_view
    • parameters:
    • body:

      return context.restrictedTraverse(context.remote_url)()

    • click save
  • in the Select type to add... box
    • select (Script) Python
    • id: redirect_view
    • parameters:
    • body:

      return context.REQUEST.RESPONSE.redirect(context.remote_url)

Now we have it so that when you view a Symlink it will display the values entered in the url attribute of the Sylink edit form (it reuses the Link edit form). But first we need to fix an oversight so when you click on a object in folder_contents it will bring you to the edit_form instead of the view form - or you wont be able to easily edit the content!

  • click on portal_skins and then into plone_templates folder
  • click on folder_contents
  • select custom in the dropdown box and click customize button
  • now you are at the edit screen of the folder_contents pagetemplate
  • look for the line that says:
    • <td tal:define="action python:item.getTypeInfo().getActionById(view);
    • In Plone 2: <td tal:define="action python:item_typeinfo.getActionById(view);
  • replace it with:
    • <td tal:define="action python:item.getTypeInfo().immediate_view;
    • In Plone 2: <td tal:define="action python:item_typeinfo.immediate_view;

    (this will be in newer plones)

NOTES ON USAGE:

Symlinks traverse the ZODB using restrictedTraverse() method. If you use a absolute path i.e. beginnging with a / you will start traversal at the root of your ZODB (PhysicalPath). So most likely you will want to start it by just specifying a folder below you and traverse from there, i.e. Members/runyaga/presentations/OSCOM/workshop instead of /myplonesite/Members/runyaga/presentations/OSCOM/workshop

Redirects need a fully qualified url, i.e. http://emerging.plone.org/clients/ashford not relative like /clients/ashford.

IDEAS:

You could make it so your view never redirected or traversed to ZODB contents after a POST from edit_form by adding some logic to your new view methods. You could check for REQUEST.has_key(remote_url) or othe values present in the edit form.

Note from dancam

Absent-minded people, we shouldn't forget to give proper Product meta type and check that Plone Folder permits to add our new types :)

Note from Joel Burton

If you do this, keep in mind that content managers will still need some way to get to the link object in order to do things like edit it, change it's workflow, etc. You may have to teach them to add link_view to the end of the URL to get to the link itself.

An alternative approach for this whole howto could be to not create new link types, but to create a single link type that has a field that asks whether this link should redirect you, or take you to the link-view page. The view for this type would then make that choice, rather than forcing you to hard-code this when you create the type.


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.