You are here

public function LikeDislikeVoteBuilder::build in Like & Dislike 8

Lazy builder callback for displaying like and dislike icons.

Parameters

string $entity_type_id: The entity type ID for which like and dislikes icons should be shown.

string|int $entity_id: The entity ID for which like and dislikes icons should be shown.

Return value

array A render array for like and dislike icons.

Overrides LikeDislikeVoteBuilderInterface::build

File

src/LikeDislikeVoteBuilder.php, line 67

Class

LikeDislikeVoteBuilder
Provides a lazy builder for user votes.

Namespace

Drupal\like_and_dislike

Code

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;
}