XF 2.1 Creating Thread and poll

DDempsey

Member
I'm trying to create a thread with a poll.

I'm able to create the thread using
PHP:
$thread = \XF::asVisitor($User, function() use ($forum, $title, $message)
        {
            $creator = \XF::service('XF:Thread\Creator', $forum);
            $creator->setContent($title, $message);
            $creator->setPrefix($forum['default_prefix_id']);
            $creator->setIsAutomated();
            return $creator->save();
        });


But not sure how to create the poll. I see that $creator = \XF::service('XF:Thread\Creator', $forum); has a $creator->setPollCreator(); but unsure how to use that.

Any tips?
 
Generally you'd use the poll controller plugin to set up the poll creator for you, then pass it to that method:

PHP:
/* @var \XF\ControllerPlugin\Poll $pollPlugin */
$pollPlugin = $this->plugin('XF:Poll');
$pollCreator = $pollPlugin->setupPollCreate('thread', $creator->getThread());
$creator->setPollCreator($pollCreator);
 
Generally you'd use the poll controller plugin to set up the poll creator for you, then pass it to that method:

PHP:
/* @var \XF\ControllerPlugin\Poll $pollPlugin */
$pollPlugin = $this->plugin('XF:Poll');
$pollCreator = $pollPlugin->setupPollCreate('thread', $creator->getThread());
$creator->setPollCreator($pollCreator);
Thanks :)

Got it working. Here's the code in case anyone else is looking for it.

PHP:
$thread = \XF::asVisitor($User, function() use ($forum, $title, $message)
        {
            $creator = \XF::service('XF:Thread\Creator', $forum);
            $creator->setContent($title, $message);
            $creator->setPrefix($forum['default_prefix_id']);
            $creator->setIsAutomated();

            $poll = \XF::service('XF:Poll\Creator', 'thread', $creator->getThread());
            $poll->setQuestion($polltitle);
            $poll->addResponses($response_array);
            $poll->setOptions(array('view_results_unvoted'=>0));
            $poll->setCloseDateRelative(3, 'days');


            $creator->setPollCreator($poll);
            return $creator->save();
        });
 
Top Bottom