About the webform module: It always stores the IP with a submission unless it's set as confidential. Is there a way to not store the IP while leaving it as not confidential?

In webform/src/WebformSubmissionForm.php is the function save where it sets the remote_addr. Is there any way to hook this? I don't think it works with a custom webform handler...

Thanks! Looking forward for your answers!


You could override the save function of webform/src/WebformSubmissionForm.php, but as you specifically ask for a hook and because this is probably also the easiest solution you can remove the field value set by the form before the entity gets saved:

use Drupal\webform\WebformSubmissionInterface;

 * Implements hook_ENTITY_TYPE_presave() for webform submissions.
function mymodule_webform_submission_presave(WebformSubmissionInterface $webform_submission) {
  $webform_submission->remote_addr->value = '';

Try to follow this code..

use Drupal\Core\Form\FormStateInterface;

* Implement hook_form_alter()
function my_module_form_alter(&$form,$form_state, $form_id) {
    $form['actions']['submit']['#validate'][] = 'custom_form_validate';                 

function custom_form_validate($form, $form_state) {         
  $ip = \Drupal::request()->getClientIp();
  $form_state->setValue('remote_addr', $ip);
  • I tried it with $form_state->setValue('remote_addr', ''); since I don't want the IP to be stored but this doesn't work. The IP gets stored anyway. However I have to mention that my custom validate function is called via AJAX (see this post). I don't know if it doesn't work because of this and I haven't tested it any other way. – elterr1ble May 8 '18 at 10:05

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.