You are here

public static function Fivestar::getElementDefaultValue in Fivestar 8

Provides the correct default value for a fivestar element.

Parameters

array $element: The fivestar element.

Return value

float The default value for the element.

1 call to Fivestar::getElementDefaultValue()
Fivestar::process in src/Element/Fivestar.php
Process callback: process fivestar element.

File

src/Element/Fivestar.php, line 305

Class

Fivestar
Provides a fivestar form element.

Namespace

Drupal\fivestar\Element

Code

public static function getElementDefaultValue(array $element) {
  switch ($element['#settings']['display_format']) {
    case 'average':
      $widget_is_average = $element['#settings']['display_format'] == 'average';
      $default_value = $widget_is_average && !empty($element['#values']['vote_average']) ? $element['#values']['vote_average'] : $element['#default_value'];
      break;
    case 'user':
      $default_value = $element['#values']['vote_user'];
      break;
    case 'smart':
      $default_value = !empty($element['#values']['vote_user']) ? $element['#values']['vote_user'] : $element['#values']['vote_average'];
      break;
    case 'dual':
      $default_value = $element['#values']['vote_user'];
      break;
    default:
      $default_value = $element['#default_value'];
  }
  for ($i = 0; $i <= $element['#stars']; $i++) {
    $this_value = ceil($i * 100 / $element['#stars']);
    $next_value = ceil(($i + 1) * 100 / $element['#stars']);

    // Round up the default value to the next exact star value if needed.
    if ($this_value < $default_value && $next_value > $default_value) {
      $default_value = $next_value;
    }
  }
  return $default_value;
}