Create a Custom Tool
How to create and install your custom tool.
If you need to add code and it is more than a few lines, or uses functionality that is not allowed in a script, or you just happen to like having code in the file system then a tool can be the thing for you.
(There could be better reasons for adding tools like creating a substitute for an existing tool.)
For doing this you need to create a product (another howto?) or you might already have a product that needs the additional code, fine. For the discussion here it is enough to say that a product is python code that is available to Zope in its Products folder. Zope knows about the product and search paths are set accordingly.
Your tools consists of code in a Python class. Create a file MyTool.py in the root folder of your product. The file should look something like this.:
from Products.CMFCore.utils import UniqueObject
from OFS.SimpleItem import SimpleItem
from Globals import InitializeClass
class MyTool (UniqueObject, SimpleItem):
""" MyTool .... """
id = 'my_tool'
meta_type= 'My Utility Tool'
plone_tool = 1
def method(self, args ...):
""" method ... """
pass
InitializeClass(MyTool)
This defines method. Note also that the attribute id is set to
my_tool.
Now you need to initialize it. Add something like this to your
__init__.py file in the root of your product folder. You should also put a file with an icon tool.gif there.:
from Products.CMFCore import utils
import MyTool
tools = ( MyTool.MyTool, )
def initialize(context):
utils.ToolInit('My Tool', tools=tools,
product_name='MySite', icon='tool.gif',
).initialize(context)
This is almost it. After restarting Zope you can now go to the
contents tab in ZMI at your site. In the drop down list you'll now
find My Tool. Select it and add the tool to the site.
You should now see it listed in the root folder of your site, with that little icon you supplied, and you should be able to access it like this, context.my_tool.method from scripts or here/my_tool/method in page templates.
If you want the Tool to be created and accessible for a Product automatically, without needing to add it in the ZMI, then you should change your product's Install function ( Extensions/Install.py ). An example of this is in the PloneLanguageTool, but briefly it can be achieved like so:
def install(self):
# ....
# Check that the tool has not been added using its id
if not hasattr(self, 'my_tool'):
addTool = self.manage_addProduct['ProductName'].manage_addTool
# Add the tool by its meta_type
addTool('My Utility Tool')
# ....
Zope will need to be restarted for these changes to be reflected.
Using GenericSetup
Just add this entry to your 'toolset.xml':
<required tool_id="..."
class="..."/>