Skip to header Skip to main navigation Skip to main content Skip to footer
  • Log in
Home
DrupalVIP
Freelancer service with great care and enthusiasm.
  • Home
  • Products
  • Blog
  • Support
    • Dictionary
    • Drupal Resources
    • External Resources

Success Message After Submitting Form

Breadcrumb

  • Home
  • DrupalVIP Support
  • Success Message After Submitting Form

In building a custom form you must keep in mind a few issues: elements, functionality, and response for best user experience

Review the following code, you can see the submit method doing few things:

  1. updating logger
  2. set message to user
  3. redirect the response to specific page

 

This code is working on Drupal 7 to 8.6

public function submitForm(array &$form, FormStateInterface $form_state) {
  $this->logger('user')->notice('Deleted %ip', ['%ip' => $this->banIp, ]);
  drupal_set_message($this->t('The IP address %ip was deleted.', [ '%ip' => $this->banIp, ]));
  $form_state->setRedirectUrl($this->getCancelUrl());
}

 

from drupal 8.6, which become more service activation is written a bit differently

public function submitForm(array &$form, FormStateInterface $form_state) {
  $this->logger('user')->notice('Deleted %ip', ['%ip' => $this->banIp,]);
  $this->messenger()->addStatus($this->t('The IP address %ip was deleted.', [    '%ip' => $this->banIp,  ]));
  $form_state->setRedirectUrl($this->getCancelUrl());
}

drupal_set_message was deprecated in instead you should use the messenger service

$this->messenger() returns an instance of the messenger service, the service that replaced drupal_set_message() in Drupal 8.

 

Drupal Development

Drupal Development
Code Snippet
public function submitForm(array &$form, FormStateInterface $form_state) {
  $this->logger('user')->notice('Deleted %ip', ['%ip' => $this->banIp,]);
  $this->messenger()->addStatus($this->t('The IP address %ip was deleted.', [    '%ip' => $this->banIp,  ]));
  $form_state->setRedirectUrl($this->getCancelUrl());
}
Read More
Tags
Drupal Support
Drupal 10
Messenger Service
drupal_set_message
setRedirectUrl
Code Example
Code Snippet
Logger Service

The Freelancer Assistance

Drupal Development

Drupal Development

Site Management

Site Management

Drupal Training

drupal training session

Proactive Maintenance

Proactive Maintenance

Drupal Shared Space

Drupal Shared Space

Fullstack Service

Fullstack Service
Review All Services

Buy Hourly Support

 

Drupal platform support for complex websites. The service includes support, training, troubleshooting, fixes, updates, tool creation and module building.
* Minimum order is for 5 hours of support
* For every 20 hours of order you will be entitled to an additional hour of support
* For every 50 hours of order you will be entitled to 5 additional hours of support

 

>> Payment <<

 
cards

מופעל על-ידי paypal

Legal

  • Home
  • Contact
  • Products

Footer menu

  • Home
  • Contact
  • Products

Copyright © 2026 DrupalVIP project of Automatic Frameworks - All rights reserved

Developed and Maintain by Jonathan Ben Hur Freelancer