When new content is created, Drupal sets this system message:

"[content-type] [content title] has been created."

Is there any way this message can be unset or overridden with an Action?

Comments

timd.mackey’s picture

Component: Rules Core » Rules Engine

Subscribing. I'd also like to be able to do this, specifically so that I can override the default message when a user saves changes to their account.

Mark Nielsen’s picture

Version: 6.x-1.4 » 7.x-2.0
kopeboy’s picture

Version: 7.x-2.0 » 7.x-2.7
Component: Rules Engine » Rules Core
Issue summary: View changes

Yep please. Not only this message, but any message created for the same event we are working on with the Rule.

If that event has already a message and we set one within the Rule we will have both :(

Is there some configuration we are missing?

Chewits’s picture

Seeking how to solve this issue, too...

Chewits’s picture

I've found the related issue: https://www.drupal.org/node/409090
and an obsolete module https://www.drupal.org/project/rules_better_message
which could solve the problem by cleaning unnecessary messages and showing your own one.
But this module does not work for me in 7.x :(

Torenware’s picture

You'd need a custom action to do this. This actually isn't that hard to do.

In YOUR_MODULE.rules.inc:


function YOUR_MODULE_rules_action_info() {

  return array(
     'clear_messages' => array(
        'label' => t('Clear any pending Drupal messages.'),
        'parameter' => array(
          'type' => array('type' => 'text', 
                                 'label' => t('Message Type'),
                                 'description' => t('A key like "status", "warning" or "error"')
                                ),
        ),
        'group' => t('YOUR MODULE'),
        'base' => 'YOUR_MODULE_action_clear_messages',
    ),
  );
}

function YOUR_MODULE_action_clear_messages($type) {
  //clear messages of $type.
  drupal_get_messages($type, TRUE);
}

I haven't tested this (been a while since I worked on Drupal 7; I'm doing D8 now), but this should be close to what you want.

Chewits’s picture

Thank you, Rob. I solved my initial problem without Rules, though.
In fact, I needed a bit more - to show different status messages depending on whether a content was published or not. It's needed for forum topics only.

Here is my code:

/**
 * We customize 'Forum topic ... has been created.' message when
 * it goes to the approval queue. This is done in two steps:
 *
 * 1. Add new status messages implementing hook_node_insert().
 *    This is done for both Published and Unpublished nodes.
 *    For Published nodes we just duplicate the old message.
 *    For Unpublished nodes we inform that the message is waiting for approval.
 *
 * 2. Remove old status message implementing hook_form_FORM_ID_alter().
 *    We override 'submit' action where we remove old messages in case it
 *    matches the pattern '@type %title has been created.'.
 *    We only remove a single match, so even if we duplicate a message
 *    on the first step, one of them will still be displayed.
 *
 * We customize also the Drupal behavior when a Forum Topic was updated and
 * the new revision goes to the Approval Queue. Now it redirects a user to
 * the Forum Category and shows the similar status message to inform the user.
 * We use hook_node_update() for that.
 */
 
/**
 * Implements hook_node_insert()
 */
function MY_MODULE_node_insert($node) {
  /**
   * Add custom status messages when a node is inserted
   */
  if ($node->type == 'forum') {
    if ($node->status == NODE_NOT_PUBLISHED) {
      drupal_set_message(t('Your forum topic has been queued for review by site administrators and will be published after approval.'));
    }
    elseif ($node->status == NODE_PUBLISHED) {
      $message = t('@type %title has been created.', array('@type' => node_type_get_name($node), '%title' => $node->title));
      drupal_set_message($message, 'status', TRUE);
    }
  }
}
 
/**
 * Implements hook_node_update()
 */
function MY_MODULE_node_update($node) {
  /**
   * If a Forum Topic was updated and the new revision goes to the Approval Queue
   * we redirect a user to the Forum Category and show a message to keep him informed.
   */
  if ($node->type == 'forum') {
    if ($node->status == NODE_NOT_PUBLISHED) {
      drupal_set_message(t('Your forum topic has been queued for review by site administrators and will be published after approval.'));
      drupal_goto('forum/' . $node->forum_tid);
    }
  }
}
 
/**
 * Implements hook_form_FORM_ID_alter().
 * @see MY_MODULE_node_insert()
 */
function MY_MODULE_form_forum_node_form_alter(&$form, &$form_state, $form_id) {
  /**
   * Override 'submit' action for 'forum_node_form' form
   */
  $form['actions']['submit']['#submit'][] = '_MY_MODULE_form_forum_node_form_submit';
}
 
/**
 * @see MY_MODULE_form_forum_node_form_alter()
 */
function _MY_MODULE_form_forum_node_form_submit($form, &$form_state) {
  /**
   * Remove the old status message 'Forum topic ... has been created.'
   */
  if (!empty($_SESSION['messages']['status'])) {
    $node = $form_state['node'];
    $old_message = t('@type %title has been created.', array('@type' => node_type_get_name($node), '%title' => $node->title));
    $old_message_key = array_search($old_message, $_SESSION['messages']['status']);
    
    if ($old_message_key !== FALSE) {
      unset($_SESSION['messages']['status'][$old_message_key]);
      
      // Reset array indexes. Otherwise it doesn’t work with some theme templates.
      $_SESSION['messages']['status'] = array_values($_SESSION['messages']['status']);
      
      // Remove the empty status message wrapper if no other messages have been set.
      if (empty($_SESSION['messages']['status'])) {
        unset($_SESSION['messages']['status']);
      }
    }
  }
}

Ref (rus): www.ishmuradov.com/page/drupal-7-izmenjaem-statusnye-soobshhenija

Hope this helps someone,
Alexander

amerie’s picture

Here is code for a Rules action to clear all or some messages from the site (specified by type and/or regular expression applied to the message text):

function mymodule_rules_action_info() {

  $actions['mymodule_rules_action_clear_messages'] = array(
    'label' => t('Clear system messages'),
    'parameter' => array(
      'type' => array(
        'type' => 'token',
        'label' => t('Clear messages of type'),
        'options list' => 'mymodule_message_types',
        'default value' => 'all',
        'optional' => FALSE,
      ),
      'pattern' => array(
        'type' => 'text',
        'label' => t('Regular Expression'),
        'description' => t('If specified, messages of the type specified above will only be cleared if their text matches this pattern.'),
        'optional' => TRUE,
      ),
    ),
    'group' => t('System'),
  );

  return $actions;
}

function mymodule_rules_action_clear_messages($type, $pattern) {
  if ($type == "all") {
    $message_type = NULL;
  }
  else {
    $message_type = $type;
  }

  // Clear all messages of specified type.
  $all_messages = drupal_get_messages($message_type);

  // If no RE provided, done.
  if (empty($all_messages) | !$pattern | $pattern == "") {
    return;
  }

  // Reset messages that do not match RE.
  foreach ($all_messages as $category => $messages) {
    foreach ($messages as $message) {
      if (preg_match($pattern, $message) != 1) {
        drupal_set_message($message, $category);
      }
    }
  }
}

function mymodule_message_types() {
  return array(
    'all' => t('All'),
    'status' => t('Status'),
    'warning' => t('Warning'),
    'error' => t('Error'),
  );
}
tr’s picture

This seems to have been answered several times already.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

bmango’s picture

Many thanks for the code in #8. It worked perfectly!