2

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;
}
?>
  • 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
1

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

  • 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
0

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!

  • 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
  • you can do that by using Advanced form block drupal.org/project/afb :) – olisb Sep 8 '15 at 16:58
0

For the benefit of those who encounter this same problem without a solution, this module: Entity Reference Prepopulate Token will provide a perfect solution. Please make sure you are using the latest dev version of Entity Reference Prepopulate module (as at 2015-Jun-10 or higher).

After enabling the module, go to the content type edit page and scroll down to where you have "Provider" and enter the following comma separated values in the text box provided under "Tokens and panel pane context":

%node:nid, [current-page:url:unaliased:args:value:1]

Just enter the above values in the box and select the optional checkbox for this provider option to activate it, hide the field from the user also if desired, to avoid editing. I've tested it, It works!

According to the module page:

This module supplements Entityreference prepopulate module (dev version is required) by adding prepopulate provider which sets default field values using tokens and replacement patterns of panel pane contexts.

Two kind of patterns can be used for prepopulating entity reference fields:

Tokens, e.g [current-page:url:unaliased:args:value:1] (it returns value "100" for address "node/100", Token module is required). Replacement patterns of panel pane contexts, e.g. %node:nid (it returns node identifier if the form containing entity reference field is placed on node view page created by Page manager). Panels module is required for using these patterns.

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.