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.
I was wondering if someone (possibly one of the project maintainers) could post an example of how to programmatically create a reference field.
Something along the lines of :
$form['foo'] = array(
'#type' => 'node_reference',
... list of attributes and brief description here ...
);
would be more than sufficient for many of us.
I'm assuming that as a field that uses the Field API, it should be quite easy to add a reference field to a form using the standard Form API, but without any documentation, many of us developers are at a loss to both help respond to questions in this issue queue or to accomplish the functionality that we'd like with References.
Comments
Comment #1
patmacs CreditAttribution: patmacs commentedSubscribing
Comment #2
BrockBoland CreditAttribution: BrockBoland commentedHas anyone figured out how to do this? I'm a little shaky on this part of the Form API, but I think that the module would need to implement hook_element_info() in order to make the node or user reference fields available to the Form API for use in custom forms.
Comment #3
lesleyfernandes CreditAttribution: lesleyfernandes commentedSomeone can help us?
I am creating a custom node type programmatically and I need to add two user reference fields in my .install file, do you have an example of how to create a field and a field instance of it?
Thank you
Comment #4
lesleyfernandes CreditAttribution: lesleyfernandes commentedComment #5
lesleyfernandes CreditAttribution: lesleyfernandes commentedI found this one for the node reference field:
Source: http://numiko.com/labs/2011/12/programmatically-adding-a-node-reference-...
Comment #6
lesleyfernandes CreditAttribution: lesleyfernandes commentedI did one for user reference: