1

in Drupal 8, I want my form display such a (submit) button:

<button name="name" type="submit" value="value">Not the value</button>

So basically, the button value (that will be submitted) shall be different than the button text / label (following that solution).

Is that possible with the Form API? If yes, how?

2 Answers 2

1

I think this might be a duplicate of a question I posted a few weeks ago for Drupal 8.

Original question: https://drupal.stackexchange.com/a/226494/57

Some more write up: http://kevinquillen.com/drupal/2017/01/28/adding-twig-template-suggestions-for-form-elements

Basically I wanted to target just one submit, hence that technique. Simply changing the #type to button is not enough in the Form API to get that result you're asking for.

5
  • This is interesting. I wonder if I can avoid defining a new twig template. Because interestingly, my Drupal already generates a <button>...</button>. And I could not find out which Twig template it is using. It's obviously not input.html.twig. Do you have an idea where <button> comes from? Feb 10, 2017 at 21:21
  • ... My form is simply defined with $form['go'] = ['#type' => 'submit', ...] Feb 10, 2017 at 21:22
  • Ah sorry, I guess I figured it out. I'm using Bootstrap and there is a input--button.html.twig. :) Feb 10, 2017 at 21:24
  • I figured - I am not using Bootstrap. Drupal core does not provide an input button twig suggestion, themes have to. But otherwise, know that if you overwrite input--button.html.twig it will affect all button submits, which is why I am isolating and creating new suggestions for them. Particularly in my case, where there could be different SVG icons or something totally different depending on the form.
    – Kevin
    Feb 10, 2017 at 21:37
  • Sure, I didn't have in mind to change the template. But knowing now which is used I can hook into the preprocess. ;) Feb 11, 2017 at 0:44
1

Could solve it myself. Since I'm using Bootstrap, I could hook into its button template input__button:

function MYMODULE_preprocess_input__button(&$variables) {

  if (!empty($variables['element']['#label'])) {
    $variables['label'] = $variables['element']['#label'];
  }
}

With that, I can now define a label using the Form API:

$form['submit'] = [
  '#type' => 'submit',
  '#name' => 'some_name',
  '#value' => 'some_value_to_send',
  '#label' => $this->t('Label to show'),
  '#button_type' => 'primary',
];

(If Bootstrap applies my patch then the hook will get obsolete.)

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.