RateField.php in Rate 8
File
src/Plugin/views/field/RateField.php
View source
<?php
namespace Drupal\rate\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\rate\RateEntityVoteWidget;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
class RateField extends FieldPluginBase {
public function query() {
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['widget_type'] = [
'default' => 'fivestar',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['widget_type'] = [
'#title' => $this
->t('Widget type'),
'#type' => 'select',
'#default_value' => $this->options['widget_type'],
'#options' => RateEntityVoteWidget::getRateWidgets(),
];
parent::buildOptionsForm($form, $form_state);
}
public function render(ResultRow $values) {
$entity = $this
->getEntity($values);
$widget = [];
if ($entity) {
$widget = [
'#lazy_builder' => [
'rate.entity.vote_widget:buildRateVotingWidget',
[
$entity
->id(),
$entity
->getEntityType()
->id(),
$entity
->bundle(),
$this->options['widget_type'],
],
],
'#create_placeholder' => TRUE,
];
}
return $widget;
}
}