3

I'm trying to the node created messages for a certain content type, but I can't use Disable Messages because it has a conflict the Commerce Add to Cart Confirmation module.

All the other methods (like the ones here) I've found either hide all status messages, or hide all status messages after a content type is created, which are inappropriate for me since I'm using rules to show a messages after a node is created, but I need to hide the default "__________ has been created" message, and I appreciate any help you can offer me.

0

7 Answers 7

7

drupal_set_message is not hookable. But the message you want removed comes from node_form_submit, so you can get rid of it using hook_form_FORM_ID_alter on node_form. Either replace node_form_submit as form's submit handler with it's clone, sans messages, or set second handler that will remove what you want removed from $_SESSION['messages'][$type].

2
  • Thanks! Could you please provide me with more information? I've never used $_SESSION['messages'][$type] before. I've been looking for information for 4 days but I'm still not sure of what to do, or I'm looking at the wrong places. I've found information that doesn't support replacing node_form_submit with a clone which was why I prefer using $_SESSION['messages'][$type].
    – ashash
    Jul 14, 2014 at 19:20
  • 4
    Look at answer by Parvind - it is almost OK. Just, instead of unset($_SESSION['messages']);, use var_dump or kpr to see what's in it. Then you will know what exactly you are looking for to unset. For your particular situation $type = 'status', so just unset last element of $_SESSION['messages']['status'] if it's what you don't want it to be.
    – Mołot
    Jul 15, 2014 at 6:36
2

While I was searching for solutions before I received the helpful reply from Mołot, I've found this module that fits my needs: Custom Submit Messages

It allows me to customise or remove any node creation, update, or deletion message in the content type->edit form.

(I had to disable messages displayed with Rules "After a node is created" to make it work as intended)

I appreciate your time and advice, but I've decided to go ahead with this because I plan to modify many more messages, the way this module allows and it will allow me to save a lot of time from doing it programatically.

1
  • I cannot mark this as the answer because the title of the question pertains to hiding the messages programmatically, although the reason for me to want to do it programatically was because I have a module conflict with the Disable Messages module and I couldn't find another module which can hide messages, nor did I think about formulating the problem in another way, i.e. "How to customize messages", which was what I was doing earlier with Disable Messages and Rules.
    – ashash
    Jul 15, 2014 at 13:14
2

Another way to accomplish this programmatically is to insert drupal_get_messages('status'); after you call your save function.

$wrapper->save();
drupal_get_messages('status');
drupal_set_message('New Custom Message... or no message at all'));

Note that ANY status messages will be caught by this, not just the one generated by the save function, so use it cautiously as it may have unattended affects.

1
  • This is wayyyyyy better than overriding the whole node submit hook. Thanks!
    – Collins
    Sep 25, 2018 at 11:54
2

Molot's answer concerning unsetting certain elements of $_SESSION['messages'] in an additional form submit handler was the best solution for me. One thing I struggled with was adding the custom handler appropriately -- not just to $form['#submit'][]. The following code works great for me:

function MY_MODULE_form_FORM_ID_form_alter(&$form, &$form_state, $form_id) {
  $form['actions']['submit']['#submit'][] = 'MY_MODULE_NODE_TYPE_remove_message';
}

function MY_MODULE_NODE_TYPE_remove_message(&$form, &$form_state) {
  foreach ($_SESSION['messages']['status'] as $key => $value) {
    if (strpos($value, 'STRING UNWANTED MESSAGE BEGINS WITH') === 0) {
        unset($_SESSION['messages']['status'][$key]);
    }
  }
}
0

I came across this question while researching how to hide the status update message in Drupal 9. The answers here have been deprecated as of 8.5+ in favor of \Drupal::messenger().

Into your custom module:

function HOOK_form_alter (array &$form, &$form_state, $form_id) {
if($form_id == 'NODE_ID') {
$form['actions']['submit']['#submit'][] = 'MY_SUBMIT_FUNCTION';
}
}

function MY_SUBMIT_FUNCTION($form, FormStateInterface $form_state) {
$messenger = \Drupal::messenger();
$messenger->deleteByType($messenger::TYPE_STATUS);
}
-1

You can do this in custom module:

/**
 * Implements hook_form_alter().
 */
function custommodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == "article_node_form") {
    //For article content type
    $form['actions']['submit']['#submit'][] = 'custommodule_custom_submit';
  }
}

function custommodule_custom_submit() {
  if (in_array("sellers", $user->roles)) {
    //sellers - For specific user role
    unset($_SESSION['messages']);
  }
}
2
  • 1
    Unsetting all messages is not a clean solution. It destroys messages created by other modules, too, and may mask errors user should be notified about immediately.
    – Mołot
    Jul 10, 2014 at 7:46
  • See this comment and feel free to use it to make your answer perfect :)
    – Mołot
    Jul 15, 2014 at 6:37
-2

http://xandeadx.ru/blog/drupal/729

Change

$_SESSION['messages']['status'][$old_message_key] = 'Текст нового сообщения';

to

unset($_SESSION['messages']['status'][$old_message_key]);

Your Answer

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

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