Creating Link Types that Take You Directly to 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
repurposethe 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
Symlinktype- change
Initial view nameto link_edit_form (then Save Changes) - click actions tab
- change
Viewaction. action: symlink_view (then Save)
- change
- go back to portal_types
contentstab - click on
Redirecttype- change
Initial view nameto link_edit_form (then Save Changes) - click actions tab
- change
Viewaction. action: redirect_view (then Save)
- change
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
customin the dropdown box and clickcustomizebutton - now you are at the edit screen of the
folder_contentspagetemplate - 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);
- <td tal:define="action python:item.getTypeInfo().getActionById(
- 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.
