When should I use hook_field_attach_presave(), and when hook_entity_presave()? I was looking for a way to act on entity fields before they are saved, and I have noticed the following about hook_field_attach_presave(), and hook_entity_presave():


They take the same parameters, but in a different order.
They are both called when an entity is saved. For example, taxonomy_term_save() contains the following code.

// Invoke hook_field_attach_presave().
field_attach_presave('taxonomy_term', $term);

module_invoke_all('taxonomy_term_presave', $term);
module_invoke_all('entity_presave', $term, 'taxonomy_term');


Similar code is present in comment_save().

field_attach_presave('comment', $comment);

// Allow modules to alter the comment before saving.
module_invoke_all('comment_presave', $comment);
module_invoke_all('entity_presave', $comment, 'comment');



As they get the same parameters, the code I should write to access the entity's fields is the same for both the hooks. This means that, apparently, there isn't any reason to choose one instead of the other hook.

When should I then implement one hook instead of the other? Are there cases where I would implement hook_field_attach_presave()?
It depends on what type of data you are trying to modify.  Some entities are not fieldable. Those entities would never call hook_field_attach_presave() where as they would call hook_entity_presave().

If you are modifying an entity's base properties such as 'title', you should use hook_entity_presave(). If you are modifying the fields attached to an entity, you should use hook_field_attach_presave().

Keep in mind that other modules might be using these hooks as well. They would expect you to be making your modifications to the type of data in its respective hooks.