0

I have content type Gellery with two fields. Image (Multiple) Game Name (Entity Reference)

I am setting Game Name in hook_form_alter. Lets say it is set to "Game 001". When image is uploaded, it actually resets the form, so the changes made to field "Game Name" donot apply when submit the form.

How to prevent form to be rebuilt on uploading image. I guess something do to with $form_state

if ($form_id == 'game_gallery_node_form') {
  $form['field_boardgame']['und'][0]['target_id']['#default_value'] = 'game (41)';
}

enter image description here

1

I've done some tests using an Entity Reference field, and setting its default value in a hook_form_alter implementation. Everything is working fine - when I upload a single or multiple image files, the default value that I set in the hook for the Entity Reference field is preserved, and is saved fine when I press Save.

Please notice the way I'm setting the default value in my implementation of hook_form_alter(). This is different from what you have:

$form['field_reference_to_page'][LANGUAGE_NONE]['#default_value'] = 'trip (189)';

(My field is called field_reference_to_page. In your case, you 'll need to replace with field_boardgame). Notice that the arrays are different from what you have. Basically there is no [0] or ['target_id']. The value 'trip (189) is the title of the node and the node ID that this field is referencing. I made sure that the node really existed, and the node ID was correct.

I put together a short video proving that it really works.

By the way, Image fields cause an AJAX call when an image is uploaded - the form is rebuilt but not reloaded. But as I said, the default_value is preserved if you implemented the form alter correctly.

7
  • If I add integer as target_id, say 41, then on submit it says "There are no entities matching "41"", but it works fine when I add the string as 'game (41)'. That means I am using correct array for default value. But it goes away when I upload the image before submitting the form. Then it donot save the Game Name. – Tanvir Ahmad Jan 4 '16 at 10:22
  • Oops, after editing my answer I just noticed that you are dealing with an Entity Reference field, not a Taxonomy Term Reference one! My answer may not work then. Later today I'll check it and edit it again if necessary. – argiepiano Jan 4 '16 at 15:43
  • Since your field is not a term reference, I'll delete the first part of my edited answer. I don't want to confuse people with partially correct info. I'll come back to this later today to see if I can help. In the meantime it seems like you are correct about using the title and ID in parenthesis: see drupal.stackexchange.com/questions/27655/… – argiepiano Jan 4 '16 at 15:47
  • I'm editing my answer with new information. Hopefully it will be helpful – argiepiano Jan 4 '16 at 21:43
  • I just noticed in your screen shot that the field seems to be greyed out. Is it disabled? This may be the problem. See drupal.org/node/159187#comment-5166192 . I haven't tried this, but perhaps using #value instead of #default_value may solve this. – argiepiano Jan 5 '16 at 1:16

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.