Search code examples
drupalhookforumdrupal-hooks

Hide the "Created new forum XXX" message in Drupal


Every time I create a new forum using the API, the message:

Created new forum blah blah

appears (status message).

Can I suppress it? maybe with a hook?


Solution

  • There is a module that creates a hook that you can use to alter messages. http://drupal.org/project/messages_alter

    I think that it would work for your use case, however if you need something it doesn't offer or just want to roll your own option: A quick look at the module will give you ideas on how you can create your own implementation if you need it.

    I honestly can't remember why we did it ourselves, instead of using the module, but here is some really simple example code.

    /**
     * function to check the messages for certian things and alter or remove thme.
     * @param $messages - array containing the messages.
     */
    
    function itrader_check_messages(&$messages){
      global $user;
      foreach($messages as &$display){
        foreach($display as $key => &$message){
        // this is where you'd put any logic for messages.
          if ($message == 'A validation e-mail has been sent to your e-mail address. In order to gain full access to the site, you will need to follow the instructions in that message.'){
            unset($display[$key]);
          }
          if (stristr($message, 'processed in about')){
            unset($display[$key]);
          }
        }
      }
      // we are unsetting any messages that have had all their members removed.
      // also we are making sure that the messages are indexed starting from 0
      foreach($messages as $key => &$display){
        $display = array_values($display);
        if (count($display) == 0){
           unset($messages[$key]);
         }
      }
      return $messages;
    } 
    

    Theme Function:

    /**
     * Theme function to intercept messages and replace some with our own.
     */
    function mytheme_status_messages($display = NULL) {
      $output = '';
      $all_messages = drupal_get_messages($display);
      itrader_check_messages($all_messages);
      foreach ($all_messages as $type => $messages) {
        $output .= "<div class=\"messages $type\">\n";
        if (count($messages) > 1) {
          $output .= " <ul>\n";
          foreach ($messages as $message) {
            $output .= '  <li>'. $message ."</li>\n";
          }
          $output .= " </ul>\n";
        }
        else {
          $output .= $messages[0];
        }
        $output .= "</div>\n";
      }
      return $output;
    }