Chris@0: moduleHandler = $module_handler; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Analyzes a review and return the results. Chris@0: * Chris@0: * @param \Drupal\views\ViewExecutable $view Chris@0: * The view to analyze. Chris@0: * Chris@0: * @return array Chris@0: * An array of analyze results organized into arrays keyed by 'ok', Chris@0: * 'warning' and 'error'. Chris@0: */ Chris@0: public function getMessages(ViewExecutable $view) { Chris@0: $view->initDisplay(); Chris@0: $messages = $this->moduleHandler->invokeAll('views_analyze', [$view]); Chris@0: Chris@0: return $messages; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Formats the analyze result into a message string. Chris@0: * Chris@17: * This is based upon the format of Chris@17: * \Drupal\Core\Messenger\MessengerInterface::addMessage() which uses separate Chris@0: * boxes for "ok", "warning" and "error". Chris@0: */ Chris@0: public function formatMessages(array $messages) { Chris@0: if (empty($messages)) { Chris@0: $messages = [static::formatMessage(t('View analysis can find nothing to report.'), 'ok')]; Chris@0: } Chris@0: Chris@0: $types = ['ok' => [], 'warning' => [], 'error' => []]; Chris@0: foreach ($messages as $message) { Chris@0: if (empty($types[$message['type']])) { Chris@0: $types[$message['type']] = []; Chris@0: } Chris@0: $types[$message['type']][] = $message['message']; Chris@0: } Chris@0: Chris@0: $output = ''; Chris@0: foreach ($types as $type => $messages) { Chris@0: $type .= ' messages'; Chris@0: $message = ''; Chris@0: if (count($messages) > 1) { Chris@0: $item_list = [ Chris@0: '#theme' => 'item_list', Chris@0: '#items' => $messages, Chris@0: ]; Chris@0: $message = \Drupal::service('renderer')->render($item_list); Chris@0: } Chris@0: elseif ($messages) { Chris@0: $message = array_shift($messages); Chris@0: } Chris@0: Chris@0: if ($message) { Chris@0: $output .= "
$message
"; Chris@0: } Chris@0: } Chris@0: Chris@0: return $output; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Formats an analysis message. Chris@0: * Chris@0: * This tool should be called by any module responding to the analyze hook Chris@0: * to properly format the message. It is usually used in the form: Chris@0: * @code Chris@0: * $ret[] = Analyzer::formatMessage(t('This is the message'), 'ok'); Chris@0: * @endcode Chris@0: * Chris@0: * The 'ok' status should be used to provide information about things Chris@0: * that are acceptable. In general analysis isn't interested in 'ok' Chris@0: * messages, but instead the 'warning', which is a category for items Chris@0: * that may be broken unless the user knows what he or she is doing, Chris@0: * and 'error' for items that are definitely broken are much more useful. Chris@0: * Chris@0: * @param string $message Chris@0: * @param string $type Chris@0: * The type of message. This should be "ok", "warning" or "error". Other Chris@0: * values can be used but how they are treated by the output routine Chris@0: * is undefined. Chris@0: * Chris@0: * @return array Chris@0: * A single formatted message, consisting of a key message and a key type. Chris@0: */ Chris@0: public static function formatMessage($message, $type = 'error') { Chris@0: return ['message' => $message, 'type' => $type]; Chris@0: } Chris@0: Chris@0: }