You are here

function csm_message_alter in Custom Submit Messages 6

Same name and namespace in other branches
  1. 7 csm.module \csm_message_alter()

Implements hook_message_alter().

File

./csm.module, line 129
The main module file for Custom Submit Messages.

Code

function csm_message_alter(&$messages) {

  // If $messages->messages['status'] doesn't exist then there are no status
  // messages and there's nothing to do
  if (!isset($messages->messages['status'])) {
    return;
  }

  // Search for submit messages and change any that are found
  // First create arrays containing messages that may need to be changed.
  // Because the messages might not be in English, we need to load the node
  // first. We can then build its create, update, and delete messages in t()
  // and check the output against the message we are looking to change.
  if (!isset($messages->messages['csm'][0])) {
    return;
  }
  $nid = $messages->messages['csm'][0];
  $node = variable_get('csm_node_temp_' . $nid, NULL);
  $args = array(
    '@type' => node_type_get_name($node),
    '%title' => $node->title,
  );
  $created = $messages
    ->contains(t('@type %title has been created.', $args), 'status');
  $updated = $messages
    ->contains(t('@type %title has been updated.', $args), 'status');
  $deleted = $messages
    ->contains(t('@type %title has been deleted.', $args), 'status');

  // Then parse through each array, double-checking for messages that need to be
  // changed and then changing them.
  // This code is no longer necessary because the matching technique (above) is
  // tighter, but it is retained so that $relevant_messages is created and can
  // be parsed later on. This could be rewritten so that $created, $updated, and
  // $deleted are parsed directly.
  $message_types_to_check = array(
    'created',
    'updated',
    'deleted',
  );
  foreach ($message_types_to_check as $delta => $type) {
    if (${$type} == FALSE) {
      continue;

      // There are no messages of type $$type so nothing to check
    }
    else {

      // Double check the messages
      foreach (${$type}['status'] as $delta_2 => $message_array) {

        // Check that the message does not start with 'The content type <em>'
        // If it doesn't then the message is relevant.
        if (!strpos($message_array['message'], 'The content type <em>')) {
          $relevant_messages[] = $message_array['index'];
        }
      }
    }
  }
  if (!isset($relevant_messages)) {

    // No relevant messages so nothing to change. There might be some stray data
    // left if the variable table, though, and also some non-visible messages,
    // so let's delete that.
    if (array_key_exists('csm', $messages->messages)) {
      $nid = $messages->messages['csm'][0];
      $nid_message = $messages
        ->match("/" . $nid . "/", 'csm');
      $messages
        ->remove($nid_message);
      $messages
        ->clean();
      variable_del('csm_node_temp_' . $nid);
    }
    return;
  }

  // Change the messages:
  foreach ($relevant_messages as $delta => $index) {
    $nid_message = $messages
      ->match("/" . $nid . "/", 'csm');
    $messages
      ->remove($nid_message);
    $messages
      ->clean();
    variable_del('csm_node_temp_' . $nid);

    // Check the active language then loads the msg based on that.
    global $language;

    // Change the status message to the custom status message.
    $message = variable_get('csm_' . $node->op . '_msg_' . $language->language . '_' . $node->type, $messages->messages['status'][$delta]);
    if ($message) {
      if ($message == '<none>') {

        // kill the message
        unset($messages->messages['status'][$delta]);
        $messages->remove_used = TRUE;
        $messages
          ->clean();
      }
      else {
        $message = $message == '<none>' ? '' : token_replace($message, 'node', $node);
        $messages->messages['status'][$delta] = t($message);
      }
    }
    unset($message);

    // Log a system message.
    watchdog('csm', '@type: node @msg_type message changed using Custom Submit Messages.', array(
      '@type' => $node->type,
      '@msg_type' => $node->op,
    ), WATCHDOG_NOTICE);
  }
  return;
}