3 namespace Drupal\views;
5 use Drupal\Core\Extension\ModuleHandlerInterface;
8 * This tool is a small plugin manager to perform analysis on a view and
9 * report results to the user. This tool is meant to let modules that
10 * provide data to Views also help users properly use that data by
11 * detecting invalid configurations. Views itself comes with only a
12 * small amount of analysis tools, but more could easily be added either
13 * by modules or as patches to Views itself.
18 * A module handler that invokes the 'views_analyze' hook.
20 * @var \Drupal\Core\Extension\ModuleHandlerInterface
22 protected $moduleHandler;
25 * Constructs an Analyzer object.
27 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
28 * The module handler that invokes the 'views_analyze' hook.
30 public function __construct(ModuleHandlerInterface $module_handler) {
31 $this->moduleHandler = $module_handler;
36 * Analyzes a review and return the results.
38 * @param \Drupal\views\ViewExecutable $view
39 * The view to analyze.
42 * An array of analyze results organized into arrays keyed by 'ok',
43 * 'warning' and 'error'.
45 public function getMessages(ViewExecutable $view) {
47 $messages = $this->moduleHandler->invokeAll('views_analyze', [$view]);
53 * Formats the analyze result into a message string.
55 * This is based upon the format of drupal_set_message which uses separate
56 * boxes for "ok", "warning" and "error".
58 public function formatMessages(array $messages) {
59 if (empty($messages)) {
60 $messages = [static::formatMessage(t('View analysis can find nothing to report.'), 'ok')];
63 $types = ['ok' => [], 'warning' => [], 'error' => []];
64 foreach ($messages as $message) {
65 if (empty($types[$message['type']])) {
66 $types[$message['type']] = [];
68 $types[$message['type']][] = $message['message'];
72 foreach ($types as $type => $messages) {
75 if (count($messages) > 1) {
77 '#theme' => 'item_list',
78 '#items' => $messages,
80 $message = drupal_render($item_list);
83 $message = array_shift($messages);
87 $output .= "<div class=\"$type\">$message</div>";
95 * Formats an analysis message.
97 * This tool should be called by any module responding to the analyze hook
98 * to properly format the message. It is usually used in the form:
100 * $ret[] = Analyzer::formatMessage(t('This is the message'), 'ok');
103 * The 'ok' status should be used to provide information about things
104 * that are acceptable. In general analysis isn't interested in 'ok'
105 * messages, but instead the 'warning', which is a category for items
106 * that may be broken unless the user knows what he or she is doing,
107 * and 'error' for items that are definitely broken are much more useful.
109 * @param string $message
110 * @param string $type
111 * The type of message. This should be "ok", "warning" or "error". Other
112 * values can be used but how they are treated by the output routine
116 * A single formatted message, consisting of a key message and a key type.
118 public static function formatMessage($message, $type = 'error') {
119 return ['message' => $message, 'type' => $type];