View source
<?php
namespace Drupal\fivestar\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
class StarsFormatter extends FivestarFormatterBase implements TrustedCallbackInterface {
public static function defaultSettings() {
return [
'text_format' => 'average',
'display_format' => 'average',
'fivestar_widget' => 'basic',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['fivestar_widget'] = [
'#type' => 'radios',
'#options' => $this->widgetManager
->getWidgetsOptionSet(),
'#default_value' => $this
->getSetting('fivestar_widget'),
'#attributes' => [
'class' => [
'fivestar-widgets',
'clearfix',
],
],
'#pre_render' => [
[
$this,
'previewsExpand',
],
],
'#attached' => [
'library' => [
'fivestar/fivestar.admin',
],
],
];
$elements['display_format'] = [
'#type' => 'select',
'#title' => $this
->t('Value to display as stars'),
'#options' => [
'average' => $this
->t('Average vote'),
],
'#default_value' => $this
->getSetting('display_format'),
];
$elements['text_format'] = [
'#type' => 'select',
'#title' => $this
->t('Text to display under the stars'),
'#options' => [
'none' => $this
->t('No text'),
'average' => $this
->t('Average vote'),
],
'#default_value' => $this
->getSetting('text_format'),
];
return $elements;
}
public static function trustedCallbacks() {
return [
'previewsExpand',
];
}
public function settingsSummary() {
$summary[] = $this
->t('Style: @widget', [
'@widget' => $this->widgetManager
->getWidgetLabel($this
->getSelectedWidgetKey()),
]);
$summary[] = $this
->t('Stars display: @style, Text display: @text', [
'@style' => $this
->getSetting('display_format'),
'@text' => $this
->getSetting('text_format'),
]);
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$entity = $items
->getEntity();
$form_builder = \Drupal::formBuilder();
$widget_active_key = $this
->getSelectedWidgetKey();
$display_settings = [
'name' => $this->widgetManager
->getWidgetInfo($widget_active_key) ? $widget_active_key : 'default',
] + $this
->getSettings();
if (!$items
->isEmpty()) {
foreach ($items as $delta => $item) {
$context = [
'entity' => $entity,
'field_definition' => $item
->getFieldDefinition(),
'display_settings' => $display_settings,
];
$elements[$delta] = $form_builder
->getForm('\\Drupal\\fivestar\\Form\\FivestarForm', $context);
}
}
else {
$bundle_fields = \Drupal::getContainer()
->get('entity_field.manager')
->getFieldDefinitions($entity
->getEntityType()
->id(), $entity
->bundle());
$field_definition = $bundle_fields[$items
->getName()];
$context = [
'entity' => $entity,
'field_definition' => $field_definition,
'display_settings' => $display_settings,
];
$elements[] = $form_builder
->getForm('\\Drupal\\fivestar\\Form\\FivestarForm', $context);
}
return $elements;
}
protected function getSelectedWidgetKey() {
$setting = $this
->getSetting('fivestar_widget') ?: 'default';
if (strpos($setting, '.css') === FALSE) {
return $setting;
}
$file_name = basename($setting);
$file_name_exploded = explode('.', $file_name);
$setting = reset($file_name_exploded);
return $setting;
}
}