Hello world,
I was wondering how to prepopulate an entity form from hook_from_alter().
In my case, I have a bunch of stories and another content type "Press folder" that references these stories.
When I create a new "Press folder" content, I would like the reference to be pre-populated with 2 random stories.
I can access the field through the hook but I REALLY don't know what to put into.
I tried to overwrite the field using the function inline_entity_form_field_widget_form() but without success.
Any help would be appreciated...
Thanks for your time. ;)
Comments
Comment #1
bojanz commentedUse hook_inline_entity_form_entity_form_alter().
You have an example in inline_entity_form.api.php
Comment #2
m42 commentedThx bojan for your answer, but it seems I've explained myself badly.
1. I have a main content type called "Press release" that contains an EntityReference field called "Referenced stories"
2. This field ("Referenced stories") references contents of type "Story"
When I create a new content of type "Press release" (which is empty at startup), I would like to have its field "Referenced stories" already filled with 2 stories.
I tried to use hook_inline_entity_form_entity_form_alter() as you mentioned but it isn't even triggered...
Following your comments, it only triggers when a node form is called, so when I MANUALLY try to create or to edit a node reference.
But what I would like to do is to pre-populate this field at startup with existing nodes.
Do you see what I mean (my English is broken, I know... :p) and how to do it ?
Comment #3
m42 commentedAfter days of analysis and reflexion, I'm now able to answer myself. Hoping this can help anyone in the future.
The following piece of code worked in my case (I removed the "Delete" button as I didn't want users to be able to) :
Comment #5
apassi commentedHi,
Nice job, any ideas how to prepopulate n-pieces new entities? Like ief single widget, but for multiple nodes.
Comment #6
Makku01 commented@hbemtec - you, sir, saved my day!
To prepopulate new entities you can do:
and paste $newEntity where hbemtec put $entity. Do not forget to change the 'needs_save' values to TRUE.
Comment #7
a.milkovsky@hbemtec, @Makku01 thanks a lot for sharing this!
If you want to have the forms expanded by default you can use next code:
(I used a bit different variable names comparing to the initial hbemtec's example, but you will get the idea)
In general just follow inline_entity_form_field_widget_form() for additional cases.
Comment #8
strings6 commentedI found this post very helpful! I modified the code slightly to work on three different fields, and pull the $cloneList nids from values stored in a user's profile. Cool stuff!
However... I also added back the delete button because I want the user to be able to remove the preset value and change to something else if they want. But the code just forces it right back to the preset value even after I remove it.
Does anyone know what value I can check against to make sure it only sets a preset initially, but let's it go if I remove the preset reference?
Thanks!
Comment #9
strings6 commentedI figured out "a way" but who knows if it's a "good way".
After the logic occurs, I do:
$form_state['default_'.$element['#field_name'].'_already_set'] = TRUE;And then I wrapped the entire logic in an if statement to check that the value isn't set before executing. LOL... I doubt adding another arm to $form_state is a great idea, but it works for now.
I also added a check to make sure arg(1) == "add" so it doesn't happen on an edit operation. Otherwise, after you delete an existing value, it wants to flip to the default value instantly (which would be weird).
Comment #10
vaene commentedHey here is how I pulled it all together. I created a Card Content Type with an Entity Reference, Title, Blurb(body) and Image; plus a couple of other fields. The idea is that the Card would be a wrapper for the Article and the Editor could change the Title, Blurb and Image and still have it link to the Node Referenced in the Entity Reference Field. I then set up the Newsletter so it would pull in 10 Blank Cards and open the forms up so the editors could quickly go down and fill in the content. Peachy. Except now I am trying to get the Title Blurb and Image fields to prepopulate with the Node info from their respective Entity Reference when an article is selected there. Having trouble with the Ajax part of it so if anyone has any suggestions, I'd love to hear them!
Drupal Ajax fun.
This works great on its own, but in the IEF fails cause, hey different form. So I am trying to get the ajax to work when Card is used as an IEF in the Newsletter CT. The code below uses what I found above to make 10 IEFs and open them up for editing. I also use it to put in an AJAX callback per IEF. But now am stuck on what to modify in the form_state and what to return from the form in MYMODULE_newsletter_overrides to get it to work like the card does on its own. As you can see I have even tried ajax_command_invoke to get this working as well, but no dice. Any pointers would be greatly appreciated!
This part brings in 10 ief forms based on the Card CT and opens them up per the suggestions in previous posts.
Comment #11
vyasamit2007 commentedHow do you do this in D8?
Comment #12
mikran commented(this post is about d7)
I didn't want to accept a solution that is essentially rewriting a large part of the widget form. So I did some digging, and I found out a simpler way to prepopulate values to any field. There is no UI for this but fields can be configured to use
default_value_function.So I updated my IEF field:
and after running that you should update the feature containing said field if using features.
Then adding a default value is as easy as:
Comment #13
pianomansam commentedSwitching back to D7 so we can start a new issue for D8.
Comment #14
menteora commented@mikran your solution is great for me!
Is possible append to array (inside 'my_module_default_value_function') a new entity (without target_id)?
I want to save this last new created entity on node 'article' save event.
Thank you very mutch!
Comment #15
menteora commentedI think I must apply solution #3 (thank you @hbemtec) to create new entity reference.
How can I disable new box opens automatically?
(sorry for my english, I attach one picture to clarify my question)
Comment #16
dbiscalchin commentedHi everyone,
Thanks everybody for all these examples. They helped me a lot to get things working.
However, I agree with @mikran and I don't think a solution that basically rewrites a large piece of a third-party code is good enough. Yet I haven't found a better solution to prepopulate with new entities.
My suggestion is to create a hook allowing other modules to provide the entities to be prepopulated by
inline_entity_form_field_widget_form()instead. This hook could be called just after the entities are loaded and added to $form_state (around line 478 of inline_entity_form.module in 7.x branch).Regards,