0

I am trying to style the registration/login forms so far I have copied all the files from \drupal\core\themes\stable\templates\form\ to themes\mytheme\template\form.

Inside this folder, for example there is input.html.twig which has the following code.

{#
/**
 * @file
 * Theme override for an 'input' #type form element.
 *
 * Available variables:
 * - attributes: A list of HTML attributes for the input element.
 * - children: Optional additional rendered elements.
 *
 * @see template_preprocess_input()
 */
#}
<input{{ attributes }} />{{ children }}

My question is how can I add a specific class name to this input, so I can apply styles to it in my css file

1 Answer 1

1

You can use the addClass() method of the attribute class:

{%
  set classes = [
    'my-class-1',
    'my-class-2',
  ]
%}

<input{{ attributes.addClass(classes) }}>

You can either pass an array like the example above or pass a single class in the method directly: attributes.addClass('my-class').

For reference see the Drupal docs: https://www.drupal.org/docs/8/theming-drupal-8/using-attributes-in-templates

2
  • Funny, I was just trying this and it does not work. I had to enclose the the the first portion of the code in {%%} though
    – hidar
    Commented Jul 3, 2017 at 9:34
  • Oh yeah, that's correct. Updated my anwer, thanks!
    – mvdgun
    Commented Jul 3, 2017 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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