Forum


Replies: 1   Views: 972
List not working when inside an array of word fragments
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by dannyguest  · 28-05-2020 - 14:53

Afternoon,

I am trying to achieve something like this: 

Test -> AddText WF

  • Foo -> AddList WF
  • Foo
  • Foo​

Test2

  • Foo
  • Foo

 

$event_wf = new WordFragment($phpdocx, 'document');
        $paragraphOptions = array(
            'lineBreak' => 'after',
            'bold' => 'true',
        );
        $itemList = array(
            'Line 1',
            'Line 2',
            'Line 3',
            'Line 4',
            'Line 5'
        );
        foreach ($events as $key => $value) {
            $wf = new WordFragment($phpdocx, 'document');
            $wf->addText($value->AccordianLabel, $paragraphOptions);
            $events_array[] = $wf;

            $wf_bullets = new WordFragment($phpdocx, 'document');
            $wf_bullets->addList($itemList, 1);
            $events_array[] = $wf_bullets;
        }

        $event_wf->addText($events_array);
        
        $phpdocx->replaceVariableByWordFragment(array('DATA_EVENTS' => $event_wf));

The problem is my output is like this: 

Test
FOOFOOFOOTest2
FOOFOO

So the list isn't working. Any ideas?

Thanks in advance

Posted by admin  · 28-05-2020 - 16:08

Hello,

Using the following line:

$event_wf->addText($events_array);

all contents are added as inline (block tags are removed).

To accomplish the required task, you need to use a single WordFragment, where you add all content, instead of grouping them with addText:

$event_wf = new WordFragment($phpdocx, 'document');
$event_wf->addText('Text');
$itemList = array(
    'Line 1', // you can add text strings or WordFragments if needed
    'Line 2',
    'Line 3',
    'Line 4',
    'Line 5'
);
$event_wf->addList($itemList, 1);
$event_wf->addText('More text');
$event_wf->addList($itemList, 1);

$docx->replaceVariableByWordFragment(array('DATA_EVENTS' => $event_wf));

WordFragments are very flexible and they allow adding a single content or group of contents.

Regards.