You are here

public function RateWidgetBase::getForm in Rate 8.2

Gets the widget form as configured for given parameters.

Return value

\Drupal\Core\Form\FormInterface configured vote form

File

src/Plugin/RateWidgetBase.php, line 147

Class

RateWidgetBase
Base class for Rate widget plugins.

Namespace

Drupal\rate\Plugin

Code

public function getForm($entity_type, $entity_bundle, $entity_id, $vote_type, $value_type, $rate_widget, $settings, $user_id = NULL) {
  $options = $settings
    ->get('options');
  $vote = $this
    ->getEntityForVoting($entity_type, $entity_bundle, $entity_id, $vote_type, $value_type, $rate_widget, $settings, $user_id);

  // Give other modules a chance to alter the rate widget options.
  $this->moduleHandler
    ->invokeAll('rate_widget_options_alter', [
    &$options,
    $entity_type,
    $entity_bundle,
    $entity_id,
    $rate_widget,
    $settings,
    $user_id,
  ]);
  $new_options = [];
  $option_classes = [];

  // For Fivestar we need only the values and labels, omit classes.
  foreach ($options as $option) {
    $new_options[$option['value']] = isset($option['label']) ? $option['label'] : '';
    $option_classes[$option['value']] = isset($option['class']) ? $option['class'] : '';
  }

  /*
   * @TODO: remove custom entity_form_builder once
   *   https://www.drupal.org/node/766146 is fixed.
   */
  $form = $this->entityFormBuilder
    ->getForm($vote, 'rate_vote', [
    'settings' => $settings,
    'plugin' => $this,
    'options' => $new_options,
    'classes' => $option_classes,
    'show_own_vote' => '1',
    'readonly' => FALSE,
    // @TODO: remove following keys when #766146 fixed (multiple form_ids).
    'entity_type' => $entity_type,
    'entity_bundle' => $entity_bundle,
    'entity_id' => $entity_id,
    'vote_type' => $vote_type,
    'rate_widget' => $rate_widget,
  ]);
  return $form;
}