Skip to content

Commit 9fbde04

Browse files
committed
Handle placeholder attribute better
1 parent afa4f86 commit 9fbde04

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
@@ -115,6 +120,10 @@ public function enterNode(Node $node)
115120
}
116121

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

0 commit comments

Comments
 (0)