Skip to content

Commit 745e130

Browse files
committed
Handle placeholder attribute better
1 parent 2e4e98f commit 745e130

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

Tests/Translation/Extractor/File/Fixture/MyFormType.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,16 @@ public function buildForm(FormBuilder $builder, array $options)
9393
'translation_domain' => false,
9494
))
9595
;
96+
97+
$builder->add('untranslateable_field_with_placeholder', 'text', array(
98+
'label' => 'field.with.placeholder.no.translation.domain',
99+
'attr' => array('placeholder' => /** @Desc("Field with a placeholder value") */ 'form.placeholder.text.skip'),
100+
'translation_domain' => false,
101+
));
102+
103+
$builder->add('custom_domain_field_with_placeholder', 'text', array(
104+
'attr' => array('placeholder' => 'form.custom_domain_field_with_placeholder.attr.placeholder'),
105+
'translation_domain' => 'custom_domain_field_with_placeholder',
106+
));
96107
}
97108
}

Tests/Translation/Extractor/File/FormExtractorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ public function testExtract()
160160
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 84));
161161
$expected->add($message);
162162

163+
$message = new Message('form.custom_domain_field_with_placeholder.attr.placeholder', 'custom_domain_field_with_placeholder');
164+
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 104));
165+
$expected->add($message);
166+
163167
$this->assertEquals($expected, $this->extract('MyFormType.php'));
164168
}
165169

Translation/Extractor/File/FormExtractor.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ class FormExtractor implements FileVisitorInterface, LoggerAwareInterface, NodeV
7777
*/
7878
private $defaultDomainMessages;
7979

80+
/**
81+
* @var Node
82+
*/
83+
private $attrNode;
84+
8085
/**
8186
* FormExtractor constructor.
8287
* @param DocParser $docParser
@@ -114,6 +119,10 @@ public function enterNode(Node $node)
114119
}
115120

116121
if ($node instanceof Node\Expr\Array_) {
122+
if ($node === $this->attrNode) {
123+
// Skip attr node since we already parsed that.
124+
return;
125+
}
117126
// first check if a translation_domain is set for this field
118127
$domain = $this->getDomain($node);
119128
$choiceDomain = $this->getChoiceDomain($node);
@@ -148,6 +157,9 @@ public function enterNode(Node $node)
148157
$this->parseItem($item, $domain);
149158
break;
150159
case 'attr':
160+
if (isset($item->value)) {
161+
$this->attrNode = $item->value;
162+
}
151163
if ($this->parseAttrNode($item, $domain)) {
152164
continue 2;
153165
}

0 commit comments

Comments
 (0)