Problem/Motivation

The data stored for each vote are described in API.txt, including

vote_source -- A unique identifier used to distinguish votes cast by anonymous users. By default, this is the IP address of the remote machine.

I think that further documentation is needed to explain how to override this default.

Many sites have a policy against logging IP addresses, so they will need some other method for identifying anonymous users.

Proposed resolution

For now, treat this as a documentation issue. It is possible to override the default, and we just need to explain how.

It might be worth making code changes so that it is easier for site builders to change how votes are tracked.

Remaining tasks

In the comments below, I will provide draft documentation. Should this be added to API.txt or README.txt or somewhere else?

User interface changes

None (assuming that this stays a documentation issue).

API changes

None (assuming that this stays a documentation issue).

Data model changes

None.

Comments

benjifisher created an issue.

benjifisher’s picture

Here is my suggested draft documentation.

Custom vote tracking
====================

Each vote is saved in the database with a field "vote_source". This should be
a unique identifier used to distinguish votes cast by anonymous users.
By default, this is the IP address of the remote machine.
There are two ways to override this default.

The module using the VotingAPI framework can specify a different value for
vote_source:

use Drupal\votingapi\Entity\Vote;
// ...
$vote = Vote::create(['type' => 'mymodule_vote_type']);
$vote->setVotedEntityId($entity_id);
$vote->setVotedEntityType($entity_type_id);
$vote->setValueType('mymodule_value_type');
$vote->setValue(1);
$vote->setSource(array($custom_vote_source));
$vote->save();

If the module does not call the setSource() method, then the default is used.

The second method is to change the default value callback. This will have no
effect if the module calls the setSource() method, but it will change the
default for all modules that use the VotingAPI framework. Add code like the
following to a custom module (in the .module file):

/**
 * Implements hook_entity_base_field_info_alter().
 */
function mysitemodule_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  if ($entity_type->id() == 'vote') {
    $fields['vote_source']->setDefaultValueCallback('mysitemodule_vote_source');
  }
}

and then implement the callback function mysitemodule_vote_source().

After adding this code, you will need to clear caches in order for it to take effect.