Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

please help. I have tried so many ways to solve the following problem but did not succeed. I really need your support on this one.

Here is the problem: There are 2 content types : Fruits and Apple.

Apple content type has an entity reference field to fruits content type.

Now, whenever I view a node of type fruits, the entity reference field in apple add form should prepopulate to fruits node title.

Please see the image below. It will make everything clear.

enter image description here

Is it possible to prepopulate the field? If yes, how? Thanks.

The modules I am using right now: 1.Panels 2. Entity reference 3. Form Block 4. Entity reference prepopulate.

As per the suggestion from Sinan Erdem , I checked http://drupal.org/node/494008 . The code is below . It still doesn't work. :( Can you tell me why?

    <?php
/** hook_form_alter **/

function pp_form_alter(&$form, $form_state, $form_id) {
// Check for a particular content type's node form. Form name is [content_type_name]_node_form
  if ($form_id == 'apple_node_form') {
    // Add an after_build function to process when everything's complete.
    $form['#after_build'][] = 'pp_after_build';
  }
}

/**
* Populate entity reference field
*/
function pp_after_build($form, &$form_state) {
  // Obtain node object from nid in url
  $entityreference_field_category = node_load(arg(1));
  //populate autocomplete entityreference field with node title. Name of field is field_[field_name].
  $form['field_category'][0]['nid']['nid']['#value'] = $entityreference_field_category->title; 

return $form;
}
?>
share|improve this question
I have tried both $form['field_category'][0]['nid']['nid']['#value'] and $form['field_category']['nid']['nid']['#value'] – Nick Dave Sep 27 '12 at 4:17

2 Answers

Please check the following issue: http://drupal.org/node/494008

share|improve this answer
Thanks. I tried the code but it didn't work. :/ Can you please check if there is any error in it? Thanks again. – Nick Dave Sep 26 '12 at 12:33

Looks like I need to not scan so fast. I see you are already using entity prepopulate. In order to prepopulate it your entity reference field, you will need to pass the value by URL. Meaning you need to create a custom node/add/contenttype link from your origin page.

For example: From Fruit you include a add link http://url.com/node/add/type?field_category=1 or http://url.com/node/add/type?field_category=[node:nid] or whatever token value you need.

You can also use the Prepopulate module (different module) to do this on almost any field including entity reference, although the syntax is a bit longer.

Good luck!

share|improve this answer
How do I pass the value by url to a block? (in this case, the node add form.) I want to allow users to add apples on the same page itself. I don't want them to click on a link, go to a different page and then add the node. :/ – Nick Dave Sep 26 '12 at 12:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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