Drupal 7 & Drupal 8: Customize or remove the node creation message

Eric Toupin

Sometimes you don’t want to show Drupal’s node creation message to your users. Or maybe you’d just like to customize a default message. You might be creating nodes for backend administrative tasks, or you might want your node creation messages to have a little more pizazz than the standard A new node of node type has been created.

Either way, you want to change or remove the node creation message. There are a couple ways to take care of it, but for the sake of copy & paste-ability I’ll go with a blanket solution. In this example, we’ll get rid of the node creation messages in two steps. I’ll touch on customizations towards the end of this post. In both cases, though, we’ll first define a custom form submission handler and then we’ll attach that handler to all of our node forms.

Define a custom form submission handler
Notification messages live in the $_SESSION variable, so our custom form submission handler will inspect the $_SESSION variable and unset the messages we don’t want. We’ll identify the unwanted node creation messages with string position matching. You could also use regular expressions if you need more flexibility. The custom handler works the same whether you’re using Drupal 7 or Drupal 8, but the function declaration is a little different.

// function mymodule_message_grooming_handler(&$form, \Drupal\Core\Form\FormStateInterface $form_state) { // Drupal 8
function mymodule_message_grooming_handler(&$form, &$form_state) { // Drupal 7
 
  // Define an array of the messages you want to remove using their first few words
  $message_removal = array(
    "Basic page",
    "Article",
    "My content type"
  );
 
  // Remove all of the messages that match your conditions
  if (isset($_SESSION['messages'])) {
    foreach ($_SESSION['messages'] as $status => $message_array) {      
      foreach ($_SESSION['messages'][$status] as $key => $value) {
        foreach ($message_removal as $message) {
          if (strpos($value, $message) === 0) {
            unset($_SESSION['messages'][$status][$key]);
          }
        }
      }
 
      // Clean up the $_SESSION array to prevent empty messages from printing to screen
      if (empty($_SESSION['messages'][$status])) {
        unset($_SESSION['messages'][$status]);
      }
    }  
  }
 
}

Attach the custom form submission handler to node forms
Next we want to attach the submission handler to node forms. For this example I’m attaching the handler to every node form for ease of use. If you’d rather specify exactly which forms get the handler attached you could do that with hook_form_FORM_ID_alter(). This code will change a little depending on whether you’re using Drupal 7 or Drupal 8. We’ll do the Drupal 7 example first.

// Drupal 7
function mymodule_form_alter(&$form, &$form_state, $form_id) {
 
  // Attach our custom submit handler to any node form
  if (substr($form_id, -9) == "node_form")  {
    $form['actions']['submit']['#submit'][] = “mymodule_message_grooming_handler”;
  }
 
}
 
// Drupal 8 (we’re targeting node forms in the hook)
function mymodule_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  foreach (array_keys($form['actions']) as $action) {
    if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
      $form['actions'][$action]['#submit'][] = 'mymodule_message_grooming_handler';
    }
  } 
}

That’s it. Make sure your module is enabled, clear your cache and start appreciating your lack of node creation messages. If you’d like to add your own custom messages you could do that in your hook_form_alter() with a drupal_set_message(), your custom submit handler by directly editing the values in the $_SESSION variable, or even with Rules if you’d rather click your way to a solution.


About the Author

Eric has been working with Aten in various capacities since the very beginning, way back in 2000. He has contributed to a full range of business operations at Aten -- both in execution and management -- with experience ranging from technical architecture and development to project management and high-level strategy. Eric is committed to maintaining excellent client relationships through clear...

Read More