Question

Searching for a way to keep a registry of views and named adapters in Plone 4 in a purely ZCML or grok manner, without making registerSomething(myClass).

For example, if I have a main product, which needs a vocabulary of names for all named (multi)adapters of certain nature. The product can provide a couple of adapters, but other adapters can come from other products (third party), which should not be the main product's dependency.

The obvious way is to make some kind of registry, like PloneArticle's one for keeping track of page models (registerArticleModel(MyPageModel)), but I hope there is a cleaner way.

The question is, is there purely ZCML/grok way to achieve the registry effect? This way, when the third party product makes it's own adapter available, the "registry" would automatically pick it up and show in a vocabulary. For example, by querying component system for all adapters with certain signature.

Of course, the "registry" should not be persistent. Main product will use fallbacks in case of missing adapters/views.

Performance is not an issue, but linear search through all adapters for compiling vocabulary is not acceptable solution.

UPDATE: to make this more concrete, consider the following use case with views:

  <browser:page
  name="content-item-plain"
  for="*"
  class=".content_item.ContentItem"
  permission="zope2.View"
  layer="my.model.browser.interfaces.IMyModelLayer"
  template="templates/content_item_plain.pt"
  />

  <browser:page
  name="content-item-another"
  for="*"
  class=".content_item.ContentItem"
  permission="zope2.View"
  layer="my.model.browser.interfaces.IMyModelLayer"
  template="templates/content_item_another.pt"
  />

...

In the template:

<-- config/layout may contain '@@content-item-another' or '@@content-item-plain' 
from the vocabulary -->

<tal:def define="layout config/layout"
     <metal:item use-macro="context/?layout/html" />
</tal:def>

Where context is almost any content type.

Does this approach make sense? If it does, how can I find all those view names for the dictionary?

Was it helpful?

Solution

I have probably overlooked the following part in the zope.component docs http://pypi.python.org/pypi/zope.component

Sometimes you want to know all adapters that are available. Let's say you want to know about all the adapters that convert a German to a US socket type:

>>> sockets = list(zope.component.getAdapters((bathroomDE,), IUSSocket))
>>> len(sockets)
3
>>> names = [name for name, socket in sockets]
>>> names.sort()
>>> names
[u'', u'dvd', u'shaver']

This seems to answer my question on the (non-multi) adapters' side. Views aren't covered still.

I can make view-to-view named adapter, but then I can't override index with template in the ZCML, which is important...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top