This document explains how to access view and context utilities in Plone.
IPortalState defines IContextState view-like interfaces to access miscellaneous information useful for the rendering the current page. The views are cached properly, so they should access the information quite effectively.
To see what's available through the interface, read the documentation in the plone.app.layout.globals.interfaces module.
Example showing how to get the portal root URL:
from Acquisition import aq_inner
from zope.component import getMultiAdapter
...
class MyView(BrowserView):
...
def mymethod(self):
context = aq_inner(self.context)
portal_state = getMultiAdapter((context, self.request), name=u'plone_portal_state')
url = portal_state.portal_url()
Example showing how to get the current language:
from zope.component import getMultiAdapter
...
portal_state = getMultiAdapter((self.context, self.request), name=u'plone_portal_state')
current_language = portal_state.language()
Example showing how to expose portal_state helper to a template:
<browser:page
for="*"
name="test"
permission="zope2.Public"
class=".views.MyView"
allowed_attributes="portal_state"
/>
A Python class exposes the variable:
from Acquisition import aq_inner
from zope.component import getMultiAdapter
class MyView(BrowserView):
def portal_state(self):
context = aq_inner(self.context)
portal_state = getMultiAdapter((context, self.request), name=u'plone_portal_state')
return portal_state
Template can use it:
<div>
The language is <span tal:content="view/portal_state/language" />
</div>
You can directly look up portal_state in templates using acquisition and view traversal, without need of ZCML code or Python view code changes. This is useful e.g. in overridden viewlet templates:
<!--
During traversal, ``@@`` signals that the traversing
machinery should look up a view by that name.
First we look up the view and then use
it to access the variables defined in
``IPortalState`` interface.
-->
<div tal:define="portal_state context/@@plone_portal_state" >
The language is <span tal:content="portal_state/language" />
</div>
You can use IContextState and IPortalState in TALES expressions, e.g. portal_actions as well.
Example portal_actions conditional expression:
python:object.restrictedTraverse('@@plone_portal_state').language() == 'fi'
Tools are persistent utility classes available in the site root. They are visible in the ZMI, and sometimes expose useful information or configuration here. Tools include e.g.:
plone.app.layout.globals.interfaces.ITools interface and Tools BrowserView provide cached access for the most commonly needed tools.
ITools is mapped as the plone_tools view for traversing.
Example:
from Acquisition import aq_inner
from zope.component import getMultiAdapter
context = aq_inner(self.context)
tools = getMultiAdapter((context, self.request), name=u'plone_tools')
portal_url = tools.url()
# The root URL of the site is got by using portal_url.__call__()
# method
the_current_root_url_of_the_site = portal_url()
getToolByName is the old-fashioned way of getting tools, using the context object as a starting point. It also works for tools which do not implement the ITools interface.
getToolByName gets any Plone portal root item using acquisition.
Example:
from Products.CMFCore.WorkflowCore import WorkflowException
# Do the workflow transition "submit" for the current context
workflowTool = getToolByName(self.context, "portal_workflow")
workflowTool.doActionFor(self.context, "submit")
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.
For basic information about updating this manual and Sphinx format please see Writing and updating the manual guide.