0

I try to change a the theme template for one form. I do not want to use theme suggestions as I only want to use a different theme in one case, and if I add a suggestion, it is always used. (maybe I'm wrong.)

I'm doing:

in my controller:

$form = \Drupal::service('entity.form_builder')->getForm($entity);

$form['#theme_wrappers'][] = 'user_register_form_01';

return $form;

in my .module

function mymodule_theme() {
  return [
    'user_register_form_01' => [
      'render element' => 'form',
      'template' => 'user-register-form-01',
    ],
  ];
}

in user-register-form-01.html.twig:

<form{{ attributes }}>

  {{ form.field_1 }}
  {{ form.field_2 }}
  {{ form.account.pass }}
  {{ form.account.mail }}

  {{ form.form_build_id }}
  {{ form.form_id }}
  {{ form.actions }}

</form>

Everything seems to be rendered correctly, but the form does not work/submit. I think it is because attributes does not contain any action information. There is one more attributes variable inside form but I don't know how to call it as when I dump it it is prefixed with #

dump(form|keys) returns

array (size=49)
  0 => string '#attributes' (length=11)
  1 => string '#attached' (length=9)
  ...
  47 => string '#children' (length=9)
  1. I don't know how to access this as form.#attributes doesn't work and form.attributes results empty.

  2. Also this #attributes seems not to contain the needed form submission information. But it seems that inside #children the #attributes does.

I am thankfull for any hint or help.

  • 1
    That would render a <form> within a <form> wouldn't it? That's invalid HTML and might explain the failure to submit properly (you don't need an action or method on a <form> tag, the form will still submit as those attributes have default values). You probably just want $form['#theme'] = 'user_register_form_01'; – Clive Jun 29 '18 at 18:39
  • already tried, if I use $form['#theme'] my template is not used at all – Miguel Jun 29 '18 at 20:23
0

Have you looked into the Context module for this (https://www.drupal.org/project/context)? One of the "reactions" is to change the theme. You can do it by system path.

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.