Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[TwigBridge] Add Twig field_id() form helper
  • Loading branch information
Legendary4226 authored and chalasr committed Mar 5, 2025
commit 5393f81e9a0ce9be5599fe4bfcbd965679bfc1aa
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `is_granted_for_user()` Twig function
* Add `field_id()` Twig form helper function

7.2
---
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/FormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function getFunctions(): array
new TwigFunction('csrf_token', [FormRenderer::class, 'renderCsrfToken']),
new TwigFunction('form_parent', 'Symfony\Bridge\Twig\Extension\twig_get_form_parent'),
new TwigFunction('field_name', $this->getFieldName(...)),
new TwigFunction('field_id', $this->getFieldId(...)),
new TwigFunction('field_value', $this->getFieldValue(...)),
new TwigFunction('field_label', $this->getFieldLabel(...)),
new TwigFunction('field_help', $this->getFieldHelp(...)),
Expand Down Expand Up @@ -93,6 +94,11 @@ public function getFieldName(FormView $view): string
return $view->vars['full_name'];
}

public function getFieldId(FormView $view): string
{
return $view->vars['id'];
}

public function getFieldValue(FormView $view): string|array
{
return $view->vars['value'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public function testFieldName()
$this->assertTrue($this->view->children['username']->isRendered());
}

public function testFieldId()
{
$this->assertSame('register_username', $this->rawExtension->getFieldId($this->view->children['username']));
$this->assertSame('register_choice_multiple', $this->rawExtension->getFieldId($this->view->children['choice_multiple']));
}

public function testFieldValue()
{
$this->assertSame('tgalopin', $this->rawExtension->getFieldValue($this->view->children['username']));
Expand Down
Loading