- πΊπΈUnited States smustgrave
In https://www.drupal.org/node/1882428 β looks like the function in question has changed.
So wonder if still a valid task?
hook_field_attach_validate() allows modules to add their own validation for field values. However, the hook is not provided with the original values for the field, which means that in order to validate changes to values, one has to load the entity in the validation hook, something like the following:
function mymodule_field_attach_validate($entity_type, $entity, &$errors) {
// Check for a pre-existing entity (i.e., the entity is being updated).
list($entity_id, , $bundle) = entity_extract_ids($entity_type, $entity);
if ($entity_id) {
// This is gross.
$orig_entity = entity_load($entity_type, array($entity_id));
$orig_entity = $orig_entity[$entity_id];
}
else {
$orig_entity = FALSE;
}
// Now do stuff.
}
Furthermore, even the above workaround will not work for user profile forms, because the $entity
does not even include the uid
in that case.
Pass the original values to hook_field_attach_validate()
in $entity->original
.
The orignal values are not directly available in field_attach_validate(), but they were presumably available to that function's caller. The only caller in core is field_attach_form_validate(), which has both new and original values in the form data.
Review proposed solution and patch.
Related issues:
None.
None. (The actual entity is not being altered; only the "pseudo entity" used during validation.)
Postponed: needs info
9.5
Enhances an existing API or introduces a new subsystem. Depending on the size and impact, possibly backportable to earlier major versions.
After being applied to the 8.x branch, it should be considered for backport to the 7.x branch. Note: This tag should generally remain even after the backport has been written, approved, and committed.
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.
In https://www.drupal.org/node/1882428 β looks like the function in question has changed.
So wonder if still a valid task?