You are here

function disable_messages_apply_filters in Disable Messages 2.x

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

Apply the filters to the messages.

Parameters

array $messages: Messages to apply filers.

Return value

array 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 129
The disable_messages module file.

Code

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

  // Retrieve the debugging cache.
  $cache = \Drupal::cache()
    ->get('disable_messages:cache_messages');
  $cache = $cache->data;

  // @todo Store the path to debug. The debug div is delayed.
  $cache['url'] = Url::fromRoute('<current>')
    ->toString();

  // 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;
  }

  // If exclude from filtering permission is not disabled.
  if (!\Drupal::config('disable_messages.settings')
    ->get('disable_messages_ignore_exclude')) {

    // If the user is not already excluded through an id filter and the user has
    // the permission exclude from message filtering, exclude from filtering.
    if (!$is_user_excluded && $user
      ->hasPermission('exclude from message filtering')) {
      $is_user_excluded = TRUE;
      $cache['excluded']['excluded'] = 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 = \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 = \Drupal::service('path.matcher')
      ->matchPath($path, $filter_paths);
    if ($path != $internal_path) {
      $page_match = $page_match || \Drupal::service('path.matcher')
        ->matchPath($internal_path, $filter_paths);
    }

    // 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) {
    foreach ($messages as $type => $arr_messages) {
      if (!$user
        ->hasPermission('view error messages')) {
        \Drupal::logger('disable_messages')
          ->notice('No Has permission');
      }

      // If permission based filtering is enabled, then filter based on type
      // and permission of the user to view the type.
      if (\Drupal::config('disable_messages.settings')
        ->get('disable_messages_enable_permissions')) {

        // If the user does not have permission to view messages of type 'type'
        // unset all messages of the type. Do this only for the standard types
        // status, warning and error.
        if (in_array($type, [
          'status',
          'warning',
          'error',
        ]) && !$user
          ->hasPermission('view ' . $type . ' messages')) {
          unset($messages[$type]);
          $cache['excluded']['permission'][$type] = TRUE;
          $arr_messages = [];
          continue;
        }
      }
      foreach ($arr_messages as $key => $message) {
        if (is_object($message) && method_exists($message, "__toString")) {
          $message = $message
            ->__toString();
        }

        // If this is an array type, eg: from webform, squash it
        // and run the test.
        if (!is_string($message)) {
          $message = print_r($message, TRUE);
        }

        // If the message is not a string then handle it after rendering.
        if (!is_string($message)) {

          /*
                    // @todo Potentially could make this or a similar approach work
                    // @codingStandardsIgnoreStart
                    // foreach ($message as $item_key => $message_item) {
                    //   \Drupal::logger('disable_messages_items')->notice(print_r($message_item, TRUE));
                    //   $messages[$type][$key][$item_key]['#post_render'][] = function ($markup, array $element) {
                    //     $cached = \Drupal::cache()->get('disable_messages:cache_messages');
                    //     $cached = $cached->data;
                    //     \Drupal::logger('disable_messages_callback')->notice(print_r($cached, TRUE));
                    //     $regexps = \Drupal::config('disable_messages.settings')->get('disable_messages_ignore_regex');
                    //     foreach ($regexps as $regex) {
                    //       if (preg_match($regex, $markup)) {
                    //         // Keep track of the regular expression that
                    //         // matched the string.
                    //         \Drupal::logger('disable_messages_hide')->notice(print_r($markup, TRUE));
                    //         $cached['excluded']['render']['regex'] = $regex;
                    //         $markup = NULL;
                    //         break;
                    //       }
                    //     }
                    //     \Drupal::cache()->set('disable_messages:cache_messages', $cached);
                    //     return $markup;
                    //   };
                    // }
                    // @codingStandardsIgnoreEnd
          */
        }
        else {
          $regexps = \Drupal::config('disable_messages.settings')
            ->get('disable_messages_ignore_regex');
          foreach ($regexps as $regex) {

            // Squash multiline strings to one line to allow for
            // simple regex matching.
            $message = preg_replace("/[\n\r]/", " ", $message);
            if (preg_match($regex, $message)) {

              // Keep track of the regular expression that matched the string.
              $cache['excluded']['regex'][$type][$key] = $regex;
              unset($messages[$type][$key]);
              break;
            }
          }
        }
      }

      // If all the messages of a type has been filtered out,
      // then clear the parent.
      if (isset($messages[$type]) && count($messages[$type]) == 0) {
        $cache[$type]['empty'] = TRUE;
        unset($messages[$type]);
      }
    }
  }
  \Drupal::cache()
    ->set('disable_messages:cache_messages', $cache);
  return $messages;
}