Drupal Creating Votes in Voting API Through Code

2019-04-12 00:38发布

I have a custom module I'm writing, part of what I want it to do is create a vote associated with a node, I'm trying to figure out how to call the voting API from my module. I loookd in the documentation but it's a little sparse.

2条回答
Summer. ? 凉城
2楼-- · 2019-04-12 00:58

Here is an example from a module I wrote a while ago.

while ($data = db_fetch_object($result)) {
  $node = node_load($data->nid);
  $node_terms = taxonomy_node_get_terms($node);
  $vote['value'] = 0;
  $vote['value_type'] = 'points';
  foreach ($node_terms as $term) {
    $vote['value'] = $vote['value'] + $users_tags[$term->name];
  }
  $vote['content_id'] = $node->nid;
  if (isset($vote['content_id'])) {
    votingapi_set_votes($vote);
  }
}
查看更多
我只想做你的唯一
3楼-- · 2019-04-12 00:59

Just another example of using this:

function _ept_set_vote($nid, $status, $uid = NULL) {
  global $user;

  $vote = array(
    array(
      'entity_type' => 'node',
      'value' => 1,
      'entity_id' => $nid,
      'uid' => (!$uid) ? $user->uid : $uid,
      'tag' => $status
    )
  );

  votingapi_set_votes($vote, array());
}

I call it like this:

switch($task_status){
      case('start'):
        _ept_set_vote($nid, "Start");
        break;
      case('completed'):
        _ept_set_vote($nid, "Completed");
        break;
    }
查看更多
登录 后发表回答