1

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)?

  • 2
    For D8 It is not defined anywhere. In D7 it was defined in the .module file. Thus, looks like they have not updated their docs & code comments on D8. – No Sssweat Oct 6 '18 at 10:23
  • it's also called in votingapi.drush.inc and reference throughout drupal 8 docs as well; @kiamlaluno, fast & accurate responses are often the most helpful and having this answer indexed would probably help many 30+ minutes going down the wrong direction. nevertheless, question updated. – E.A.T Oct 6 '18 at 19:49
  • @NoSssweat, are you familiar with the votingapi for D8? Do you know how to set votes programmatically? – E.A.T Oct 24 '18 at 1:41
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.