Announcing the Field Formatter Pattern module
A contributed module to add custom HTML pattern attribute to the text fields.
The Field Formatter Pattern module provides the option to the site builders to add custom HTML pattern attribute to the text fields under the Manage Form Display settings page. For example, you can enforce the content editors to provide the name of a taxonomy term in a particular format.
In one of our projects, we had a controlled vocabulary with a required a field_key field. We needed the value of the key to be in an appropriate format so that we could enforce certain naming conventions. In aour case we wanted to enforce the "audience:" prefix for the key, eg. audience:youth
We thought of doing it with altering the taxonomy add form using hook_form_FORM_ID_alter and allow the field values in the certain format. As this requirement was very common and it could not be specific to a single taxonomy field. The need may arise for the content types fields. We therefore decided to look for a more generic solution.
In order to make this genric and not rely on specific taxonomy term form ID. We decided to leverage the Field API and make it available across multiple entities.
- We added the
hook_field_widget_third_party_settings_formso that Site builders could define the Pattern and Pattern error message for that field.
- We added
hook_field_widget_form_alterso to add the pattern and pattern error message attribute could be added to the entity add form
How to Implement the Field Formatter Pattern module
Using the Field Formatter Pattern Drupal module is very simple as other contributed and core Drupal modules. The module can be installed by following steps:
- Installation: Install the Drupal Field Formatter Pattern module from here and install the module in the same way as you would install any contributed Drupal module.
- Enabling:You will find that Field Formatter Pattern along with other Fields modules. Let's enable it.
- Mange Form Display section in the Content type: Navigate to Administration > Structure > Content types [Content type to edit] > Manage form display. Select the contextual links icon for the desired field and enter the Pattern and pattern error message and save the form.
How it works?
- Add a taxonomy term: Administration > Structure > Taxonomy > Audience > Add
- Try adding no-allowed value: Now if any content editor try to add non-allowed value. It shows the HTML5 error to provide the value in the right format.
Give it a try and let us know what you think in the issue queue.