Create Advanced Poll programatically

Public

Code snippet allowing you to create an advanced poll programatically (7.x-3.x-dev)

</> CopyGet raw version
php
  1. $options = array(
  2. 'vote1',
  3. 'vote 2',
  4. 'vote 3',
  5. );
  6.  
  7. global $user;
  8. $poll_node = new StdClass();
  9. // Information for base node
  10. $poll_node->type = 'advpoll';
  11. $poll_node->uid = $user->uid;
  12. $poll_node->status = 1;
  13. $poll_node->promote = 0;
  14. $poll_node->sticky = 0;
  15. $poll_node->title ='Advpoll title';
  16. $poll_node->comment = 0;
  17. $poll_node->active = 1;
  18. // Loop through the options and create them
  19. foreach ($options as $option) {
  20. $poll_node->advpoll_choice[LANGUAGE_NONE][] = array(
  21. // Random id (uses the same method as the module) - advpoll_node_presave ensure it's unique
  22. 'choice_id' => dechex(time() * rand(5, 50)),
  23. 'choice' => $option,
  24. 'write_in' => 0,
  25. );
  26. }
  27.  
  28. node_save($poll_node);