I need to override hook_field_validate() for a particular field (provided by the Video Embed Field module). The description for that hook states:

Validate this module's field data.

So the idea is that it's only invoked for the module that implements a particular field, which makes sense.

Unfortunately there's no altering mechanism in place (that I can find) so the implementations of those hooks are pretty much hard-coded. Also the hook is never run through module_implements() (as you would expect), so in turn is not available to be overridden using hook_module_implements_alter().

Does anyone know of a way, however unclean, of overriding hook_field_validate() for a particular field implementation, without hacking any core/contrib modules?

  • 1
    The code is aiming a specific function that must be implemented in a specific module (the one implementing the field); that is why the code doesn't use module_implements(), nor module_invoke_all(). As consequence, hook_module_implements_alter() is not invoked too. – kiamlaluno Jul 28 '12 at 15:48
  • Yeah I understand why it does that (similar to the hooks invoked for node type modules), I'm just trying to find a way to override it. @Oswald's solution looks promising I'm just testing it out – Clive Jul 28 '12 at 15:54

There is hook_field_attach_validate(), which is invoked by the single function that invokes hook_field_validate(), namely field_attach_validate(), which is called by field_attach_form_validate().

I did not find an obvious way to get rid of the original validation function, but a quick hack would be to remove the errors, that the original validation function produced, from the $errors array in your hook_field_attach_validate() implementation.

| improve this answer | |
  • 1
    As the errors are just returned in that $errors array, and they are not yet printed with drupal_set_message(), altering the array is the way to go. I was going to answer the question, and I would have given the same answer. – kiamlaluno Jul 28 '12 at 15:58
  • Thanks both this is definitely the way to go. I had to abandon the whole thing unfortunately because of limitations in the video embed module but I tested this and it works great – Clive Jul 28 '12 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.