I am working drupal 8 site. In this site I create a contact form for contact page. I am able to translate field label but I want to translate my placeholder.I can't find any option for translate contact forms placehoder in drupal 8.
-
See drupal.stackexchange.com/a/191927/47547– 4uk4Commented Feb 3, 2017 at 7:36
-
Placeholder value is a widget setting, see this core issue: drupal.org/node/2546212– BerdirCommented Feb 3, 2017 at 19:55
-
@4k4 I am asking about placeholder not field label.– Pankaj YogiCommented Feb 4, 2017 at 5:10
-
@Berdir I using contact form not Entity form.– Pankaj YogiCommented Feb 4, 2017 at 5:13
-
Contact forms in Drupal 8 are entity forms.– BerdirCommented Feb 4, 2017 at 8:58
1 Answer
Normally you should be able to translate the placeholder in configuration translation as described in https://drupal.stackexchange.com/a/191927/47547. @Berdir pointed out that there is an open issue that prevents this: https://www.drupal.org/node/2546212
A workaround is to make the placeholder translatable in a form alter hook:
function mytheme_form_contact_message_feedback_form_alter(&$form, $form_state) {
$form['subject']['widget']['0']['value']['#placeholder'] = t('Please enter a subject');
}
After visiting the translated form you can translate this string in the user interface translation.
If you want to translate all placeholders in the form and use the untranslated strings from the widget settings:
function mytheme_form_contact_message_feedback_form_alter(&$form, $form_state) {
array_walk_recursive($form, function (&$value, $key) {
if ($key === '#placeholder' && is_string($value) && $value !== '') {
$value = t($value);
}
});
}
-
Thanks It's Working butthis function showing error . I just pass placehole in t() function they showing in User interface translation. function mytheme_form_contact_message_feedback_form_alter(&$form, $form_state) { array_walk_recursive($form, function (&$value, $key) { if ($key === '#placeholder' && $value !=='') { $value = t($value); } }); } Commented Feb 4, 2017 at 11:14
-
What was the error? What you've changed to get it working? The code in your comment seems to be the same as in the answer.– 4uk4Commented Feb 4, 2017 at 11:25
-
I am Just use this function function mytheme_form_contact_message_feedback_form_alter(&$form, $form_state) { $form['subject']['widget']['0']['value']['#placeholder'] = t('Please enter a subject'); } Commented Feb 4, 2017 at 11:33
-
I think the error was from a placeholder already containing a translated markup. I've added an extra check.– 4uk4Commented Feb 4, 2017 at 12:49