Merge pull request #1016 from jun-i-corn/feature/DefaultValueForDropDownSDT
allow setValue() for SDTs
This commit is contained in:
commit
ac4180ef7d
|
|
@ -15,10 +15,16 @@ $textrun->addSDT('comboBox')->setListItems(array('1' => 'Choice 1', '2' => 'Choi
|
||||||
$textrun = $section->addTextRun();
|
$textrun = $section->addTextRun();
|
||||||
$textrun->addText('Date: ');
|
$textrun->addText('Date: ');
|
||||||
$textrun->addSDT('date');
|
$textrun->addSDT('date');
|
||||||
|
$textrun->addTextBreak(1);
|
||||||
|
$textrun->addText('Date with pre set value: ');
|
||||||
|
$textrun->addSDT('date')->setValue('03/30/2017');
|
||||||
|
$textrun->addTextBreak(1);
|
||||||
|
$textrun->addText('Date with pre set value: ');
|
||||||
|
$textrun->addSDT('date')->setValue('30.03.2017');
|
||||||
|
|
||||||
$textrun = $section->addTextRun();
|
$textrun = $section->addTextRun();
|
||||||
$textrun->addText('Drop down list: ');
|
$textrun->addText('Drop down list: ');
|
||||||
$textrun->addSDT('dropDownList')->setListItems(array('1' => 'Choice 1', '2' => 'Choice 2'));
|
$textrun->addSDT('dropDownList')->setListItems(array('1' => 'Choice 1', '2' => 'Choice 2'))->setValue('Choice 1');
|
||||||
|
|
||||||
// Save file
|
// Save file
|
||||||
echo write($phpWord, basename(__FILE__, '.php'), $writers);
|
echo write($phpWord, basename(__FILE__, '.php'), $writers);
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,10 @@ class SDT extends Text
|
||||||
$writeFormField = "write{$type}";
|
$writeFormField = "write{$type}";
|
||||||
$alias = $element->getAlias();
|
$alias = $element->getAlias();
|
||||||
$tag = $element->getTag();
|
$tag = $element->getTag();
|
||||||
|
$value = $element->getValue();
|
||||||
|
if ($value === null) {
|
||||||
|
$value = 'Pick value';
|
||||||
|
}
|
||||||
|
|
||||||
$this->startElementP();
|
$this->startElementP();
|
||||||
|
|
||||||
|
|
@ -60,7 +64,7 @@ class SDT extends Text
|
||||||
// Content
|
// Content
|
||||||
$xmlWriter->startElement('w:sdtContent');
|
$xmlWriter->startElement('w:sdtContent');
|
||||||
$xmlWriter->startElement('w:r');
|
$xmlWriter->startElement('w:r');
|
||||||
$xmlWriter->writeElement('w:t', 'Pick value');
|
$xmlWriter->writeElement('w:t', $value);
|
||||||
$xmlWriter->endElement(); // w:r
|
$xmlWriter->endElement(); // w:r
|
||||||
$xmlWriter->endElement(); // w:sdtContent
|
$xmlWriter->endElement(); // w:sdtContent
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -267,7 +267,7 @@ class ElementTest extends \PHPUnit_Framework_TestCase
|
||||||
$phpWord = new PhpWord();
|
$phpWord = new PhpWord();
|
||||||
$section = $phpWord->addSection();
|
$section = $phpWord->addSection();
|
||||||
|
|
||||||
$section->addSDT('comboBox');
|
$section->addSDT('comboBox')->setListItems(array('1' => 'Choice 1', '2' => 'Choice 2'))->setValue('select value');
|
||||||
$section->addSDT('dropDownList');
|
$section->addSDT('dropDownList');
|
||||||
$section->addSDT('date')->setAlias('date_alias')->setTag('my_tag');
|
$section->addSDT('date')->setAlias('date_alias')->setTag('my_tag');
|
||||||
|
|
||||||
|
|
@ -275,9 +275,16 @@ class ElementTest extends \PHPUnit_Framework_TestCase
|
||||||
|
|
||||||
$path = '/w:document/w:body/w:p';
|
$path = '/w:document/w:body/w:p';
|
||||||
|
|
||||||
|
$this->assertTrue($doc->elementExists($path . '[1]/w:sdt/w:sdtContent/w:r/w:t'));
|
||||||
|
$this->assertEquals('select value', $doc->getElement($path . '[1]/w:sdt/w:sdtContent/w:r/w:t')->nodeValue);
|
||||||
$this->assertTrue($doc->elementExists($path . '[1]/w:sdt/w:sdtPr/w:comboBox'));
|
$this->assertTrue($doc->elementExists($path . '[1]/w:sdt/w:sdtPr/w:comboBox'));
|
||||||
|
$this->assertTrue($doc->elementExists($path . '[1]/w:sdt/w:sdtPr/w:comboBox/w:listItem'));
|
||||||
|
$this->assertEquals('1', $doc->getElementAttribute($path . '[1]/w:sdt/w:sdtPr/w:comboBox/w:listItem[1]', 'w:value'));
|
||||||
|
$this->assertEquals('Choice 1', $doc->getElementAttribute($path . '[1]/w:sdt/w:sdtPr/w:comboBox/w:listItem[1]', 'w:displayText'));
|
||||||
|
|
||||||
$this->assertTrue($doc->elementExists($path . '[2]/w:sdt/w:sdtPr/w:dropDownList'));
|
$this->assertTrue($doc->elementExists($path . '[2]/w:sdt/w:sdtPr/w:dropDownList'));
|
||||||
$this->assertFalse($doc->elementExists($path . '[2]/w:sdt/w:sdtPr/w:alias'));
|
$this->assertFalse($doc->elementExists($path . '[2]/w:sdt/w:sdtPr/w:alias'));
|
||||||
|
|
||||||
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:date'));
|
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:date'));
|
||||||
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:alias'));
|
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:alias'));
|
||||||
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:tag'));
|
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:tag'));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue