Using Node Reference in a custom module form

Events happening in the community are now at Drupal community events on www.drupal.org.
noobizness's picture

Has anyone got a nodereference form to work correctly in a custom module (in autocomplete mode?) It seems that the autocomplete ajax function expects more data than exists and so constantly returns an empty array [ ] of options.

Any suggestions for using nodereference in a custom module form?

Thanks

Comments

This might be a place to start

cluke009's picture

There seems to be very little info on the topic at the moment. I have been digging through the node reference module for a few hours now and have gained little understanding on the subject.

The code below seems to be enough to get an autocomplete field on the page but there are large gaps in my understanding with regard to the '#value_callback'. I believe this is how we get are autocomplete result but I am not sure of what exactly is going on here.

<?php
/**
* my_module.module
*/

/**
* Implement hook_menu().
*/
function my_module_menu() {
 
$items = array();

 
// Create path to form at example.com/my_module/nodereference
 
$items['my_module/nodereference'] = array(
   
'title' => t('Custom Node Reference'),
   
'page callback' => 'drupal_get_form',
   
'page arguments' => '', // Not sure what goes here.
   
'access callback' => TRUE,
  );

  return
$items;
}

/**
* Implement hook_form().
*/
function my_module_form($form, &$form_state) {

 
$form = array();

 
$form['field_nodereference'] = array(
   
'#type' => 'nodereference_autocomplete',
   
'#title' => 'Nodereference Field',
   
'#value_callback' => 'nodereference_options',
  ); 

  return
$form;
}

/**
* Taken from modules/cck/modules/nodereference/nodereference.module
*
* Value for a nodereference autocomplete element.
*
* Substitute in the node title for the node nid.
*/
function nodereference_options($element, $edit = FALSE) {
 
$field_key  = $element['#columns'][0];
  if (!empty(
$element['#default_value'][$field_key])) {
   
$nid = $element['#default_value'][$field_key];
   
$value = db_result(db_query(db_rewrite_sql('SELECT n.title FROM {node} n WHERE n.nid = %d'), $nid));
   
$value .= ' [nid:'. $nid .']';
    return array(
$field_key => $value);
  }
  return array(
$field_key => NULL);
}

?>

I think I am on the right track here but I am not a php guru by any stretch.

Did you manage to get the

pvanerk's picture

Did you manage to get the nodereference_select working in a custom form? I am looking for the same thing. Thanks!

This worked for me

agrivas's picture

I am not sure if this is what you were looking for, but the following code worked for me. I did not use the nodereference module but I managed (quite easily) to create an autocomplete textfield with values from a view, so practically you can pull any kind of field that has implemented a views handler (CCK etc)

<?php
/**
* my_module.module
*/

/**
* Implement hook_menu().
* The page callback must be the name of the function that selects all the possible values.
*/
function my_module_menu() {
 
$items = array();

 
$items['my_module/autocomplete'] = array(
   
'title'  => 'Autocomplete for my module',
   
'page callback' => 'my_module_autocomplete',
   
'type' => MENU_CALLBACK,
  );

  return
$items;
}

/**
* Implement autocomplete function.
* I created a view that selects all the nodes of a certain content type (filters views section)
* and in the fields section I selected the node title and node id.
* You can obviously select any kind of field from any kind of content type.
*/

function my_module_autocomplete() {
 
$the_results = views_get_view_result('<enter view name here>', 'default'); 
  
 
$results = array();
  foreach (
$the_results as $result) {
     
$results[$result->node_title . ' [nid:' . $result->nid . ']'] = $result->node_title;
  }

  print
drupal_to_js($results);
  exit();
}

/**
* Implement hook_form().
* The autocomplete_path must be the path set from our hook_menu() function
*/
function my_module_form($form, &$form_state) {

 
$form = array();

 
// The other form fields can go here

 
$form['autocomplete_id'] = array(
   
'#type' => 'textfield',
   
'#autocomplete_path' => 'my_module/autocomplete'
 
);

  return
$form;
}

/**
* Implement hook_form_submit()
* The autocomplete textfield is in the following format: <title>[nid:<nid>]
* so I had to parse it and store only the nid in my database.
*/
function my_module_form_submit($form, &$form_state) {
 
$record = array();

 
// Store any other fields from $form_state['values'] that you need

  // This will get rid of the node title and store only the nid value.
 
$autocomp_id = explode('[nid:', $form_state['values']['autocomplete_id']);
 
$autocomp_id = trim($autocomp_id[1], "]");
 
$record['<db field name id>'] = $autocomp_id;

 
$result = drupal_write_record('<db table name>', $record);
 
$form_state['redirect'] = "my_module"; // or any path you want
}

?>

I hope you will find it helpfull. :-)

Use the Entity Reference Autocomplete module

andy_read's picture

I know this is a very old thread, but it came up in a google search, so I thought I'd add the answer I eventually found: Use the Entity Reference Autocomplete contrib module, which was developed later (https://www.drupal.org/project/entityreference_autocomplete).

SF Bay Area

Group categories

Resources

user group

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: