Here’s a version of @dmegatool and @sazcurrain’s code which works with Drupal 10 (and probably 8+). I used Claude to write it but it’s working fine on an actual project. The full module code, with comments, is at GitHub.
use Drupal\Core\Form\FormStateInterface; use Drupal\votingapi\VoteInterface; use Drupal\Core\Entity\EntityInterface; function cookie_voter_get_id() { $cookie_name = 'Drupal_CookieVoter'; if (empty($_COOKIE[$cookie_name])) { $unique_id = uniqid('vote_', true); setcookie( $cookie_name, $unique_id, time() + 31536000, // 1 year expiration '/' ); $_COOKIE[$cookie_name] = $unique_id; } return $_COOKIE[$cookie_name]; } function cookie_voter_entity_create(EntityInterface $entity) { if ($entity instanceof VoteInterface && !\Drupal::currentUser()->id()) { $cookie_id = cookie_voter_get_id(); $entity->set('vote_source', $cookie_id); } } function cookie_voter_rate_vote_data_alter(&$vote_data, $entity_type, $entity_bundle, $entity_id, $rate_widget, $settings, $user_id) { if (empty($user_id) || $user_id == 0) { $cookie_id = cookie_voter_get_id(); $vote_data['vote_source'] = $cookie_id; } } function cookie_voter_entity_presave(EntityInterface $entity) { if ($entity instanceof VoteInterface && $entity->getOwnerId() == 0) { $cookie_id = cookie_voter_get_id(); $entity->set('vote_source', $cookie_id); } } function cookie_voter_query_votingapi_vote_check_alter($query) { foreach ($query->conditions() as $key => $condition) { if (isset($condition['field']) && $condition['field'] === 'vote_source' && !\Drupal::currentUser()->id()) { $cookie_id = cookie_voter_get_id(); $condition['value'] = $cookie_id; $query->condition('vote_source', $cookie_id, '='); } } } function cookie_voter_module_implements_alter(&$implementations, $hook) { if (in_array($hook, ['entity_create', 'rate_vote_data_alter'])) { if (isset($implementations['cookie_voter'])) { $group = $implementations['cookie_voter']; unset($implementations['cookie_voter']); $implementations = ['cookie_voter' => $group] + $implementations; } } }
Here’s a version of @dmegatool and @sazcurrain’s solution which works with Drupal 10 (and probably 8+). I used Claude to write it but it’s working fine on an actual project. The full module code with comments is at GitHub.
use Drupal\Core\Form\FormStateInterface; use Drupal\votingapi\VoteInterface; use Drupal\Core\Entity\EntityInterface; function cookie_voter_get_id() { $cookie_name = 'Drupal_CookieVoter'; if (empty($_COOKIE[$cookie_name])) { $unique_id = uniqid('vote_', true); setcookie( $cookie_name, $unique_id, time() + 31536000, // 1 year expiration '/' ); $_COOKIE[$cookie_name] = $unique_id; } return $_COOKIE[$cookie_name]; } function cookie_voter_entity_create(EntityInterface $entity) { if ($entity instanceof VoteInterface && !\Drupal::currentUser()->id()) { $cookie_id = cookie_voter_get_id(); $entity->set('vote_source', $cookie_id); } } function cookie_voter_rate_vote_data_alter(&$vote_data, $entity_type, $entity_bundle, $entity_id, $rate_widget, $settings, $user_id) { if (empty($user_id) || $user_id == 0) { $cookie_id = cookie_voter_get_id(); $vote_data['vote_source'] = $cookie_id; } } function cookie_voter_entity_presave(EntityInterface $entity) { if ($entity instanceof VoteInterface && $entity->getOwnerId() == 0) { $cookie_id = cookie_voter_get_id(); $entity->set('vote_source', $cookie_id); } } function cookie_voter_query_votingapi_vote_check_alter($query) { foreach ($query->conditions() as $key => $condition) { if (isset($condition['field']) && $condition['field'] === 'vote_source' && !\Drupal::currentUser()->id()) { $cookie_id = cookie_voter_get_id(); $condition['value'] = $cookie_id; $query->condition('vote_source', $cookie_id, '='); } } } function cookie_voter_module_implements_alter(&$implementations, $hook) { if (in_array($hook, ['entity_create', 'rate_vote_data_alter'])) { if (isset($implementations['cookie_voter'])) { $group = $implementations['cookie_voter']; unset($implementations['cookie_voter']); $implementations = ['cookie_voter' => $group] + $implementations; } } }
- Status changed to Closed: outdated
25 days ago 1:25pm 18 September 2025