Customizing JavaScript pattern settings in Plone 5

There are a couple ways to customize some default pattern options coming in Plone 5. This post is reblogged from Nathan Van Gheem's Blog.

reblogged from www.nathanvangheem.com

Through the web

The most simple way to customize JavaScript pattern settings in Plone 5 is to use the new "Resource Registry" control panel.

The new "Resource Registry" control panel now offers a simple form to provide base settings, in JSON format, for patterns.

../images-used/pattern-options/image_large

A quick example you could try is to customize the minimum number of letters that must be typed before livesearch is activated. To do this, you would add a pattern name of "livesearch" and a JSON value of {"minimumInputLength": 5} to require 5 letters to be input instead of the default, 4.

Add-on package

Addon packages can also customize pattern settings. This is useful when pattern settings might be pulling information from configuration stored in the database which would not be static.

Plone provides adapter registration lookups to render additional pattern settings on the site. To show how this works, we'll go through a simple example.

First off, define the pattern option adapter:

import json
from Products.CMFPlone.interfaces import IPatternsSettings

class PatternSettingsAdapter(object):
    implements(IPatternsSettings)

    def __init__(self, context, request, field):
        self.request = request
        self.context = context
        # some options are pulled from fields
        # when getting called to put in default on the body tag,
        # field will be None
        self.field = field

    def __call__(self):
        # return custom pattern options here
        return {'data-pat-mypattern': json.dumps({'foo': 'bar'})}

And then, wire up your adapter with some zcml:

<adapter for="* * *"
  factory=".mymodule.PatternSettingsAdapter"
  provides="Products.CMFPlone.interfaces.IPatternsSettings"
  name="mymoduel_settings" />

To see how this is currently being used in Plone core, take a look at Products.CMFPlone.patterns.__init__.py(master). Also, to see how these patterns are rendered, you can look at plone.app.layout.globals.pattern_settings.py(master).