I want to modify the contents of a field of an Article node just before it comes up for editing. I have read the documentation at http://api.hostdog.gr/api/drupal-8/drupal%21core%21modules%21node%21node.api.php/function/hook_prepare/8, which states that hook_prepare will "Act on a node object about to be shown on the add/edit form." I've read lots of other stuff too.

I've tried lots of ways of naming the function:

and on and on

But the function is never called. I've tried putting these functions in both the MyModule.module file and in the MyTheme.theme file. I've read countless posts. Nothing has caused my function to be invoked (I output to the error log to tell it got called).

I simply want to look at one field in the node and possibly modify it just before it comes up in the add/edit form for the user to edit it. I feel really dumb; this has got to be dead simple and I'm just not getting it.

1 Answer 1


The code that is provided by the page that you linked to is wrong. While this hook exists in D7 and while it is possible that during early D8 core development, in alpha or beta, it existed at one point. In D8 there is no such thing as hook_prepare.

As per the list of all D8 core hooks, it looks to me that hook got replaced with a newcomer hook called hook_ENTITY_TYPE_prepare_form

 * Implements hook_ENTITY_TYPE_prepare_form
function mymodule_node_prepare_form(\Drupal\node\NodeInterface $node, $operation, \Drupal\Core\Form\FormStateInterface $form_state) {
  if ($node->getType() == 'article') {
    // your code goes here

and as always, anytime you implement a new hook, for the first time, clear the cache so it gets picked up by Drupal.

  • Thanks a ton, @No Sssweat. That was it! May 21, 2019 at 14:28
  • @MarshallMorrise you may have probably done this already, but just in case, updated my answer with a conditional as it looks like you don't want this code to fire on every content type.
    – No Sssweat
    May 22, 2019 at 7:47

Your Answer

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

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