Problem/Motivation
- When updating an existing user's data, the uid for the user is not available in hook_field_attach_validate().
- The issue originates in entity_form_field_validate(), which passes on a "pseudo-entity" generated from the form values:
$pseudo_entity = (object) $form_state['values']; field_attach_form_validate($entity_type, $pseudo_entity, $form, $form_state); - Most fieldable core entities include the entity id in the form values. (See, for example, node_form().) Therefore, the
$pseudo_entityincludes the id as expected in those cases. - However, user_profile_form() does not use this pattern. It instead stores the entity data to
$form_state['user'], which is not available in the field attach hooks.
Proposed resolution
Standardize entity CRUD forms. Or, at a minimum, provide the full, complete entity in a consistent place in $form_state, and use this somehow in entity_form_field_validate().
The workaround for this issue is to look up the user by name in hook_field_attach_validate():
function mymodule_access_field_attach_validate($entity_type, $entity, &$errors) {
if ($entity_type == 'user') {
if ($account = user_load_by_name($entity->name)) {
$entity->uid = $account->uid;
}
}
Remaining tasks
TBD
Related issues:
- #1220212: Provide original entity in hook_field_attach_validate()
- #367006: [meta] Field attach API integration for entity forms is ungrokable
User interface changes
None.
API changes
TBD
Comments
Comment #1
xjmThe workaround above was used to resolve #1237054: User form changes not validated properly because of core bug.
Comment #1.0
xjmUpdated issue summary.
Comment #1.1
xjmUpdated issue summary.
Comment #2
sunI think this should be fixed in a generic way in #1220212: Provide original entity in hook_field_attach_validate()
We can keep this issue to specifically add
to user_profile_form()
Comment #2.0
sunUpdated issue summary.
Comment #3
berdirNo longer an issue in 8.x, so moving to 7.x
Comment #4
drikc commentedThe workaround in the issue summary which use user_load_by_name($entity->name) to retrieve the missing uid works only if the username isn't modified. The following workaround use the solution mentioned in comment #2: