You are here

public static function WebformRating::buildRateIt in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Element/WebformRating.php \Drupal\webform\Element\WebformRating::buildRateIt()

Build RateIt div.

Parameters

array $element: A rating element.

Return value

array A renderable array containing the RateIt div tag.

See also

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

2 calls to WebformRating::buildRateIt()
WebformRating::formatHtmlItem in src/Plugin/WebformElement/WebformRating.php
Format an element's value as HTML.
WebformRating::preRenderWebformRating in src/Element/WebformRating.php
Prepares a #type 'webform_rating' render element for input.html.twig.

File

src/Element/WebformRating.php, line 87

Class

WebformRating
Provides a webform element for entering a rating.

Namespace

Drupal\webform\Element

Code

public static function buildRateIt(array $element) {

  // Add default properties since this element does not have to be a render
  // element.
  // @see \Drupal\webform\Plugin\WebformElement\WebformRating::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 selector based on its parents.
  if (isset($element['#attributes']['data-drupal-selector'])) {
    $attributes['data-rateit-backingfld'] = '[data-drupal-selector="' . $element['#attributes']['data-drupal-selector'] . '"]';
  }

  // Set value for HTML preview.
  // @see \Drupal\webform\Plugin\WebformElement\WebformRating::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' => [
        'webform/webform.element.rating',
      ],
    ],
  ];
}