You are here

function fivestar_ajax_submit in Fivestar 7.2

AJAX submit handler for fivestar_custom_widget.

1 string reference to 'fivestar_ajax_submit'
fivestar_expand in ./fivestar.module
Process callback for fivestar_element -- see fivestar_element()

File

./fivestar.module, line 543

Code

function fivestar_ajax_submit($form, $form_state) {
  if (!empty($form_state['settings']['content_id'])) {
    $entity = entity_load($form_state['settings']['entity_type'], array(
      $form_state['settings']['entity_id'],
    ));
    $entity = reset($entity);
    _fivestar_update_field_value($form_state['settings']['content_type'], $entity, $form_state['settings']['field_name'], $form_state['settings']['langcode'], $form_state['values']['vote']);
    $votes = _fivestar_cast_vote($form_state['settings']['entity_type'], $form_state['settings']['content_id'], $form_state['values']['vote'], $form_state['settings']['tag']);
  }
  $values = array();
  $values['user'] = isset($votes['user']['value']) ? $votes['user']['value'] : 0;
  $values['average'] = isset($votes['average']['value']) ? $votes['average']['value'] : 0;
  $values['count'] = isset($votes['count']['value']) ? $votes['count']['value'] : 0;

  // We need to process the 'fivestar' element with the new values.
  $form['vote']['#values'] = $values;

  // Also need to pass on form_state and the complete form,
  // just like form_builder() does.
  $new_element = fivestar_expand($form['vote'], $form_state, $form_state['complete form']);

  // Update the description. Since it doesn't account of our cast vote above.
  // @todo Look into all this, I honestly don't like it and it's a bit weird.
  $form['vote']['vote']['#description'] = isset($new_element['vote']['#description']) ? $new_element['vote']['#description'] : '';
  if (!$form['vote']['#allow_revote'] && !$form['vote']['#allow_clear']) {
    $form['vote'] = $new_element;
  }
  return array(
    '#type' => 'ajax',
    '#commands' => array(
      array(
        'command' => 'fivestarUpdate',
        'data' => drupal_render($form['vote']),
      ),
    ),
  );
}