I have a vocabulary source that gets a list of simple terms comprised of UUID/Title pairs.
Based on plone documentation that shows dynamic vocabularies, the code I have is:
@provider(IContextSourceBinder)
def organizations(context):
root = context.portal_url.getPortalObject()
portal_catalog = root.portal_catalog
brains = portal_catalog.searchResults(portal_type='Organization')
result = [ (brain["UID"], brain["Title"]) for brain in brains]
terms = make_terms(result)
return SimpleVocabulary(terms)
And then I have a content type, MyPhone, that has uses the source in a choice field.
In the interface I have:
class IMyPhone(model.Schema):
form.widget('manufacturer',SelectFieldWidget)
manufacturer = schema.Choice(
title=u"Manufacturer",
source=organizations,
required=False,
)
I want to store the title of the manufacturer in a metadata column, so in my catalog.xml, I have:
<column value="Manufacturer"/>
I don't know how to store this. I figured maybe I could do something like:
from plone.uuid.utils import uuidToObject
...
@implementer(IMyPhone)
class MyPhone(Container):
def Manufacturer(self):
return uuidToObject(self.manufacturer).title
Note: Manufacturer is capitalized, along the lines of the title field in metadata being capitalized as 'Title'
This appears to work at first. I go into the ZMI, goto portal_catalog, and go to the MyPhone, and the 'Manufacturer' metadata column is filled in. Unfortunately, it breaks when I try to rebuild the catalog. It duplicates indexes.
2020-02-20 09:37:25,688 ERROR [Products.ZCatalog:99][waitress] A different
document with value '2409cca287ab4a20bc17240bdae48d84' already exists in the index.
Its duplicating all the Organization indexes. I tried a print statement in the vocabulary source:
@provider(IContextSourceBinder)
def organizations(context):
...
brains = portal_catalog.searchResults(portal_type='Organization')
result = [ (brain["UID"], brain["Title"]) for brain in brains]
for k,v in result:
print(k,v)
...
It was printing the organizations twice like this:
bad4dbf922a844ba84728a401bce0e98 AT&T
2409cca287ab4a20bc17240bdae48d84 Verizon
bad4dbf922a844ba84728a401bce0e98 AT&T
2409cca287ab4a20bc17240bdae48d84 Verizon
And because of duplicates, the vocabulary breaks.
I'm assuming its because of the uuidToObject querying the portal_catalog. Is there any way I could just get the title of the Organization chosen as the Manufacturer to store in the metadata?
The only thing I could think of right now is include a hidden textline 'Manufacturer' in IMyPhone that updates during certain events.