2

I'm trying to change a label of Drupal 8's contact form ('Your email address'). The label is defined /core/modules/contact/src/MessageForm.php, but obviously I don't want to change this in core.

$form['mail'] = array(
'#type' => 'email',
'#title' => $this->t('Your email address'),
'#required' => TRUE,
);

In Drupal 7 I always used string overrides but the module isn't available for Drupal 8 yet. How can string be overridden in Drupal 8?

3
  • 1
    It looks like there is a usable version of this module for Drupal 8, according to this post from a year ago: drupal.org/node/2236791
    – Kevin
    Apr 3, 2017 at 20:28
  • @Kevin Thanks. Looks like it isn't even alpha though - will try the custom method first.
    – rkhff
    Apr 3, 2017 at 21:17
  • This is an example of a question where we may have a duplicate already for how to override a string.
    – mpdonadio
    Apr 3, 2017 at 23:07

2 Answers 2

5

The easiest method would be to implement a hook_form_alter inside a custom module that overrides the title

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21form.api.php/function/hook_form_alter/8.2.x

Some information how to create custom module.

https://www.drupal.org/docs/8/creating-custom-modules

3
  • 2
    This hook can also be used from the theme layer, too.
    – Kevin
    Apr 3, 2017 at 20:24
  • Thanks... and ouch, that's complicated. Lots of reading on creating custom modules to do...
    – rkhff
    Apr 3, 2017 at 21:06
  • @Kevin, correct :) I always forget to mention that Apr 3, 2017 at 21:37
6

You can always override strings in settings.php like this:

$settings['locale_custom_strings_en'][''] = array(
  'Related entities for @group' => 'Add Content to group @group',
);

You can change en to de or whatever two letter language code you like.

Please note: the entire string must exist by itself. For example, the above string override existed in the group module controller like this:

return $this->t('Related entities for @group', ['@group' => $group->label()]);

This will NOT work:

$settings['locale_custom_strings_en'][''] = array(
  'Related entities for ' => 'Add Content to ',
);

So for the example listed in the question, you could use:

$settings['locale_custom_strings_en'][''] = array(
  'Your email address' => 'Your email ID',
);
1
  • Yep, sometimes it is much easy just to add override for core string or config
    – apostnikov
    Nov 27, 2018 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.