Has adding blocks programmatically changed in 5.7?
Permalink
(I'm crossposting this on Stack Overflow - the concrete5 tag there is seeing way too little love!)
I'm porting an import tool from a 5.6 install to 5.7.
It reads some data from XML files (I've confirmed the data is there), generates pages programmatically, sets some attributes on those pages, and finally inserts into each a block named "Content" containing some stuff from the XML file:
on the output end, this is what I use in the template to show the data:
I copied this verbatim from the 5.6 script as there don't seem to have been any changes in 5.7.
The import script runs fine in 5.7, it creates the pages, sets the page attributes correctly - but seems to fail to insert the actual blocks! The areas are visible when editing, but they are empty.
What am I doing wrong?
I'm porting an import tool from a 5.6 install to 5.7.
It reads some data from XML files (I've confirmed the data is there), generates pages programmatically, sets some attributes on those pages, and finally inserts into each a block named "Content" containing some stuff from the XML file:
$ContentBlock = BlockType::getByHandle('content'); $data = array('content' => $XML->Content); $newPage->addBlock($ContentBlock, 'Content', $data);
on the output end, this is what I use in the template to show the data:
<?php $a = new Area('Content'); $a->display($c); ?>
I copied this verbatim from the 5.6 script as there don't seem to have been any changes in 5.7.
The import script runs fine in 5.7, it creates the pages, sets the page attributes correctly - but seems to fail to insert the actual blocks! The areas are visible when editing, but they are empty.
What am I doing wrong?
I think you just have a typo the code should be
Apologies - that typo I introduced when preparing the code for using it here, I wanted to remove any possibly confusing German variable names. Never a good idea, I should know by now. The real code is definitely typo-free.
I have test this code and it works fine for me in 5.8.1
So I would guess either your $XML->content doesn't have anything in it or your $newPage isn't a page object.
$newPage->addBlock(BlockType::getByHandle('content'), 'Main', array('content' => '<p>Testing the code insertion</p>'));
So I would guess either your $XML->content doesn't have anything in it or your $newPage isn't a page object.
That low-level test indeed works - even though, very weirdly, a test output of the XML content does, too! But then the problem must definitely be with the content I'm feeding it. Will investigate, thanks!
Ugh. I had to explicitly cast the XML content to string, for a reason I neither understand nor really want to. :)
echo() seems to do the casting automatically, so my test output: always showed the correct content, but feeding it to addBlock() resulted in empty data!
This works:
Thanks again - I could have easily spent hours hunting this down!
echo() seems to do the casting automatically, so my test output:
echo $XML->Content;
This works:
$data = array('content' => (string)$XML->Content);
Thanks again - I could have easily spent hours hunting this down!