1

I have a classic contact form. I can add for example some custom placeholder like this :

function my_theme_form_contact_message_about_your_project_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    // placeholder
    $form['field_contact_name']['widget'][0]['value']['#placeholder'] = t('Saisir votre nom');

} 

It works very well and I can also easily modify wrapper, name, etc.

But for a reason I can’t understand, I am not able to modify the text of the submit button.

Here is what I’ve tried among a lot of things and I think it should be the right thing (as seen on other answers) :

$form['actions']['submit']['#value'] = 'hello world!';

Here is the html of the button :

<button data-drupal-selector="edit-submit" class="button button--primary js-form-submit form-submit btn-primary btn" type="submit" id="edit-submit" name="op" value="Envoyer">Envoyer</button>

I use a custom theme based on Bootstrap theme.

Of course, I can do it with jQuery :

$('#edit-submit').text('lorem ipsum');

But I would like to know how to do it the Drupal way.

EDIT :

The solution is :

form['actions']['submit']['#value'] = 'hello world!';

It works on a clean Drupal install but not on the website I am currently working on.

1

Do you know why it is not working ?

Because it seems to me that you're playing the guessing game.

How about installing the Devel and adding kint($form) to inspect the $form array?

You will see its

$form['actions']['submit']['#value'] = t('Post Comments');
  • Before asking, I've already tried $form['actions']['submit']['#value'] = t('Post Comments'); (as seen on other answer) and it didn't work. I should have mentioned this in my question. I will install Devel. – Sébastien Gicquel Feb 4 at 16:26
  • Hello you're right. I've already tested your solution before asking. Since, it didn't work, I asked here form more information. So, I've tried on a clean Drupal install and it works. It doesn't work on the website I am currently working on for some specific reason I need to find (I didn't create this web site, only creating a new theme). – Sébastien Gicquel Feb 5 at 11:40
  • I've found the issue on the website : there is a CustomController where I found code about forms and it was overriding my code : $build['contact_form']['actions']['submit']['#value'] = 'lorem ipsum'; Sorry to have disturbed you, not easy to work on a website you don't know. – Sébastien Gicquel Feb 5 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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