5

In Drupal 7 we were able to use field_attach_update($entity_type, $entity) to update fields without having the need to update the whole node. $node->save() is very heavy and calls many hooks in between.

With the coming of Drupal 8, field_attach_update was deprecated. Is there an alternative to it, and what is it if yes?

In my case I'm updating two image fields programatically and don't want to call $node->save in order to not trigger all the other hooks.

1

1 Answer 1

3

There is no alternative, saving an entity is the only supported API.

Saving will only update the configurable field tables if it detects a change, and we plan to implement similar logic for the base tables as well.

But the hooks are still called and there is no supported way to prevent that.

4
  • Can we assume that there are no (current) plans to implement support for a hook bypass of some sort? Do you think it'd be worth a feature request or is it doomed to failure because of the way the system needs to work? I also used to use this feature regularly on heavy D7 sites, it'd be good to know if it's going to be re-implemented at some point
    – Clive
    Commented Apr 20, 2017 at 8:41
  • I think it's unlikely that something would get added, field_attach_update() and so on was never meant to be an API either, there was just no way to force it to be internal in the 7.x API. Not calling the hooks could result in all kinds of data inconsistencies with workflows, search indexes and many other things that rely on those hooks
    – Berdir
    Commented Apr 20, 2017 at 22:54
  • What if we call a email_send() function when the node is saved but would like to avoid that when the node is updated programatically. Also in my case, I generate programatically 2 fields based on the form values and would like to persist them without having to save the node again. Updating the db directly is not safe.
    – 113408
    Commented Apr 21, 2017 at 8:06
  • If it's your own hook, you can always do something like setting a flag, either on the node object like $node->skipMails = TRUE; or some static/global/service thing.
    – Berdir
    Commented Apr 21, 2017 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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