I want to process some details before saving the node in Drupal 8. How will I get the form ID of the submitted form?

function mymodule_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
  $entity->getEntityType()->id() // this only prints "node"

Is there any other workaround?

  • Why do you need the form ID? What are you trying to achieve with that? – kiamlaluno Dec 20 '16 at 10:51
  • well i want to know whether its edit form (existing node) or save form (new node) – Crazyrubixfan Dec 20 '16 at 10:54
  • Then, you don't need the form ID, which would be the same in both the cases. – kiamlaluno Dec 20 '16 at 10:55
  • 1
    @IcecreamJelly, you can't get the form_id in presave. The entity doesn't even know if there is a form, because you can save entities without form. But you can check, if the entity is new: $entity->isNew(). – 4k4 Dec 20 '16 at 11:03

hook_entity_presave() doesn't get the form ID, and this makes sense: An entity could be programmatically saved from a module or a Drush plugin without showing to the user a form to edit/create the entity.

Since you just want to know if the entity is being edited or created:

  • You don't need to know the form ID
  • You can use hook_entity_presave() or hook_ENTITY_TYPE_presave() with code similar to the following one

    function mymodule_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
      if ($entity->isNew()) {
        // The entity is being created.

As side note, $entity->getEntityType()->id() returns 'node' as expected: You are asking to the entity object its entity type, and for nodes that is clearly 'node'.


To answer your question without going into the details as to why you needed it. (others have answered the solution to your original problem)

However, you can get the form_id from the request:


You should check if the variable is set in the first place and then check its value.
I had a specific case where i needed to take action on an entity save from a specific form but not on any other case.

This was the only solution i came up with.

  • Thanks for answering the original question, this is what I was looking for. In my case, I have a presave hook that performs some book-keeping, but only wanted it called if an editor actually used the proper form -- not if the entity was saved programmatically. – Kimberly W Jul 25 '18 at 20:12

Try :

function mymodule_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
    //EDIT Entity here

I think that hook_entity_presave won't help you.

But you can use hook_form_alter on your node form and in code of the hook add one more submit function, which would be executed when the node form is submitted.

Anyway, in Drupal 7 the solution was as follows.

By the way, here is code, where one can see an example of addition of submit function

 function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if (isset($form['type']) && $form['type']['#value'] . '_node_settings' == $form_id) {
    $upload_enabled_types = \Drupal::config('mymodule.settings')->get('upload_enabled_types');
    $form['workflow']['upload_' . $form['type']['#value']] = array(
      '#type' => 'radios',
      '#title' => t('Attachments'),
      '#default_value' => in_array($form['type']['#value'], $upload_enabled_types) ? 1 : 0,
      '#options' => array(t('Disabled'), t('Enabled')),
    // Add a custom submit handler to save the array of types back to the config file.
    $form['actions']['submit']['#submit'][] = 'mymodule_upload_enabled_types_submit';
  • i have to execute it before the node save as i said earlier . but hook_form_alter works after the node save – Crazyrubixfan Dec 20 '16 at 10:48
  • As the answer says, you would add a form submission handler, which would execute before the node is saved, if you add it in the right order. Passing the form ID to the form submission handler isn't difficult. – kiamlaluno Dec 20 '16 at 10:54

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.