You are here

class LikeDislikeVoteBuilder in Like & Dislike 8

Provides a lazy builder for user votes.

Hierarchy

Expanded class hierarchy of LikeDislikeVoteBuilder

1 string reference to 'LikeDislikeVoteBuilder'
like_and_dislike.services.yml in ./like_and_dislike.services.yml
like_and_dislike.services.yml
1 service uses LikeDislikeVoteBuilder
like_and_dislike.vote_builder in ./like_and_dislike.services.yml
Drupal\like_and_dislike\LikeDislikeVoteBuilder

File

src/LikeDislikeVoteBuilder.php, line 15

Namespace

Drupal\like_and_dislike
View source
class LikeDislikeVoteBuilder implements LikeDislikeVoteBuilderInterface, TrustedCallbackInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The voting storage.
   *
   * @var \Drupal\votingapi\VoteStorageInterface
   */
  protected $voteStorage;

  /**
   * The account.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a new LikeDislikeVoteBuilder.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user, ConfigFactoryInterface $config_factory) {
    $this->entityTypeManager = $entity_type_manager;
    $this->voteStorage = $entity_type_manager
      ->getStorage('vote');
    $this->currentUser = $current_user;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function build($entity_type_id, $entity_id) {

    // Load the entity for which like and dislikes icons should be shown.
    $entity = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->load($entity_id);
    $hide_vote_widget = $this->configFactory
      ->get('like_and_dislike.settings')
      ->get('hide_vote_widget');
    $like_access = like_and_dislike_can_vote($this->currentUser, 'like', $entity);
    $dislike_access = like_and_dislike_can_vote($this->currentUser, 'dislike', $entity);
    list($likes, $dislikes) = like_and_dislike_get_votes($entity);
    $icons = [];

    // Like icon.
    if (!$hide_vote_widget || $like_access) {
      $like_attributes = new Attribute([
        'title' => $this
          ->t('Like'),
        'data-entity-id' => $entity_id,
        'data-entity-type' => $entity_type_id,
      ]);
      if (!$like_access) {
        $like_attributes
          ->addClass('disable-status');
      }
      if ((bool) $this->voteStorage
        ->getUserVotes($this->currentUser
        ->id(), 'like', $entity_type_id, $entity_id)) {
        $like_attributes
          ->addClass('voted');
      }
      $icons['like'] = [
        'count' => $likes,
        'label' => $this
          ->t('Like'),
        'attributes' => $like_attributes,
      ];
    }

    // Dislike icon.
    if (!$hide_vote_widget || $dislike_access) {
      $dislike_attributes = new Attribute([
        'title' => $this
          ->t('Dislike'),
        'data-entity-id' => $entity_id,
        'data-entity-type' => $entity_type_id,
      ]);
      if ((bool) $this->voteStorage
        ->getUserVotes($this->currentUser
        ->id(), 'dislike', $entity_type_id, $entity_id)) {
        $dislike_attributes
          ->addClass('voted');
      }
      if (!$dislike_access) {
        $dislike_attributes
          ->addClass('disable-status');
      }
      $icons['dislike'] = [
        'count' => $dislikes,
        'label' => $this
          ->t('Dislike'),
        'attributes' => $dislike_attributes,
      ];
    }
    $build['icons'] = [
      '#theme' => 'like_and_dislike_icons',
      '#attached' => [
        'library' => [
          'like_and_dislike/icons',
        ],
      ],
      '#entity_id' => $entity_id,
      '#entity_type' => $entity_type_id,
      '#icons' => $icons,
    ];

    // Attach JS logic in case user has enough permissions to vote.
    if ($like_access || $dislike_access) {
      $build['icons']['#attached']['library'][] = 'like_and_dislike/behavior';
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'build',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LikeDislikeVoteBuilder::$configFactory protected property The config factory.
LikeDislikeVoteBuilder::$currentUser protected property The account.
LikeDislikeVoteBuilder::$entityTypeManager protected property The entity type manager.
LikeDislikeVoteBuilder::$voteStorage protected property The voting storage.
LikeDislikeVoteBuilder::build public function Lazy builder callback for displaying like and dislike icons. Overrides LikeDislikeVoteBuilderInterface::build
LikeDislikeVoteBuilder::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
LikeDislikeVoteBuilder::__construct public function Constructs a new LikeDislikeVoteBuilder.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.