Skip to content

Commit aef36c2

Browse files
committed
Allow autocomplete for other field types, specify rules for field types
1 parent ab1ae4f commit aef36c2

File tree

7 files changed

+109
-18
lines changed

7 files changed

+109
-18
lines changed

Classes/ViewHelpers/String/AutocompleteViewHelper.php

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public function render(): string
2727
{
2828
$field = $this->arguments['field'];
2929

30-
[$autocompleteTokens, $token, $section, $type, $purpose]
30+
[$fieldType, $autocompleteTokens, $token, $section, $type, $purpose]
3131
= [
32+
$field->getType(),
3233
'',
3334
$field->getAutocompleteToken(),
3435
trim($field->getAutocompleteSection()),
@@ -41,6 +42,10 @@ public function render(): string
4142
return $token;
4243
}
4344

45+
if (!$this->tokenIsAllowedForFieldType($token, $fieldType)) {
46+
return '';
47+
}
48+
4449
// Optional section token must begin with the string 'section-'
4550
if (!empty($section)) {
4651
if ($this->tokenIsAllowedForSection($token)) {
@@ -57,7 +62,7 @@ public function render(): string
5762

5863
// Optional purpose token is only allowed for certain autofill-field tokens
5964
if (!empty($purpose)) {
60-
if ($this->tokenIsAllowedForPurpose($token, $purpose)) {
65+
if ($this->tokenIsAllowedForPurpose($token, $purpose, $fieldType)) {
6166
$autocompleteTokens .= $purpose . ' ';
6267
}
6368
}
@@ -67,6 +72,11 @@ public function render(): string
6772

6873

6974
/**
75+
* Checks if the given type token is allowed for the specified autocomplete field token.
76+
*
77+
* Based on WHATWG HTML Spec:
78+
* https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill
79+
*
7080
* @param string $token
7181
* @param string $type
7282
*
@@ -75,35 +85,95 @@ public function render(): string
7585
protected function tokenIsAllowedForType(string $token, string $type): bool
7686
{
7787
$allowedTypes = ['shipping', 'billing'];
78-
$tokensNotSupportingType = ['nickname', 'sex', 'impp', 'url', 'organization-title', 'tel-country-code', 'tel-area-code', 'tel-national', 'tel-local', 'tel-local-prefix', 'tel-local-suffix', 'tel-extension', 'username', 'new-password', 'current-password', 'one-time-code', 'bday', 'bday-day', 'bday-month', 'bday-year', 'language', 'photo'];
88+
$tokensNotSupportingType = [
89+
'nickname', 'sex', 'impp', 'url', 'organization-title',
90+
'tel-country-code', 'tel-area-code', 'tel-national', 'tel-local',
91+
'tel-local-prefix', 'tel-local-suffix', 'tel-extension',
92+
'username', 'new-password', 'current-password', 'one-time-code',
93+
'bday', 'bday-day', 'bday-month', 'bday-year', 'language', 'photo'
94+
];
7995
return in_array($type, $allowedTypes)
8096
&& !in_array($token, $tokensNotSupportingType);
8197
}
8298

83-
8499
/**
100+
* Checks if the given purpose token is allowed for the specified autocomplete field token.
101+
*
102+
* Based on WHATWG HTML Spec:
103+
* https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill
104+
*
85105
* @param string $token
86106
* @param string $purpose
107+
* @param string $fieldType
87108
*
88109
* @return bool
89110
*/
90-
protected function tokenIsAllowedForPurpose(string $token, string $purpose): bool
111+
protected function tokenIsAllowedForPurpose(string $token, string $purpose, string $fieldType): bool
91112
{
92113
$allowedPurposes = ['home', 'work', 'mobile', 'fax', 'pager'];
93114
$tokensSupportingPurpose = ['tel', 'email', 'impp'];
115+
$purposeAllowedForFields = ['input', 'textarea', 'hidden'];
94116

95-
return in_array($token, $allowedPurposes, true)
96-
&& !in_array($token, $tokensSupportingPurpose, true);
117+
return in_array($fieldType, $purposeAllowedForFields)
118+
&& in_array($purpose, $allowedPurposes, true)
119+
&& in_array($token, $tokensSupportingPurpose, true);
97120
}
98121

99122
/**
123+
* Checks if the given autocomplete field token allows a section token prefix.
124+
*
125+
* Based on WHATWG HTML Spec:
126+
* https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill
127+
*
100128
* @param string $token
101129
*
102130
* @return bool
103131
*/
104132
protected function tokenIsAllowedForSection(string $token): bool
105133
{
106-
$tokensNotSupportingSection = ['nickname', 'sex', 'impp', 'url', 'organization-title', 'username', 'new-password', 'current-password', 'one-time-code', 'bday', 'bday-day', 'bday-month', 'bday-year', 'language', 'photo'];
134+
$tokensNotSupportingSection = [
135+
'nickname', 'sex', 'impp', 'url', 'organization-title',
136+
'username', 'new-password', 'current-password', 'one-time-code',
137+
'bday', 'bday-day', 'bday-month', 'bday-year', 'language', 'photo'
138+
];
107139
return !in_array($token, $tokensNotSupportingSection, true);
108140
}
141+
142+
/**
143+
* Checks if the given autocomplete field token is allowed for the current field type.
144+
*
145+
* Based on WHATWG HTML Spec:
146+
* https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill
147+
*
148+
* @param string $token
149+
* @param string $fieldType
150+
*
151+
* @return bool
152+
*/
153+
protected function tokenIsAllowedForFieldType(string $token, string $fieldType): bool
154+
{
155+
$allowedForAllTypes = ['on', 'off'];
156+
$allowedForSelect = ['country', 'country-name', 'language', 'sex', 'bday', 'bday-day', 'bday-month', 'bday-year', 'title', 'address-level1', 'address-level2', 'cc-exp-month', 'cc-exp-year'];
157+
$allowedForLocation = ['country', 'country-name', 'street-address', 'postal-code', 'address-line1', 'address-line2', 'address-line3', 'address-level1', 'address-level2', 'address-level3', 'address-level4'];
158+
$allowedForCountry = ['country', 'country-name'];
159+
$allowedForHidden = ['name', 'honorific-prefix', 'given-name', 'additional-name', 'family-name', 'honorific-suffix', 'email', 'username', 'organization', 'organization-title', 'country', 'country-name', 'language'];
160+
$allowedForPassword = ['new-password', 'current-password'];
161+
162+
switch ($fieldType) {
163+
case 'input':
164+
case 'textarea':
165+
//allow all
166+
return true;
167+
case 'location':
168+
return in_array($token, $allowedForAllTypes, true) || in_array($token, $allowedForLocation, true);
169+
case 'select':
170+
return in_array($token, $allowedForAllTypes, true) || in_array($token, $allowedForSelect, true);
171+
case 'country':
172+
return in_array($token, $allowedForAllTypes, true) || in_array($token, $allowedForCountry, true);
173+
case 'hidden':
174+
return in_array($token, $allowedForAllTypes, true) || in_array($token, $allowedForHidden, true);
175+
case 'password':
176+
return in_array($token, $allowedForAllTypes, true) || in_array($token, $allowedForPassword, true);
177+
}
178+
}
109179
}

Configuration/TCA/tx_powermail_domain_model_field.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
Field::TABLE_NAME . '.validation_title;21, ' .
6161
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
6262
Field::TABLE_NAME . '.prefill_title;33, ' .
63+
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
64+
Field::TABLE_NAME . '.palette.autocomplete;50, ' .
65+
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
66+
Field::TABLE_NAME . '.palette.autocomplete_additional;51, ' .
6367
'--palette--;Layout;41, ' .
6468
'description, ' .
6569
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
@@ -91,6 +95,10 @@
9195
'--palette--;Layout;43, ' .
9296
'description, ' .
9397
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
98+
Field::TABLE_NAME . '.palette.autocomplete;50, ' .
99+
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
100+
Field::TABLE_NAME . '.palette.autocomplete_additional;51, ' .
101+
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
94102
Field::TABLE_NAME . '.marker_title;5, ' .
95103
'--div--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:tabs.access, ' .
96104
'sys_language_uid, l10n_parent, l10n_diffsource, hidden, starttime, endtime';
@@ -104,6 +112,10 @@
104112
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
105113
Field::TABLE_NAME . '.prefill_title;31, ' .
106114
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
115+
Field::TABLE_NAME . '.palette.autocomplete;50, ' .
116+
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
117+
Field::TABLE_NAME . '.palette.autocomplete_additional;51, ' .
118+
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
107119
Field::TABLE_NAME . '.marker_title;5, ' .
108120
'--div--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:tabs.access, ' .
109121
'sys_language_uid, l10n_parent, l10n_diffsource, hidden, starttime, endtime';
@@ -121,6 +133,10 @@
121133
'--palette--;Layout;43, ' .
122134
'description, ' .
123135
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
136+
Field::TABLE_NAME . '.palette.autocomplete;50, ' .
137+
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
138+
Field::TABLE_NAME . '.palette.autocomplete_additional;51, ' .
139+
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
124140
Field::TABLE_NAME . '.marker_title;5, ' .
125141
'--div--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:tabs.access, ' .
126142
'sys_language_uid, l10n_parent, l10n_diffsource, hidden, starttime, endtime';
@@ -136,6 +152,10 @@
136152
'--palette--;Layout;43, ' .
137153
'description, ' .
138154
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
155+
Field::TABLE_NAME . '.palette.autocomplete;50, ' .
156+
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
157+
Field::TABLE_NAME . '.palette.autocomplete_additional;51, ' .
158+
'--palette--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:' .
139159
Field::TABLE_NAME . '.marker_title;5, ' .
140160
'--div--;LLL:EXT:powermail/Resources/Private/Language/locallang_db.xlf:tabs.access, ' .
141161
'sys_language_uid, l10n_parent, l10n_diffsource, hidden, starttime, endtime';
@@ -1047,7 +1067,7 @@
10471067
],
10481068
'displayCond' => [
10491069
'AND' => [
1050-
'FIELD:type:IN:input,textarea',
1070+
'FIELD:type:IN:input,textarea,password,select,country,location,hidden',
10511071
'FIELD:autocomplete_token:!IN:on,off,nickname,sex,impp,url,organization-title,username,new-password,current-password,one-time-code,bday,bday-day,bday-month,bday-year,language,photo',
10521072
'FIELD:autocomplete_token:REQ:true',
10531073
],
@@ -1066,7 +1086,7 @@
10661086
],
10671087
'displayCond' => [
10681088
'AND' => [
1069-
'FIELD:type:IN:input,textarea',
1089+
'FIELD:type:IN:input,textarea,select,country,location,hidden',
10701090
'FIELD:autocomplete_token:!IN:on,off,nickname,sex,impp,url,organization-title,tel-country-code,tel-area-code,tel-national,tel-local,tel-local-prefix,tel-local-suffix,tel-extension,username,new-password,current-password,one-time-code,bday,bday-day,bday-month,bday-year,language,photo',
10711091
'FIELD:autocomplete_token:REQ:true',
10721092
],
@@ -1089,7 +1109,7 @@
10891109
],
10901110
'displayCond' => [
10911111
'AND' => [
1092-
'FIELD:type:IN:input,textarea',
1112+
'FIELD:type:IN:input,textarea,select,country,location,hidden',
10931113
'FIELD:autocomplete_token:IN:email,impp,tel',
10941114
],
10951115
],
@@ -1124,12 +1144,7 @@
11241144
'allowLanguageSynchronization' => true,
11251145
],
11261146
],
1127-
'displayCond' => [
1128-
'OR' => [
1129-
'FIELD:type:=:input',
1130-
'FIELD:type:=:textarea',
1131-
],
1132-
],
1147+
'displayCond' => 'FIELD:type:IN:'.$fieldsWithAutocompleteOptions,
11331148
],
11341149
],
11351150
];

Resources/Private/Partials/Form/Field/Country.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
prependOptionLabel="{f:translate(key:'pleaseChoose')}"
1717
class="powermail_country {settings.styles.framework.fieldClasses} {vh:validation.errorClass(field:field, class:'powermail_field_error')}"
1818
value="{vh:misc.prefillField(field:field, mail:mail)}"
19+
autocomplete="{vh:string.autocomplete(field:field)}"
1920
additionalAttributes="{vh:validation.validationDataAttribute(field:field)}"
2021
id="powermail_field_{field.marker}" />
2122
</div>

Resources/Private/Partials/Form/Field/Hidden.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
id="powermail_field_{field.marker}"
44
property="{field.marker}"
55
value="{vh:misc.prefillField(field:field, mail:mail)}"
6-
class="powermail_hidden {field.css} powermail_{field.marker}" />
6+
class="powermail_hidden {field.css} powermail_{field.marker}"
7+
autocomplete="{vh:string.autocomplete(field:field)}" />

Resources/Private/Partials/Form/Field/Location.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
property="{field.marker}"
1111
value="{vh:misc.prefillField(field:field, mail:mail)}"
1212
additionalAttributes="{data-powermail-location:'prefill',data-powermail-eidurl:'{f:uri.page(absolute:1)}'}"
13+
autocomplete="{vh:string.autocomplete(field:field)}"
1314
class="powermail_location {settings.styles.framework.fieldClasses} {vh:validation.errorClass(field:field, class:'powermail_field_error')}"/>
1415
</div>
1516
</div>

Resources/Private/Partials/Form/Field/Password.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
id="powermail_field_{field.marker}"
99
property="{field.marker}"
1010
value=""
11+
autocomplete="{vh:string.autocomplete(field:field)}"
1112
additionalAttributes="{vh:validation.validationDataAttribute(field:field)}"
1213
class="powermail_password {settings.styles.framework.fieldClasses} {vh:validation.errorClass(field:field, class:'powermail_field_error')}" />
1314
</div>
@@ -25,6 +26,7 @@
2526
id="powermail_field_{field.marker}_mirror"
2627
property="{field.marker}_mirror"
2728
value=""
29+
autocomplete="{vh:string.autocomplete(field:field)}"
2830
additionalAttributes="{vh:validation.passwordValidationDataAttribute(field:field)}"
2931
class="powermail_password {settings.styles.framework.fieldClasses} {vh:validation.errorClass(field:field, class:'powermail_field_error')}" />
3032
</div>

Resources/Private/Partials/Form/Field/Select.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
property="{field.marker}"
1010
class="powermail_select {settings.styles.framework.fieldClasses} {vh:validation.errorClass(field:field, class:'powermail_field_error')}"
1111
id="powermail_field_{field.marker}"
12+
autocomplete="{vh:string.autocomplete(field:field)}"
1213
additionalAttributes="{vh:validation.validationDataAttribute(field:field)}"
1314
multiple="{field.multiselectForField}"
1415
value="{vh:misc.prefillField(field:field, mail:mail)}" />

0 commit comments

Comments
 (0)