I have a user entity reference field. When I type in a name, but the entity/user doesn't exist I would like to save the name as a plain text value. Is this possible?
Thanks.
I have a user entity reference field. When I type in a name, but the entity/user doesn't exist I would like to save the name as a plain text value. Is this possible?
Thanks.
Instead of adding an user entity field you can create a normal text field and then use formalter function to make it an auto complete.
The steps would be as follows.
Create a custom module.
First you would need to create a menu item to get the list of users to do that you can create a hook_menu function
function module_name_menu() {
$items['userlist/autocomplete'] = array(
'page callback' => '_module_name_autocomplete',
'access arguments' => array('you can define the permissions here'),
'type' => MENU_CALLBACK
);
return $items;
}
The next step would be to create the call back function.
function _module_name_autocomplete($string) {
$matches = array();
// Some fantasy DB table which holds cities
$query = db_select('users', 'u');
// Select rows that match the string
$return = $query
->fields('u', array('name'))
->condition('c.name', '%' . db_like($string) . '%', 'LIKE')
->range(0, 10)
->execute();
// add matches to $matches
foreach ($return as $row) {
$matches[$row->name] = check_plain($row->name);
}
// return for JS
drupal_json_output($matches);
}
The last step would be to create a form alter function to
function module_name_form_page_node_form_alter(&$form, &$form_state, $form_id) {
$form['field_name']['#autocomplete_path'] = 'userlist/autocomplete';
}
This would change your textfield into an autocomplete field. you would also be able to enter any other name as well.