[development] Programatically creating users w/Field API data
Rob Thorne
rob at torenware.com
Mon Jun 13 22:59:25 UTC 2011
I'm looking for the "recommended recipe" for a common task -- creating
users programatically. I'm trying to do this in D7 more or less the way
it was done in D6 -- create an $edit array with the key attributes
(name, mail, status), and passing it to user_save(). This works, but
while I can save away Field API fields this way, the hack I'm using
feels too low level.
Here's what I mean. As far as I can tell, you need to do something like
this:
$edit = array(
'name' => "Killroy",
'mail' => 'killroy at washere.net",
'status' => 1,
'field_graffiti_location' =>
array(
'und' => array(0 => array('value' => '2nd and Main')),
),
);
Pass this as user_save(NULL, $edit), and if users are set up with the
field 'field_graffiti_location', and it does work -- user_save() calls
the needed Field API calls to attach the data.
Now, I worked this out using a debugger -- I haven't found any example
code for this so far. But it strikes me as a very low level way of
doing this, and fragile. Given that Fields were intended to make life
simpler, I'm thinking There's Gotta Be A Better Way To Do This.
Especially when we start talking about multi-valued fields and/or fields
that take advantages of multi-lingual content.
Is there? Are there recommended setters and getters for fields attached
to an instance of an entity, or do developers need to muck around with
what are basically Field API internals in order to do this?
Rob Thorne
Torenware Networks
More information about the development
mailing list