Adding a Block to Main Area in php

Permalink 1 user found helpful
Hi.
So what I've already gotten is a new page created under a parent page:
$parentPage = Page::getByID(98, 'RECENT');
$data = array(
'name' => $_POST['name'],
'cHandle' => $_POST['name'] . "_handle",
'cDescription' => $_POST['description']
);
$pt = CollectionType::getByHandle('full');
$newPage = $parentPage->add($pt,$data);

Now what I'd like to do is add a content or html block to the main area of that page. I have read through the documentation on pages, collection, collection version, area, and blocks both here and in the API, but the documentation is not very helpful and seems sometimes wrong.

Anyway this is what I've got:
$blockType = BlockType::getByHandle('html');
$area = new Area('Main');
// I have no idea what this data array is supposed to contain. The
// documentation on that is nonexistent. The word "data" is not
// sufficient to know what it might accept.
$data = array(
'html' => '<h3>' . $_POST['name'] . '<h3/>
<p>' . $_POST['description'] . '<p/>'
);
$newPage->addBlock($blockType, $area, $data);
$area->display($newPage);

 
cannonf700 replied on at Permalink Reply
cannonf700
try to simplify it:
$bt = BlockType::getByHandle('html');
$a = new Area('Main');
$a->display($c);


The other info is unnecessary unless you want to publish the name and description of the 'post' (maybe the page's name and description).
I'm assuming that you want to hard code that automatic creation of an html block on a given page-type.

This weblink has a nice cheat-sheet that could prove useful in the future.
http://www.weblicating.com/c5/cheat-sheet/...
Mainio replied on at Permalink Reply
Mainio
Yes, I might also have gotten the first message wrong. What I got there is that he wanted to add a new HTML block to the main area from code only.

But, if you just want to add the block area, this is what you need to do.

EDIT: Although, this has nothing to do with the block type, this is just a common area where you can add any blocks.
Mainio replied on at Permalink Reply
Mainio
Not sure why you want to do this in the first place but the method that adds the block is BlockType::add(). You can find what it does from
/concrete/models/block_types.php

It depends on the block type what you need in that $data array. So, you might want to check the block type controller's save() method. In your case HTML block controller can be found from:
/concrete/blocks/html/controller.php

From there you can see that the only thing you need to pass in the data array is:
$args['content'] = isset($data['content']) ? $data['content'] : '';


So you need:
$data = array(
   'content' => "<p>Here's my HTML although not sure why you want to do this...</p><p>However, let developers do what they want to do...</p>"
);
TechnatomyClient replied on at Permalink Reply
I found that your code works when I apply it to a Contents block. For $data, I simply used a String and not an array.