Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Is it possible to add an entity ref field to a form?
would be with a form_alter or where i am actually trying to do it is in the hook_field_settings_form() when defining the settings for a custom field.
i sort of thought something like:
$form['question_reference'] = array(
'#type' => 'entityreference',
'#title' => t('Question Reference'),
'#cardinality' => FIELD_CARDINALITY_UNLIMITED,
'#settings' => array(
'target_type' => 'node',
'target_bundles' => array('question'),
),
);
Comments
Comment #1
epb CreditAttribution: epb commentedDid you ever find a way to do this?
Comment #2
xatoo CreditAttribution: xatoo commented+1 for this
I don't think it is possible right now since hook_element_info() is not implemented in the entityreference module. Therefore it provides no Form API elements. It would be very useful for the cases where we want to use entityreference with unfieldable entity types.
Comment #3
Ludo.RCan someone confirm that this not feasable?
Comment #4
liquidcms CreditAttribution: liquidcms commentedi never did get it working; assumed it wasn't possible.
Comment #5
Ludo.RI also tried with
#type => 'entityreference_autocomplete'
, but it didn't work either.I think #2 is right.
Comment #6
Ludo.RCould the entityreference field be added as a regular Form API element?
Comment #7
jl53k CreditAttribution: jl53k commentedI agree with #2, this would be a very useful feature. Currently I am using a quick and dirty query -> select list approach.c
Comment #8
jl53k CreditAttribution: jl53k commentedI agree with #2, this would be a very useful feature. Currently I am using a quick and dirty query -> select list approach.Sorry, double-posted
Comment #9
Kodiak CreditAttribution: Kodiak commented+1 it would be very neat
Comment #10
kenneth.venken CreditAttribution: kenneth.venken commentedI've spent the last hours figuring out if it was possible to create a form api entityreference element. Attached is a proof of concept patch that creates a very minimal entity reference element.
You can test it out by applying the patch and going to /test which is a simple form with just one entityreference element. The element is an autocomplete field referencing all article nodes.
I try to reuse as much code as possible. The element is actually just an autocomplete textfield for which the autocomplete path is set in a process function. The path contains two parameters: the form id and the element id. The autocomplete callback will load the form, get the element by its id and load its settings. That's about it.
Any feedback is much appreciated.
Comment #11
amitaibuYou can already use field_attach_form(), that will attach a field to any form. Isn't that what you want?
Comment #12
xatoo CreditAttribution: xatoo commented@Amitaibu: No because in your custom modules you can have form elements which aren't fields. For instance, when I have an entity with a schema (a, b, c) and c is an 'id' that references another entity I can not use a field because the data is stored in the entity table itself.
Comment #13
rv0 CreditAttribution: rv0 commented@Amitaibu
field_attach_form will only work on entities for starters
I was looking into replacing the autocomplete function in the OG Menu backend with EntityReference autocomplete.
Will have a look for ideas in the patch provided with #10
Comment #14
kenneth.venken CreditAttribution: kenneth.venken commentedI tried another approach this time. Now i send the entityreference settings as an url parameter. This eliminates the need to keep track of the form to which the element belongs, but it does require encoding the settings array into a very long string and decoding it in the autocomplete callback.
The code is not finished yet, but i'm releasing it so others can improve on it and because i'm not quite sure when i can find the time to work on it again. I've extracted all relevant code from the handlers and put that into EntityReferenceSource classes. These source classes are basically the handlers without all the field specific code. In the future these classes can be shared between the handlers and the entityreference element. I haven't tested the EntityReferenceSourceViews class yet, but the EntityReferenceSourceGeneric class seems to do the job.
Comment #15
Elin Yordanov CreditAttribution: Elin Yordanov commentedHey, I was also looking for a possibility to add a custom entity reference field to my custom form and come across to this module Entity Reference Autocomplete, which is for about 2 weeks around. It looks promising, I'll give it a try.
I just wanted to point it out to the others who are looking for a similar functionality.
Comment #16
liquidcms CreditAttribution: liquidcms commented#15 - works for me
Comment #18
m.stentaThis was not committed, so it shouldn't be marked as closed.
Comment #19
bechtold CreditAttribution: bechtold commentedAs mentioned in #15 there is the Entityreference Autocomplete Module but it is incompatible to this module: #2427817: Enabling this module breaks entityreference moduleComment #20
Anonymous (not verified) CreditAttribution: Anonymous commentedI also agree with this idea +1. I have the same problem here and we should reopen this issue
Comment #21
mark_fullmerAssuming you want an autocomplete type form field, it seems like the solution described here would work for most situations: https://www.drupal.org/node/854216
Requires a page callback to query the desired entity, but that means this is also quite flexible/customizable.
Comment #22
kala4ekYou can use the following module: https://www.drupal.org/project/cerf
It's have only dev version, but you can try it.
Comment #23
bmunslow CreditAttribution: bmunslow at SOCIETAT PORTAL DE LLEIDA S.L commentedCheck out the Entity Reference Autocomplete module, it achieves this functionality and has a stable version.