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

patmacs’s picture

Subscribing

BrockBoland’s picture

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

lesleyfernandes’s picture

Issue summary: View changes

Someone 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

lesleyfernandes’s picture

Version: 7.x-2.x-dev » 7.x-2.1
Component: Documentation » Code: user_reference
lesleyfernandes’s picture

I found this one for the node reference field:

$field = array(
    "field_name"=>;"field_field_name",
    "label"=>"Field label",
    "type"=>"node_reference",
    "settings"=>array(
      "referenceable_types"=>array(
        "gallery_image"=>"gallery_image"
      ),
    ),
    "cardinality"=>"-1"
  );
 
  field_create_field($field);
 
  $instance = array(
    "field_name"=>"field_field_name",
    "label"=>"Field label",
    "type"=>"node_reference",
    "widget"=>array(
      "type"=>"options_select"
    ),
  );
 
  $instance["entity_type"] = "node";
 
  foreach(array("type1", "type2", "type3") as $type){
    $instance["bundle"] = $type;  
    field_create_instance($instance);
  }

Source: http://numiko.com/labs/2011/12/programmatically-adding-a-node-reference-...

lesleyfernandes’s picture

I did one for user reference:

$field = array(
      'field_name' => 'FIELD_NAME',
      'label' => t('LABEL'),
      'description' => t('DESCRIPTION.'),
      'type' => 'user_reference',
      'settings' => array(
        'referenceable_roles' => array(2 => 'authenticated user'), // for authenticated users
        'referenceable_status' => array(1 => 'active'), // only active users
      ),
      'cardinality' => -1, //ilimited field values
  );
  field_create_field($field);


  $instance = array(
      'field_name' => 'FIELD_NAME',
      'label' => t('LABEL'),
      'description' => t('DESCRIPTION.'),
      'required' => TRUE,
      'type' => 'user_reference',
      'widget' => array(
        'type' => 'userreference_autocomplete',
      ),
      'settings' => array(
        'cardinality' => -1,
      ),
  );


  $instance["entity_type"] = "node";


  foreach(array("type1", "type2", "type3") as $type){
    $instance["bundle"] = $type;  
    field_create_instance($instance);
  }