0

given a form that is built like the following:

public function buildForm(array $form, FormStateInterface $form_state)
{
   $form['#theme'] = 'mymodule_testForm';
   $form['actions']['#type'] = 'actions';
   $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('submit'),
      '#button_type' => 'primary',
   );
   return $form;
}

How do I access the submit button in my twig template?

  • {{ form.actions.submit }} (assuming 'render element' => 'form' in hook_theme())? – Clive Dec 11 '15 at 0:23
  • But don't count to being able to alter anything in Twig. It's too late for that. If the variable in twig is not preprocessed, like attributes, you won't be able to do anything. I have created an issue for this in do.org already but I doubt anything will be done in 8.x branch. So back to good old hook_preprocess_hook. – user21641 Dec 11 '15 at 15:28
0

Hat tip to Clive who already answered the question as a comment:

{{ form.actions}}

To render the rest of the form, you can do something like {{ form | without('actions') }}

So you whole code will look something like this:

In my_module.module:

/**
 * Implements hook_theme().
 */
function my_module_theme($existing, $type, $theme, $path) {
  $result = [
    'custom_form' => [
      'render element' => 'form',
      'template' => 'custom-form',
    ],
  ];

  return $result;
}

And in custom-form.html.twig:

<div class="col-md-8">
    <h3>Form</h3>
    {{ form | without('actions') }}
</div>
<aside class="col-md-4">
    <h3>Action</h3>
    {{ form.actions }}
</aside>

For more reading:

Hope that helps!

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.