Skip to content

Commit 488b10b

Browse files
committed
allow setValue() for SDTs
1 parent 6da9d8a commit 488b10b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

samples/Sample_34_SDT.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@
1515
$textrun = $section->addTextRun();
1616
$textrun->addText('Date: ');
1717
$textrun->addSDT('date');
18+
$textrun->addTextBreak(1);
19+
$textrun->addText('Date with pre set value: ');
20+
$textrun->addSDT('date')->setValue('03/30/2017');
21+
$textrun->addTextBreak(1);
22+
$textrun->addText('Date with pre set value: ');
23+
$textrun->addSDT('date')->setValue('30.03.2017');
1824

1925
$textrun = $section->addTextRun();
2026
$textrun->addText('Drop down list: ');
21-
$textrun->addSDT('dropDownList')->setListItems(array('1' => 'Choice 1', '2' => 'Choice 2'));
27+
$textrun->addSDT('dropDownList')->setListItems(array('1' => 'Choice 1', '2' => 'Choice 2'))->setValue('Choice 1');
2228

2329
// Save file
2430
echo write($phpWord, basename(__FILE__, '.php'), $writers);

src/PhpWord/Writer/Word2007/Element/SDT.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public function write()
4343
}
4444
$type = $element->getType();
4545
$writeFormField = "write{$type}";
46+
$value = $element->getValue();
47+
if ($value === null) {
48+
$value = 'Pick value';
49+
}
4650

4751
$this->startElementP();
4852

@@ -58,7 +62,7 @@ public function write()
5862
// Content
5963
$xmlWriter->startElement('w:sdtContent');
6064
$xmlWriter->startElement('w:r');
61-
$xmlWriter->writeElement('w:t', 'Pick value');
65+
$xmlWriter->writeElement('w:t', $value);
6266
$xmlWriter->endElement(); // w:r
6367
$xmlWriter->endElement(); // w:sdtContent
6468

0 commit comments

Comments
 (0)