1

How to remove any duplicate messages of a certain type.

For example when performing 2 searches using the search api module before rendering the page the message "To perform a search you must enter at least 3 characters" may be displayed twice.

1 Answer 1

1

Use the following function to remove any duplicate messages of a certain type:

/**
 * @param $type - the type of messages that should be checked
 *
 * Removes any duplicate messages from the messages session
 */
function remove_duplicate_messages($type) {
  $stack = array();
  if(isset($_SESSION['messages'][$type])) {
    foreach($_SESSION['messages'][$type] as $k => $msg) {
      if(in_array($msg, $stack)) {
        // Message is already set - remove it
        unset($_SESSION['messages'][$type][$k]);
      } else {
        // Add message to stack
        $stack[] = $msg;
      }
    }
  }
}

Example use:

remove_duplicate_messages('warning');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.