You are here

fivestar.theme.inc in Fivestar 8

Same filename and directory in other branches
  1. 7.2 includes/fivestar.theme.inc

Provides the theming functions for fivestar.

File

includes/fivestar.theme.inc
View source
<?php

/**
 * @file
 * Provides the theming functions for fivestar.
 */
use Drupal\Core\Render\Element;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Render\Element\RenderElement;

/**
 * Shows a preview of a widget using a custom CSS file.
 */
function theme_fivestar_preview_widget($variables) {

  //  $form = [
  //    '#post' => [],
  //    '#programmed' => FALSE,
  //    '#tree' => FALSE,
  //    '#parents' => [],
  //    '#array_parents' => [],
  //    '#required' => FALSE,
  //    '#attributes' => [],
  //    '#title_display' => 'before',
  //  ];
  //  $complete_form = [];
  //  $form_state = new FormState();
  //  $buidler = \Drupal::formBuilder();
  //  $form_state->setValues([]);
  //  $form_state->setProcessInput(FALSE);
  //  $form_state->setCompleteForm($complete_form);
  //  $form['vote'] = [
  //    '#type' => 'fivestar',
  //    '#stars' => 5,
  //    '#allow_clear' => TRUE,
  //    '#allow_revote' => TRUE,
  //    '#allow_ownvote' => TRUE,
  //    '#widget' => [
  //      'name' => isset($variables['name']) ? $variables['name'] : 'default',
  //      'css' => isset($variables['css']) && $variables['css'] != 'default' ? $variables['css'] : FALSE,
  //    ],
  //  ];
  //
  //  // Attach necessary JS settings.
  //  $settings = [
  //    'titleUser' => t('Your rating') . ': ',
  //    'titleAverage' => t('Average') . ': ',
  //    'feedbackSavingVote' => t('Saving your vote...'),
  //    'feedbackVoteSaved' => t('Your vote has been saved.'),
  //    'feedbackDeletingVote' => t('Deleting your vote...'),
  //    'feedbackVoteDeleted' => t('Your vote has been deleted.'),
  //  ];
  //  $form['#attached'][] = [
  //    'js' => [
  //      [
  //        'type' => 'setting',
  //        'data' => $settings,
  //      ],
  //    ],
  //  ];
  //
  //  $buidler->doBuildForm('fivestar_preview', $form, $form_state);
  //  $output = $form;
  //  $output['#prefix'] = '<div class="fivestar-star-preview fivestar-' . $form['vote']['#widget']['name'] . '">';
  //  $output['#suffix'] = '</div>';
  //
  //  return \Drupal::service('renderer')->render($output);
}

/**
 *
 */
function theme_fivestar_preview($variables) {
  extract($variables, EXTR_SKIP);
  $values = [
    'vote_average' => 50,
    'vote_user' => 80,
    'vote_count' => 20,
  ];
  $settings = [
    'stars' => $stars,
    'allow_clear' => $unvote,
    'allow_revote' => $revote,
    'allow_ownvote' => $ownvote,
    'style' => $style,
    'text' => $text,
    'title' => $title,
    'autosubmit' => FALSE,
    'vote_type' => 'vote',
  ];
  $form = drupal_get_form('fivestar_custom_widget', $values, $settings);
  $form = \Drupal::service('renderer')
    ->render($form);

  // This regex is sadly necessary because having duplicate form_tokens or
  // form_id elements can cause the content type form to choke. Forms inside of
  // forms is also frowned upon, so this removes the wrapping form tag as well.
  $form = str_replace([
    '<form',
    '</form>',
  ], [
    '<div',
    '</div>',
  ], $form);
  $form = preg_replace('/( method=".*?")|( action=".*?")|(<input.*?name="(form_token|form_id|destination|form_build_id)".*?\\/>)/', '', $form);
  return $form;
}

/**
 *
 */
function theme_fivestar_preview_wrapper($variables) {
  return '<div class="fivestar-preview fivestar-preview-' . $variables['type'] . '">' . $variables['content'] . '</div>';
}

/**
 * Theme function for 'default' fivestar field formatter.
 *
 * This themes static stars. That is, pairs of stars where neither set
 * of stars is "exposed". Exposed stars are clickable and displayed in a
 * form. Theming of exposed stars is handled by the form array (and calls
 * the same theme functions called at the end of this function).
 */
function theme_fivestar_formatter_default($variables) {
  $element = $variables['element'];
  if (empty($element['#instance_settings']['stars'])) {
    $element['#instance_settings']['stars'] = 5;
  }

  // Add CSS and JS.
  $path = drupal_get_path('module', 'fivestar');
  drupal_add_js($path . '/js/fivestar.js');
  drupal_add_css($path . '/css/fivestar.css');
  $variables = [
    'rating' => $element['#rating'],
    'stars' => $element['#instance_settings']['stars'],
    'widget' => $element['#widget'],
  ];
  $star_display = theme('fivestar_static', $variables);
  return theme('fivestar_static_element', [
    'description' => $element['#description'],
    'star_display' => $star_display,
    'is_form' => FALSE,
  ]);
}

/**
 * Themes the fivestar form element by adding necessary css and javascript.
 */
function theme_fivestar($variables) {
  $element = $variables['element'];
  return theme('form_element', [
    'element' => $element,
  ]);
}

/**
 * Themes the straight HTML version of the fivestar select list. This is used
 * to remove the wrapping 'form-item' div from the select list.
 */
function theme_fivestar_select($variables) {
  $element = $variables['element'];
  unset($element['#theme']);
  return \Drupal::service('renderer')
    ->render($element);
  Element::setAttributes($element, [
    'id',
    'name',
    'size',
  ]);
  RenderElement::setAttributes($element, [
    'form-select',
  ]);
  $variables['#attributes'] = $element['#attributes'];
  $variables['#options'] = form_select_options($element);
  $variables['#theme'] = 'select';
}

Functions

Namesort descending Description
theme_fivestar Themes the fivestar form element by adding necessary css and javascript.
theme_fivestar_formatter_default Theme function for 'default' fivestar field formatter.
theme_fivestar_preview
theme_fivestar_preview_widget Shows a preview of a widget using a custom CSS file.
theme_fivestar_preview_wrapper
theme_fivestar_select Themes the straight HTML version of the fivestar select list. This is used to remove the wrapping 'form-item' div from the select list.