Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help with indexing a field of a subform #69

Closed
b4oshany opened this issue Mar 23, 2018 · 1 comment
Closed

Need help with indexing a field of a subform #69

b4oshany opened this issue Mar 23, 2018 · 1 comment

Comments

@b4oshany
Copy link

b4oshany commented Mar 23, 2018

Is there a way to index a field from a subform?

My aim is to have a user list their educational background, which should be indexed and searchable, in order to find the user by their school(s) when using the Plone's global search box.

I'm using collective.z3cform.datagridfield for the subform.

The snippets below works for rendering and saving the data but it's not showing up in the index.

# Subform
class IEducation(model.Schema):

    dexteritytextindexer.searchable('school')

    school = schema.TextLine(
        title=_(u'School'),
        required=True,
    )
    directives.widget(
        'school',
        AjaxSelectFieldWidget,
        pattern_options={
            "maximumSelectionSize": 1
        },
        vocabulary="my.package.vocabularies.schools"
    )

    from_date = schema.Date(
        title=_(u'From'),
        required=True
    )

    to_date = schema.Date(
        title=_(u'To'),
        required=True
    )
# Parent form
class IMember(model.Schema):

    education = schema.List(
        title=_(u'Education'),
        value_type=DictRow(title=u"education", schema=IEducation),
        required=False
    )
    directives.widget(
        'education',
        BlockDataGridFieldFactory
    )

Reference the index point in the `indexers.py``

@indexer(IEducation)
def school(obj):
    return obj.school

Set the adapter for the school index

  <adapter factory=".indexers.school" name="school" />

Create a dynamic vocabulary for the school index

@provider(IContextSourceBinder)
def available_schools(context):
    catalog = api.portal.get_tool('portal_catalog')
    schools = catalog.uniqueValuesFor('school')
    return SimpleVocabulary([
        SimpleTerm(value=school, title=school) for school in schools
    ])

Register the utility for the dynamic school vocabulary

  <utility
      provides="zope.schema.interfaces.IVocabularyFactory"
      component=".vocabularies.available_schools"
      name="my.package.vocabularies.schools"
      />
@b4oshany b4oshany changed the title Indexing field Indexing field of a subform Mar 23, 2018
@b4oshany b4oshany changed the title Indexing field of a subform Need help with indexing field of a subform Mar 23, 2018
@b4oshany b4oshany changed the title Need help with indexing field of a subform Need help with indexing a field of a subform Mar 23, 2018
@b4oshany
Copy link
Author

It's working now. I changed the indexer to point to IMember and then build a list from the list of dictionary

@indexer(IMember)
def school(obj):
    if not obj.education:
        return []
    return [
        edu['school'] for edu in obj.education
    ]

Credits to the plone community

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant