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'),
        ),      
      );
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

epb’s picture

Did you ever find a way to do this?

xatoo’s picture

+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.

Ludo.R’s picture

Can someone confirm that this not feasable?

liquidcms’s picture

i never did get it working; assumed it wasn't possible.

Ludo.R’s picture

I also tried with #type => 'entityreference_autocomplete', but it didn't work either.

I think #2 is right.

Ludo.R’s picture

Title: possible to add an entityreference field to a form? » Provide Entityreference as a Form API element
Category: support » feature

Could the entityreference field be added as a regular Form API element?

jl53k’s picture

I agree with #2, this would be a very useful feature. Currently I am using a quick and dirty query -> select list approach.c

jl53k’s picture

I 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

Kodiak’s picture

+1 it would be very neat

kenneth.venken’s picture

Status: Active » Needs work
FileSize
3.5 KB

I'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.

amitaibu’s picture

You can already use field_attach_form(), that will attach a field to any form. Isn't that what you want?

xatoo’s picture

@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.

rv0’s picture

@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

kenneth.venken’s picture

I 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.

Elin Yordanov’s picture

Hey, 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.

liquidcms’s picture

Issue summary: View changes
Status: Needs work » Fixed

#15 - works for me

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

m.stenta’s picture

Status: Closed (fixed) » Needs work

This was not committed, so it shouldn't be marked as closed.

bechtold’s picture

As mentioned in #15 there is the Entityreference Autocomplete Module but it is incompatible to this module: #2427817: Enabling this module breaks entityreference module

Anonymous’s picture

I also agree with this idea +1. I have the same problem here and we should reopen this issue

mark_fullmer’s picture

Assuming 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.

kala4ek’s picture

You can use the following module: https://www.drupal.org/project/cerf
It's have only dev version, but you can try it.

bmunslow’s picture

Check out the Entity Reference Autocomplete module, it achieves this functionality and has a stable version.