You are here

function log_filter_log in Log entity 7

Parse log placeholders.

1 string reference to 'log_filter_log'
log_filter_info in ./log.module
Implements hook_filter_info().

File

./log.module, line 434
Log - A general purpose record keeping system.

Code

function log_filter_log($text) {

  // Maintain a static array of log links that have been generated already.
  $links =& drupal_static(__FUNCTION__);

  // Look for pattern matches and iterate through them.
  $matches = array();
  preg_match_all('/\\[log:[0-9]+\\]/', $text, $matches);
  if (!empty($matches[0])) {
    foreach ($matches[0] as $match) {

      // Extract the log id.
      $id = str_replace('[log:', '', $match);
      $id = str_replace(']', '', $id);

      // If the log has not been loaded already...
      if (empty($links[$id])) {

        // Load the log's name from the database.
        $name = db_query('SELECT name FROM {log} WHERE id = :id', array(
          ':id' => $id,
        ))
          ->fetchField();

        // If the name is empty, skip it.
        if (empty($name)) {
          continue;
        }

        // Generate a link with the log's label.
        $link = l('[Log #' . $id . ': ' . check_plain($name) . ']', 'log/' . $id);

        // Store the link in the static cache.
        $links[$id] = $link;
      }

      // If a link is available, replace all occurrences of the original token
      // in the text.
      if (!empty($links[$id])) {
        $text = str_replace($match, $links[$id], $text);
      }
    }
  }

  // Return the formatted text.
  return $text;
}