I have file in src/Form/TestForm.php.

I would like to add link of Forgot password in buildForm function & want to display in twig template file.

How to achieve this?

  • 1
    Elements inside the form should be added via the buildForm method and not in twig.
    – Eyal
    Oct 3, 2017 at 5:22
  • 1
    Also, the TestForm.php file should be placed under src/Form/ and not src/form/
    – Eyal
    Oct 3, 2017 at 5:23
  • If you are having troubles then i suggest a tutorial such as valuebound.com/resources/blog/…
    – Eyal
    Oct 3, 2017 at 5:26
  • Sorry @Eyal it's typo mistake in form. I already created Form. I just wanted to pass link from TestForm.php to Twig template
    – user9589
    Oct 3, 2017 at 6:20

2 Answers 2


You should add the link in the form builder, something like the following example:.

$forgot_password_link = \Drupal\Core\Link::createFromRoute($this->t('Password link'), 'user.pass')->toRenderable();
$form['forgot_password_link'] = [
  '#markup' => render($forgot_password_link),

You can add links in Twig directly:

<a href="{{ path('user.pass') }}">{{ 'Forgot password?'|t }}</a>

see https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates

Your Answer

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