View source  
  <?php
namespace Drupal\rate;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\votingapi\Entity\Vote;
use Drupal\votingapi\VoteResultFunctionManager;
class RateEntityVoteWidget {
  
  protected $config;
  
  protected $entityTypeManager;
  
  protected $accountProxy;
  
  protected $resultManager;
  
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $account_proxy, VoteResultFunctionManager $result_manager) {
    $this->config = $config_factory
      ->get('rate.settings');
    $this->entityTypeManager = $entity_type_manager;
    $this->accountProxy = $account_proxy;
    $this->resultManager = $result_manager;
  }
  
  public function buildRateVotingWidget($entity_id, $entity_type_id, $bundle, $widget_type = NULL) {
    $output = [];
    $enabled_types_widgets = $this->config
      ->get('enabled_types_widgets');
    if (isset($enabled_types_widgets[$entity_type_id]) && in_array($bundle, array_keys($enabled_types_widgets[$entity_type_id]))) {
      if (empty($widget_type)) {
        $widget_type = $enabled_types_widgets[$entity_type_id][$bundle]['widget_type'];
      }
      $rate_theme = 'rate_template_' . $widget_type;
      $use_ajax = $this->config
        ->get('use_ajax');
      
      $vote_storage = $this->entityTypeManager
        ->getStorage('vote');
      $vote_ids = $vote_storage
        ->getUserVotes($this->accountProxy
        ->id(), NULL, $entity_type_id, $entity_id);
      $has_voted = !empty($vote_ids) ? TRUE : FALSE;
      $user_can_vote = $this->accountProxy
        ->hasPermission('cast rate vote on ' . $entity_type_id . ' of ' . $bundle);
      
      $user_voted = NULL;
      if ($has_voted && ($vote_id = reset($vote_ids))) {
        
        if ($vote = Vote::load($vote_id)) {
          $user_voted = $vote
            ->getValue();
        }
      }
      
      $output['rate_vote_widget'] = [
        '#theme' => $rate_theme,
        '#results' => $this->resultManager
          ->getResults($entity_type_id, $entity_id),
        '#use_ajax' => $use_ajax,
        '#can_vote' => $user_can_vote,
        '#has_voted' => $has_voted,
        '#user_voted' => $user_voted,
        '#entity_id' => $entity_id,
        '#entity_type_id' => $entity_type_id,
        '#attributes' => [
          'class' => [
            'links',
            'inline',
          ],
        ],
        '#widget_type' => $widget_type,
        '#cache' => [
          'contexts' => [
            'user',
          ],
          'tags' => [
            'vote:' . $bundle . ':' . $entity_id,
          ],
        ],
      ];
    }
    return $output;
  }
  
  public static function getRateWidgets() {
    return [
      "fivestar" => "Fivestar",
      "number_up_down" => "Number Up / Down",
      "thumbs_up" => "Thumbs Up",
      "thumbs_up_down" => "Thumbs Up / Down",
      "yesno" => "Yes / No",
    ];
  }
}