Using the code below in a block, I am displaying a form in a block on all pages for a content type called Goals. I want to use this block / form to update progress on the goal to a content type called Goal Progress, and connect the two through a key called goal_id - the nid of the Goal.

The challenge that I am having is setting the value for the goal_id field (Goal Progress content type). I suspect I need to use $form_state['values']['field_goal_id']['und']['0']['value'] = 'value'; to declare the value, but I can't figure out where that should be placed.

Been looking for a solution for a couple of hours now, so any suggestions are greatly appreciated.

  $node = menu_get_object();

  if (isset($node->nid)) {
    $nid = $node->nid;

  module_load_include('inc', 'node', 'node.pages');
  $node_form = node_add('goal_progress');
  print drupal_render($node_form);
  • where is the rest of your code? does this code render the whole node form? Is that your intention? – 2pha Oct 4 '14 at 1:42
  • Yes, that is all the code used and it displays the entire form – drupalist Oct 4 '14 at 3:14

OK, I have found a working solution. I added $form['field_goal_id']['und']['0']['value']['#value'] = $node->nid; to hook_form_alter in template.php, and the value is populating the field and the form is submitting successfully. Sweet!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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