VotingAPI docs are out of date.
votingapi_set_vote() has been removed in Drupal 8.
What should I use in its place to programmatically set votes (for instance in a migration)?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.Sign up to join this community
Do you know how to set votes programmatically?
use Drupal\votingapi\Entity\Vote; $vote = Vote::create(['type' => 'vote']); // this comes from /admin/structure/vote-types $vote->setVotedEntityType('node'); // entity type $vote->setVotedEntityId($nid); // node id $vote->setValueType('points'); // this comes from /admin/structure/vote-types $vote->setValue(1); // the number of points given per vote $vote->setOwnerId($uid); // the user id of the person who casted the vote $vote->setCreatedTime($timestamp) // timestamp of when vote was casted (optional) // it will use the current time if you don't include it $vote->save();
The above code will create 1 casted vote. Therefore, you will have to run this code for every single vote.