1

I have built a form and this is my submit button for it:

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => 'Name',
    '#theme' => 'custom_input'
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $this->t('Submit'),
    '#theme' => 'custom_input'
  );

  $form['#theme'] = 'custom_form_theme' ;

  return $form;
}

My custom_module.module file is:

function custom_module_theme($existing, $type, $theme, $path) {
  return array(
    'custom_form_theme'  => array(
      'render element' => 'form',

    'custom_input' => array(
      'render element' => 'form',
    ),
  );
}

I have a /themes/custom/form/input.html.twig file for inputs and also have a /modules/custom/custom_module/templates/custom_input.html.twig to theme the form I am creating. For now they are identical and I am using twig debug to see what template is rendering the elements. twig code below:

<input{{ attributes.addClass(text) }} />{{ children }}

The name textfield renders correctly and uses my custom modules templates custom_input.html.twig (which is what I want) however for the submit input, it gets rendered twice, firstly using my themes input.html.twig and then using my custom modules custom_input.html.twig so I get 2 button on my page. I dont understand why the submit is rendering twice.

  1. What am I doing wrong?
  2. Why does it render twice instead of overriding the /theme/custom twig?
  • 1
    Not enough code to be able to answer your questions. – Jaypan Feb 2 '17 at 1:04
  • @Jaypan Sorry, updated the question with more info. is that enough or do you need more? – harnamc Feb 2 '17 at 1:50
  • have a look at my answer here, may help. – No Sssweat Feb 2 '17 at 1:51
  • With your updated code and explanation, I think I see the problem. As you said, both your templates are being called. This is because you are rendering an input, which will call input.html.twig, and because you've also added a #theme, which calls custom_input.html.twig. You'll may need to define a new custom element type, rather than using #theme, to do what you want. – Jaypan Feb 2 '17 at 1:59
  • @Jaypan thanks for the answer. Do you have a rough code that you can share? – harnamc Feb 2 '17 at 8:15

Your Answer

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

Browse other questions tagged or ask your own question.