0

I've got a validation form (id="mymodule-myct-verify-form") that retrieves data from temp_node to populate it's hidden fields. On submit, the form sends its data to /node/add/myct. (currently via $form['#method' = 'get';, which I'm planning on changing to 'post') What I want to do is this: in mymodule_hook_form_alter, get the data from $_REQUEST (later $_POST) and use that to set the default values of my CCK fields. Currently, jQuery is the approach I'm going for, being unable to get mymodule_form_alter() and mymodule_after_build_convert() to work. Here's what I've got:

 function mymodule_form_alter(&$form, &$form_state, $form_id) {
     if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'].'' == "myct") {
        // if converting, set default values through an after_build function
        if (isset($_REQUEST['conv']) && (strip_tags($_REQUEST['conv']) == 'app') &&
            isset($_REQUEST['taid']) && (intval($_REQUEST['taid']) > 0)
            && ($form['type']['#value'] . '_node_form' == $form_id &&
            (strpos($form['#action'], '/node/add/myct') !== FALSE))
        ) { // on the /node/add/myct/?conv=app&taid=1 page, this is true
            $form['#after_build'][] = 'mymodule_after_build_convert';
        }
  }
}

function mymodule_after_build_convert ($form, &$form_state) {
    //TODO: Set default values for CCK fields
    /*dsm($form);
    dsm($form_state);
    dsm($_REQUEST);*/
    // set receiving date and time
    dsm($form['#field_info']['field_status_10']); // date popup
    $form['#field_info']['field_status_10']['widget']['default_value'] = time();     //date("j F, Y - H:i");
    dsm($form['#field_info']['field_status_10']);
    // set deal type (select list)
    $form['#field_info']['field_deal_type']['widget']['default_value'][0]['value'] = intval($_REQUEST['ta_type']);

    return $form;
}

Why is my date_popup field empty instead of containing today's date? Why does the select list not change?

5
  • Via AJAX it's technically possible, but otherwise no, JavaScript doesn't have access to server side vars
    – Clive
    Feb 26, 2014 at 13:55
  • Thanks for that, @Clive. It confirms what I suspected. Would you be able to point me in the right direction as to how I might be able to do that with AJAX, if at all? Feb 26, 2014 at 13:59
  • @Clive is it even a Drupal question now? Accessing POST from jQuery is neat problem, but Drupal hardly changes anything in that matter. Or maybe I simply fail to see something.
    – Mołot
    Feb 26, 2014 at 14:12
  • @Mołot: I'm developing a Drupal site, hence if I can use a drupal-based solution (such as abandoning a jQuery approach and using hook_form_alter, for example), so much the better. Currently, jQuery is the approach I'm going for, having found that first, but I'm happy to try something else if there's a Drupal way. Maybe it's not clear, but I'm looking for a Drupal way to solve a problem on a Drupal site. Feb 26, 2014 at 14:22
  • I would make that more explicit in the question, or users would get this is a question about jQuery and think it should be closed here and asked on Stack Overflow. Comments should not be used to report important details users would need to answer the question.
    – apaderno
    Feb 26, 2014 at 17:17

1 Answer 1

1

After doing some research, it turns out that taking a JavaScript/jQuery approach is the wrong way to do this. Rather, the code should use hook_form_alter() to retrieve POSTed data ($form['#method'] = 'post';) and set the default values of the node form (including CCK fields) via a function specified with $form['#after_build'].

So, if you want to dig in here and start monkeying with the "real" properties, you need to come in after the point that the CCK form elements are processed. Luckily, the form API has a phase designed for exactly this purpose: #after_build.

/**
 * Implements hook_form_alter().
 */
function example_form_alter(&$form, $form_state, $form_id) {
  // Check for a particular content type's node form.
  if ($form_id == 'example_node_form') {
    // Add an after_build function to process when everything's complete.
    $form['#after_build'][] = 'example_after_build';
  }
}

/**
 * Modifies CCK form elements on the example node form.
 */
function example_after_build($form, &$form_state) {
  // Your code here.
  return $form;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.