heimrichhannot / contao-form-autocomplete-bundle
This bundle offers functionality for adding the HTML5 attribute "autocomplete" to form fields in the Contao CMS.
Installs: 1 924
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 1
Open Issues: 0
Type:contao-bundle
pkg:composer/heimrichhannot/contao-form-autocomplete-bundle
Requires
- php: ^7.2 || ^8.0
- contao/core-bundle: ^4.9
- heimrichhannot/contao-utils-bundle: ^2.236 || ^3.0
- symfony/config: ^4.4 || ^5.4
- symfony/dependency-injection: ^4.4 || ^5.4
- symfony/http-kernel: ^4.4 || ^5.4
README
This bundle offers functionality for adding the HTML5 attribute autocomplete to form fields in the Contao CMS.
The autocomplete attribute is used in the following cases:
- Browsers use it for presenting different autocompletion options for fields wth different
autocompletevalue set. - Screen readers verbalize it, hence it's a good practice to have them set.
Features
- adds the HTML5
autocompleteattribute to form fields - supports:
- Contao Formgenerator
- all form fields generated by using Widget::parse() (the hook getAttributesFromDca is used)
Installation
- Install via composer:
composer require heimrichhannot/contao-form-autocomplete-bundle.
How does it work?
Form generator
The form generator is supported by default. The autocomplete value is guessed based on the field name.
The value is guessed based on the mapping defined in the bundle config. You can extend this mapping in your project (see chapter below).
Dca fields
There are the following supported ways to add the autocomplete value to a field (the priority is descending):
- The DCA definition has it set in its
evalsection:// tl_my_table.php 'myFirstnameField' => [ 'label' => &$GLOBALS['TL_LANG']['tl_my_table']['myFirstnameField'], // ... 'eval' => [ 'autocomplete' => 'given-name' ] ] - If no DCA definition is found or relevant in the given context (e.g. in form generator there is no DCA context), the bundle tries to guess based on the mapping
of field name to
autocompletevalue defined in this bundle. You can extend this mapping in your project (see chapter below).
Technical details
Extend the given mapping
Simply add the following code according to your needs to your app/config/config.yml (Contao 4.4) or config/config.yml:
huh_form_autocomplete: mapping: given-name: synonyms: - my-new-value new-autocomplete-name: synonyms: - brand-new-autocomplete-name
How does the matching of a field name and an autocomplete value work?
The algorithm is currently very simple. Basically it works the following way:
- If an
autocompletevalue is set in the field's DCA, it's used. - The mapping in
src/Resources/config/config.ymlcontains all the currently possible values of the HTML5autocompletefield. If the field name equals one of these, it's used. - If the step before isn't met, the algorithm iterates the defined synonyms of the
autocompletevalues. If there's a match, the correspondingautocompletevalue is used. - If none of the steps above is met,
falseis returned.
The comparison only takes into account characters. Special characters and separation characters like - or _ are ignored. Hence a synonym some-name equals someName in
this comparison.