Commit 8a48acb0 authored by Dries's avatar Dries

- Patch #331708 by chx: convert poll_choice_js to FAPI2.

parent 22570fd6
......@@ -315,7 +315,8 @@ function poll_more_choices_submit($form, &$form_state) {
// Make the changes we want to the form state.
if ($form_state['values']['poll_more']) {
$form_state['choice_count'] = count($form_state['values']['choice']) + 5;
$n = $_GET['q'] == 'poll/js' ? 1 : 5;
$form_state['choice_count'] = count($form_state['values']['choice']) + $n;
}
}
......@@ -363,55 +364,33 @@ function _poll_choice_form($key, $chid = NULL, $value = '', $votes = 0, $weight
* Menu callback for AHAH additions.
*/
function poll_choice_js() {
// Add the new element to the stored form state. Without adding the element
// to the form, Drupal is not aware of this new elements existence and will
// not process it. We retreive the cached form, add the element, and resave.
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form_state = array('submitted' => FALSE);
if (!$form = form_get_cache($form_build_id, $form_state)) {
exit();
}
$delta = count($_POST['choice']);
$key = isset($form['#node']->choice) ? 'new:'. ($delta - count($form['#node']->choice)) : 'new:'. $delta;
// Match the new choice at the current greatest weight.
$weight = 0;
foreach ($_POST['choice'] as $choice) {
$weight = $choice['weight'] > $weight ? $choice['weight'] : $weight;
}
// Build our new form element.
$form_element = _poll_choice_form($key, NULL, NULL, NULL, $weight, $delta + 1);
drupal_alter('form', $form_element, array(), 'poll_choice_js');
// Dynamically increase the delta of the weight field for every field added.
foreach(element_children($form['choice_wrapper']['choice']) as $n) {
$form['choice_wrapper']['choice'][$n]['weight']['#delta'] = $delta + 1;
}
// Get the form from the cache.
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
// Add the new poll choice.
$form['choice_wrapper']['choice'][$key] = $form_element;
// We will run some of the submit handlers so we need to disable redirecting.
$form['#redirect'] = FALSE;
// Reorder the form to use the same order as post.
$order = array_flip(array_keys($_POST['choice']));
$form['choice_wrapper']['choice'] = array_merge($order, $form['choice_wrapper']['choice']);
// We need to process the form, prepare for that by setting a few internals
// variables.
$form['#post'] = $_POST;
$form['#programmed'] = FALSE;
$form_state['post'] = $_POST;
// Resave the cache.
form_set_cache($form_build_id, $form, $form_state);
$form += array(
'#post' => $_POST,
'#programmed' => FALSE,
);
// Build, validate and if possible, submit the form.
drupal_process_form($form_id, $form, $form_state);
// Rebuild the form.
$form = form_builder('poll_node_form', $form, $form_state);
// This call recreates the form relying solely on the form_state that the
// drupal_process_form set up.
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
// Render the new output.
$choice_form = $form['choice_wrapper']['choice'];
unset($choice_form['#prefix'], $choice_form['#suffix']); // Prevent duplicate wrappers.
$choice_form[$key]['#attributes']['class'] = empty($choice_form[$key]['#attributes']['class']) ? 'ahah-new-content' : $choice_form[$key]['#attributes']['class'] .' ahah-new-content';
$choice_form[$key]['chvotes']['#value'] = 0;
$output = theme('status_messages') . drupal_render($choice_form);
drupal_json(array('status' => TRUE, 'data' => $output));
......
......@@ -153,3 +153,56 @@ class PollBlockTestCase extends DrupalWebTestCase {
$this->assertText(t('The block settings have been updated.'), t('Block successfully move to footer region.'));
}
}
/**
* Test adding new choices.
*/
class PollJSAddChoice extends DrupalWebTestCase {
function getInfo() {
return array(
'name' => t('Poll add choice'),
'description' => t('Submits a POST request for an additional poll choice.'),
'group' => t('Poll')
);
}
function setUp() {
parent::setUp('poll');
}
/**
* Test adding a new choice.
*/
function testAddChoice() {
$web_user = $this->drupalCreateUser(array('create poll content', 'access content'));
$this->drupalLogin($web_user);
$this->drupalGet('node/add/poll');
$edit = array(
'title' => $this->randomName(),
'choice[new:0][chtext]' => $this->randomName(),
'choice[new:1][chtext]' => $this->randomName(),
);
// @TODO: the framework should make it possible to submit a form to a
// different URL than its action or the current. For now, we can just force
// it.
$this->additionalCurlOptions[CURLOPT_URL] = url('poll/js', array('absolute' => TRUE));
$this->drupalPost(NULL, $edit, t('More choices'));
unset($this->additionalCurlOptions[CURLOPT_URL]);
// The response is drupal_json, so we need to undo some escaping.
$response = json_decode(str_replace(array('\x3c', '\x3e', '\x26'), array("<", ">", "&"), $this->drupalGetContent()));
$this->assertTrue(is_object($response), t('The response is an object'));
$this->assertIdentical($response->status, TRUE, t('Response status is true'));
// This response data is valid HTML so we will can reuse everything we have
// for HTML pages.
$this->content = $response->data;
// Needs to be emptied out so the new content will be parsed.
$this->elements = '';
$this->assertFieldByName('choice[chid:0][chtext]', $edit['choice[new:0][chtext]'], t('Field !i found', array('!i' => 0)));
$this->assertFieldByName('choice[chid:1][chtext]', $edit['choice[new:1][chtext]'], t('Field !i found', array('!i' => 1)));
$this->assertFieldByName('choice[new:0][chtext]', '', t('Field !i found', array('!i' => 2)));
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment