Get form ID on hook_entity_presave()

by RubberDuckIsKing   Last Updated January 11, 2018 16:07 PM

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?

Tags : 8 hooks

Answers 4

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';
Yakimkin Roman
Yakimkin Roman
December 20, 2016 10:42 AM

Try :

function mymodule_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
    //EDIT Entity here
A Ajay Reddy
A Ajay Reddy
December 20, 2016 11:13 AM

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'.

December 20, 2016 13:00 PM

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.

Master Fuddel
Master Fuddel
January 11, 2018 15:17 PM

Related Questions

How to find hooks being called by a Drupal page?

Updated April 03, 2015 21:20 PM

Modify text of error message for required field

Updated April 22, 2015 20:03 PM

Where are hooks stored/cached?

Updated April 13, 2015 20:03 PM