Skip to content

Commit

Permalink
add option for StaticCatalogVocabulary instead of dynamic one
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTango committed Jan 15, 2025
1 parent 9fd4df8 commit 780d75a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
8 changes: 8 additions & 0 deletions bobtemplates/plone/vocabulary/.mrbob.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ vocabulary_name.required = True
vocabulary_name.default = AvailableThings
vocabulary_name.post_ask_question = bobtemplates.plone.base:check_klass_name

is_static_catalog_vocab.question = Is the vocabulary a StaticCatalogVocabulary?
is_static_catalog_vocab.help = StaticCatalogVocabulary is often used for Choice fields where the values are resolved by relations.
is_static_catalog_vocab.required = True
is_static_catalog_vocab.default = n
is_static_catalog_vocab.post_ask_question = mrbob.hooks:validate_choices mrbob.hooks:to_boolean
is_static_catalog_vocab.choices = y|n
is_static_catalog_vocab.choices_delimiter = |


[template]
pre_render = bobtemplates.plone.vocabulary:prepare_renderer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
# -*- coding: utf-8 -*-

# from plone import api
from zope.schema.interfaces import IVocabularyFactory
{{% if is_static_catalog_vocab %}}
from plone.app.vocabularies.catalog import StaticCatalogVocabulary
{{% else %}}
from {{{package.dottedname}}} import _
from plone.dexterity.interfaces import IDexterityContent
from zope.globalrequest import getRequest
from zope.interface import implementer
from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary
{{% endif %}}


{{% if not is_static_catalog_vocab %}}
class VocabItem(object):
def __init__(self, token, value):
self.token = token
self.value = value
{{% endif %}}


@implementer(IVocabularyFactory)
Expand All @@ -22,6 +28,18 @@ class {{{ vocabulary_name_klass }}}(object):
"""

def __call__(self, context):
{{% if is_static_catalog_vocab %}}
return StaticCatalogVocabulary(
{
# possible portal_types:
"portal_type": [
"Event",
]
},
# customizable title of the Choice items, by default brain.Title:
title_template="{brain.Title}",
)
{{% else %}}
# Just an example list of content for our vocabulary,
# this can be any static or dynamic data, a catalog result for example.
items = [
Expand All @@ -47,6 +65,7 @@ class {{{ vocabulary_name_klass }}}(object):
)
# Create a SimpleVocabulary from the terms list and return it:
return SimpleVocabulary(terms)
{{% endif %}}


{{{ vocabulary_name_klass }}}Factory = {{{ vocabulary_name_klass }}}()

0 comments on commit 780d75a

Please sign in to comment.