LikeAndDislike.php in Like & Dislike 8
File
src/Plugin/views/field/LikeAndDislike.php
View source
<?php
namespace Drupal\like_and_dislike\Plugin\views\field;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\like_and_dislike\LikeDislikeVoteBuilderInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LikeAndDislike extends FieldPluginBase implements ContainerFactoryPluginInterface {
protected $likeDislikeBuilder;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('like_and_dislike.vote_builder'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, LikeDislikeVoteBuilderInterface $likeDislikeBuilder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->likeDislikeBuilder = $likeDislikeBuilder;
}
public function query() {
}
public function render(ResultRow $values) {
if ($entity = $values->_entity) {
if (like_and_dislike_is_enabled($entity)) {
$entity_type = $entity
->getEntityTypeId();
$entity_id = $entity
->id();
return $this->likeDislikeBuilder
->build($entity_type, $entity_id);
}
else {
return $this
->t('Enable the current entity/bundle in the Like & Dislike settings page.');
}
}
return NULL;
}
}