You are here

function disable_messages_apply_filters in Disable Messages 8

Same name and namespace in other branches
  1. 6 disable_messages.module \disable_messages_apply_filters()
  2. 7 disable_messages.module \disable_messages_apply_filters()
  3. 2.x disable_messages.module \disable_messages_apply_filters()

Apply the filters to the messages.

Parameters

string $messages: Messages to apply filers.

Return value

mixed return filtered messages.

1 call to disable_messages_apply_filters()
disable_messages_preprocess_status_messages in ./disable_messages.module
Implements hook_preprocess_HOOK().

File

./disable_messages.module, line 80
The disable_messages module file.

Code

function disable_messages_apply_filters($messages) {
  $user = \Drupal::currentUser();

  // Cache the messages for debugging.
  $cache = $messages;

  // Check userid level filtering.
  $is_user_excluded = in_array((string) $user
    ->id(), explode(',', \Drupal::config('disable_messages.settings')
    ->get('disable_messages_exclude_users')), TRUE);

  // Store flags for debug.
  $cache['excluded']['uid'] = FALSE;
  if ($is_user_excluded) {
    $cache['excluded']['uid'] = TRUE;
  }

  // Administrator role except super admin is also,
  // not excluded as this might actually be a new requirement.
  // You can exclude them specifically via the exclude users option.
  $is_user_excluded = $is_user_excluded || !in_array('administrator', $user
    ->getRoles()) && \Drupal::currentUser()
    ->hasPermission('exclude from message filtering');
  if ($is_user_excluded && !$cache['excluded']['uid']) {
    $cache['excluded']['permission'] = TRUE;
  }

  // Check page level filtering.
  $filter_by_page = \Drupal::config('disable_messages.settings')
    ->get('disable_messages_filter_by_page');
  if ($filter_by_page > 0) {
    $filter_paths = explode("\n", \Drupal::config('disable_messages.settings')
      ->get('disable_messages_page_filter_paths'));
    $current_url = Url::fromRoute('<current>');
    $internal_path = disable_messages_remove_white_space($current_url
      ->getInternalPath());
    $path = \Drupal::service('path.alias_manager')
      ->getPathByAlias($internal_path);
    $page_match = disable_messages_path_match($filter_paths, $path);
    if ($path != $internal_path) {
      $page_match = $page_match || disable_messages_path_match($filter_paths, $internal_path);
    }

    // If $filter_by_page is 1 then listed paths are excluded from any filtering
    // and if 2 then filtering is applied only on listed paths.
    if ($filter_by_page == 1) {
      $is_page_excluded = $page_match;
    }
    else {
      $is_page_excluded = !$page_match;
    }
  }
  else {
    $is_page_excluded = FALSE;
  }

  // Store flags for debug.
  $cache['excluded']['page'] = $is_page_excluded;

  // If userid is excluded from filtering don't do any filtering.
  if (!$is_user_excluded && !$is_page_excluded) {
    $regexps = \Drupal::config('disable_messages.settings')
      ->get('disable_messages_ignore_regex');
    foreach ($messages as $type => $arr_messages) {

      // Check if the user has been denied access
      // to the specific type of messages.
      foreach ($arr_messages as $key => $message) {
        foreach ($regexps as $regex) {
          if (preg_match($regex, $message)) {

            // Keep track of the regular expression that matched the string.
            $cache[$type]['regex'][$key] = $regex;
            unset($messages[$type][$key]);
            break;
          }
        }
      }
      if (count($messages[$type]) == 0) {
        $cache[$type]['empty'] = TRUE;
        unset($messages[$type]);
      }
    }
  }
  Drupal::cache()
    ->set('cache_messages', $cache);
  return $messages;
}