1

I need to test whether a user is creating a new node as opposed to editing an existing node in a custom #submit function added to a node edit/creation form.

I can access the node via

$node = $form_state->getFormObject()->getEntity();

$node->id() - gives the new node ID so cant be used.

$node->get('nid') - also gives an gives the new node ID so cant be used.

$node->isNew() - This is actually FALSE in this hook despite it being new content.

  • Also, looking inside $node the ['values']['nid']['x-default'] is an empty array when the content is new and filled when it isn't however i cant seem to find a 'getter' to get that information out of the object. – Rob Holmes Sep 6 '16 at 14:22
  • Sounds like the form submit is called after the node was saved. – Kevin Sep 6 '16 at 14:26
  • Yup, that sounds like the case however i need to find a way of making the determination at this point in time. – Rob Holmes Sep 6 '16 at 14:27
  • Could you use hook_node_presave() instead? See: api.drupal.org/api/drupal/core!modules!node!src!Entity!Node.php/… – Kevin Sep 6 '16 at 14:30
  • Sorry, it is now hook_entity_presave: api.drupal.org/api/drupal/… – Kevin Sep 6 '16 at 14:32
4

You could try this method:

if ($form_state->getFormObject()->getOperation() == 'edit') {
  // existing node 
}

These are the operations defined for node:

 "form" = {
   "default" = "Drupal\node\NodeForm",
   "delete" = "Drupal\node\Form\NodeDeleteForm",
   "edit" = "Drupal\node\NodeForm"
 },
  • Ah... this looks like the way forward, when adding a new node at this point i get 'default' but when editing i get 'edit' so i think the is the distinction i am looking for. – Rob Holmes Sep 6 '16 at 14:56
  • You are right. I checked in the node entity. For nodes there is no 'add', I put this information in the answer. – 4k4 Sep 6 '16 at 15:16
  • This is an interesting question, and curiosity got to me and I had to trace this out. EntityForm sets the submit handlers for Save to [::submitForm, ::save]. This means that NodeForm::submitForm then NodeForm::save get called, the later of which sets up the form redirect. So, to override the redirect, you have to do this after; so have to have the custom submit handler on the end of the array (which the OP did). I am curious, though, whether there is a better option in $form or $form_state to use. – mpdonadio Sep 6 '16 at 23:29
  • There should be no problem with getOperation(), but you have to check in the entity type definition. If 'add' and 'edit' is not defined, drupal uses 'default' when building the route, see api.drupal.org/api/drupal/… – 4k4 Sep 7 '16 at 7:00
0

You can implement hook_entity_presave to look at an entity on create/update:

https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!entity.api.php/function/hook_entity_presave/8.2.x

Just implement some logic to check that the entity is a Node, and that it is either isNew() or has no id(), then put your logic inside of that.

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.