If you have an entity reference field, you need to refer to the entity with it's name/title/label field (this applies for taxonomy terms too). Like you did it.
This code will run for your association:
/**
* Entityreference field handler for Drupal 7.
*/
class EntityreferenceHandler extends AbstractHandler {
/**
* {@inheritdoc}
*/
public function expand($values) {
$entity_type = $this->fieldInfo['settings']['target_type'];
$entity_info = entity_get_info($entity_type);
// For users set label to username.
if ($entity_type == 'user') {
$entity_info['entity keys']['label'] = 'name';
}
$return = array();
foreach ($values as $value) {
$target_id = db_select($entity_info['base table'], 't')
->fields('t', array($entity_info['entity keys']['id']))
->condition('t.' . $entity_info['entity keys']['label'], $value)
->execute()->fetchField();
if ($target_id) {
$return[$this->language][] = array('target_id' => $target_id);
}
}
return $return;
}
}
Which also means, the first item will be tried to associate with your user, which doesn't mean, that it can be (i.e. for content type restriction). So make sure you have only one entity which named like you want to associate. Also make sure, you don't have ', ' and ' - ' in your value, because those will be converted to multi value, and if ' - ' is present than the ': ' value will be converted to key: value
pairs. You need to say exactly the entity name which is possible to refer inside that field (but as far as I can see in the code, make sure you don't have space before and/or after the name field's value, because the query above may fail to find your entity). Also check if your field is a real field, (not like the path 'field', which is not a field) you can do it, be executing the field_info_field_map()
command, and if you find your field machine name there, as a key, you are fine.
If all the above is fulfilled and you already has a node which can be referred by the user's field_lines_ref
field and the node title is 'Line Name' you should be able to create your user with the reference:
Given users:
| name | pass | mail | status | roles | field_lines_ref |
| test | test | [email protected] | 1 | Tester | Line Name |
From features file or
$user_obj = (object) array(
'name' => 'test',
'pass' => 'test',
'mail' => '[email protected]',
'status' => '1',
'roles' => 'Tester',
'field_lines_ref' => 'Line Name',
);
$user = $this->userCreate($user_obj);
from context object.
If these doesn't help, you may need to debug your own code (including vendor code), to see where is the problem.
No entity 'Line Name' of type 'node' exists. (Exception)
? More info might help.field_lines_ref
simply doesn't exist as a field (among other possible problems.) What Drupal major version are you using? I only ask because: couldfield_lines_ref
actually be a field on a separate profile entity? (It's also possible that the extension drivers just don't support fields on users; whatapi_driver
are you using? Try "drupal" rather than "drush".)