Add/Activate a Topic for a Page programmaticaly

Permalink 1 user found helpful
I've created a page like so:
$blog = Page\Page::getByID(157);
$template = \PageTemplate::getByHandle('blog_entry');
$entry = $blog->add($type, array(
    'cName' => 'My title',
    'cDescription' => 'description',
    'cHandle' => 'my_title',
    'cvIsApproved' => true,
    'cDatePublic' => $publishDate->format('Y-m-d H:i:s')
), $template);

And I've my topic:
use \Concrete\Core\Tree\Type\Topic as TopicTree;
use \Concrete\Core\Tree\Node\Type\Topic as TopicTreeNode;
use \Concrete\Core\Tree\Node\Node as TreeNode;
$topicTree = TopicTree::getByName('Blog Entries');
$parent = TreeNode::getByID($topicTree->getRootTreeNodeObject()->treeNodeID);
$item0 = TopicTreeNode::add('my topic', $parent);


So now my question:

How to add/activate that topic ($item0) to that page ($entry) like one would do in the frontend in the pages attributes by going to Page attributes -> Blog -> Blog entries? See attached image.

EDIT: After 2 days of digging around I'v asked this question on SO:http://stackoverflow.com/questions/34305548/activate-a-topic-for-a-...

PLEASE HELP! I'm desperate...

1 Attachment

daenu
 
MrKDilkington replied on at Permalink Reply
MrKDilkington
This is an interesting question. I would also like to know how this could be done.
daenu replied on at Permalink Best Answer Reply
daenu
So there were 2 issues:
The first one was to use the \Page Alias not the full Namespace Concrete\Core\Page. It's system dependent and they will stop using Aliases in future releases.

The second was that $page->setAttribute method takes the handle as first and the DisplayPath aas second parameter. (not the name of the topic)

So here it is:

if (!$item0) {
    $topicTree = TopicTree::getByName('Blog Entries');
    $parentTopic = TreeNode::getByID($topicTree->getRootTreeNodeObject()->treeNodeID);
    $item0 = TopicTreeNode::add('my topic', $parentTopic);
}
$entry->setAttribute('blog_entry_topics', array($item0->getTreeNodeDisplayPath()));

Thx to Joe for giving the solution. For further reading see the question on SO:http://stackoverflow.com/questions/34305548/activate-a-topic-for-a-...