function Analyzer::formatMessages

Same name in other branches
  1. 9 core/modules/views/src/Analyzer.php \Drupal\views\Analyzer::formatMessages()
  2. 8.9.x core/modules/views/src/Analyzer.php \Drupal\views\Analyzer::formatMessages()
  3. 11.x core/modules/views/src/Analyzer.php \Drupal\views\Analyzer::formatMessages()

Formats the analyze result into a message string.

This is based upon the format of \Drupal\Core\Messenger\MessengerInterface::addMessage() which uses separate boxes for "ok", "warning" and "error".

File

core/modules/views/src/Analyzer.php, line 60

Class

Analyzer
View analyzer plugin manager.

Namespace

Drupal\views

Code

public function formatMessages(array $messages) {
    if (empty($messages)) {
        $messages = [
            static::formatMessage(t('View analysis can find nothing to report.'), 'ok'),
        ];
    }
    $types = [
        'ok' => [],
        'warning' => [],
        'error' => [],
    ];
    foreach ($messages as $message) {
        if (empty($types[$message['type']])) {
            $types[$message['type']] = [];
        }
        $types[$message['type']][] = $message['message'];
    }
    $output = '';
    foreach ($types as $type => $messages) {
        $type .= ' messages';
        $message = '';
        if (count($messages) > 1) {
            $item_list = [
                '#theme' => 'item_list',
                '#items' => $messages,
            ];
            $message = \Drupal::service('renderer')->render($item_list);
        }
        elseif ($messages) {
            $message = array_shift($messages);
        }
        if ($message) {
            $output .= "<div class=\"{$type}\">{$message}</div>";
        }
    }
    return $output;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.