You are here

public static function YamlFormRating::buildRateIt in YAML Form 8

Build RateIt div.

Parameters

array $element: A rating element.

Return value

string The RateIt div tag.

See also

https://github.com/gjunge/rateit.js/wiki

2 calls to YamlFormRating::buildRateIt()
YamlFormRating::formatHtml in src/Plugin/YamlFormElement/YamlFormRating.php
Format an element's value as HTML.
YamlFormRating::preRenderYamlFormRating in src/Element/YamlFormRating.php
Prepares a #type 'yamlform_rating' render element for input.html.twig.

File

src/Element/YamlFormRating.php, line 70

Class

YamlFormRating
Provides a form element for entering a rating.

Namespace

Drupal\yamlform\Element

Code

public static function buildRateIt(array $element) {

  // Add default properties since this element does not have to be a render
  // element.
  // @see \Drupal\yamlform\Plugin\YamlFormElement\YamlFormRating::formatHtml
  $element += [
    '#min' => 0,
    '#max' => 5,
    '#step' => 1,
    '#star_size' => 'medium',
    '#reset' => FALSE,
  ];
  $is_readonly = !empty($element['#readonly']) || !empty($element['#attributes']['readonly']);
  $attributes = [
    'class' => [
      'rateit',
      'svg',
    ],
    'data-rateit-min' => $element['#min'],
    'data-rateit-max' => $element['#max'],
    'data-rateit-step' => $element['#step'],
    'data-rateit-resetable' => !$is_readonly && $element['#reset'] ? 'true' : 'false',
    'data-rateit-readonly' => $is_readonly ? 'true' : 'false',
  ];

  // Set range element's #id.
  if (isset($element['#id'])) {
    $attributes['data-rateit-backingfld'] = '#' . $element['#id'];
  }

  // Set value for HTML preview.
  // @see \Drupal\yamlform\Plugin\YamlFormElement\YamlFormRating::formatHtml
  if (isset($element['#value'])) {
    $attributes['data-rateit-value'] = $element['#value'];
  }
  if (isset($element['#starwidth']) && isset($element['#starheight'])) {
    $attributes['data-rateit-starwidth'] = $element['#starwidth'];
    $attributes['data-rateit-starheight'] = $element['#starheight'];
  }
  else {

    // Set star width and height using the #star_size.
    $sizes = [
      'large' => 32,
      'medium' => 24,
      'small' => 16,
    ];
    $size = isset($sizes[$element['#star_size']]) ? $element['#star_size'] : 'small';
    $attributes['data-rateit-starwidth'] = $attributes['data-rateit-starheight'] = $sizes[$size];
    $attributes['class'][] = 'rateit-' . $size;
  }
  return [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#attributes' => $attributes,
    '#attached' => [
      'library' => [
        'yamlform/yamlform.element.rating',
      ],
    ],
  ];
}