Now we have entities, I seem to be replicating the author form element from node module a lot.

It would be handy to have it as a ready-rolled form element.

CommentFileSizeAuthor
#1 1620954.elements.author-field.patch1.76 KBjoachim
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joachim’s picture

Status: Active » Needs review
FileSize
1.76 KB

Here's a patch that seems to work :)

A nice bonus feature would be an option to set the value to the global $user if the value is empty, but I'm not sure how best to do that.

joachim’s picture

> A nice bonus feature would be an option to set the value to the global $user if the value is empty,

I've been thinking more about this, and I see two options:

A. Set '#default_value' to the magic value of 'user' to mean 'put in the current user's UI'.

Pro: it's a one-liner to code:

#default_value' => isset($mydata->uid) ? $mydata->uid : 'user',

Con: it's a magic value, and those are bad.

B. Add a key '#use_global_user_if_empty'.

Pro: it's explicit.
Con: it's two lines of code, so it's not actually a saving over having to do a 'global $user;' yourself. And the key name is a bit wordy.

#default_value' = isset($mydata->uid) ? $mydata->uid : NULL,
'#use_global_user_if_empty' => TRUE,

I'm leaning towards B or neither.