Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\views;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Extension\ModuleHandlerInterface;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * This tool is a small plugin manager to perform analysis on a view and
|
Chris@0
|
9 * report results to the user. This tool is meant to let modules that
|
Chris@0
|
10 * provide data to Views also help users properly use that data by
|
Chris@0
|
11 * detecting invalid configurations. Views itself comes with only a
|
Chris@0
|
12 * small amount of analysis tools, but more could easily be added either
|
Chris@0
|
13 * by modules or as patches to Views itself.
|
Chris@0
|
14 */
|
Chris@0
|
15 class Analyzer {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * A module handler that invokes the 'views_analyze' hook.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var \Drupal\Core\Extension\ModuleHandlerInterface
|
Chris@0
|
21 */
|
Chris@0
|
22 protected $moduleHandler;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Constructs an Analyzer object.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
Chris@0
|
28 * The module handler that invokes the 'views_analyze' hook.
|
Chris@0
|
29 */
|
Chris@0
|
30 public function __construct(ModuleHandlerInterface $module_handler) {
|
Chris@0
|
31 $this->moduleHandler = $module_handler;
|
Chris@0
|
32 }
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Analyzes a review and return the results.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @param \Drupal\views\ViewExecutable $view
|
Chris@0
|
38 * The view to analyze.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @return array
|
Chris@0
|
41 * An array of analyze results organized into arrays keyed by 'ok',
|
Chris@0
|
42 * 'warning' and 'error'.
|
Chris@0
|
43 */
|
Chris@0
|
44 public function getMessages(ViewExecutable $view) {
|
Chris@0
|
45 $view->initDisplay();
|
Chris@0
|
46 $messages = $this->moduleHandler->invokeAll('views_analyze', [$view]);
|
Chris@0
|
47
|
Chris@0
|
48 return $messages;
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * Formats the analyze result into a message string.
|
Chris@0
|
53 *
|
Chris@17
|
54 * This is based upon the format of
|
Chris@17
|
55 * \Drupal\Core\Messenger\MessengerInterface::addMessage() which uses separate
|
Chris@0
|
56 * boxes for "ok", "warning" and "error".
|
Chris@0
|
57 */
|
Chris@0
|
58 public function formatMessages(array $messages) {
|
Chris@0
|
59 if (empty($messages)) {
|
Chris@0
|
60 $messages = [static::formatMessage(t('View analysis can find nothing to report.'), 'ok')];
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 $types = ['ok' => [], 'warning' => [], 'error' => []];
|
Chris@0
|
64 foreach ($messages as $message) {
|
Chris@0
|
65 if (empty($types[$message['type']])) {
|
Chris@0
|
66 $types[$message['type']] = [];
|
Chris@0
|
67 }
|
Chris@0
|
68 $types[$message['type']][] = $message['message'];
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 $output = '';
|
Chris@0
|
72 foreach ($types as $type => $messages) {
|
Chris@0
|
73 $type .= ' messages';
|
Chris@0
|
74 $message = '';
|
Chris@0
|
75 if (count($messages) > 1) {
|
Chris@0
|
76 $item_list = [
|
Chris@0
|
77 '#theme' => 'item_list',
|
Chris@0
|
78 '#items' => $messages,
|
Chris@0
|
79 ];
|
Chris@0
|
80 $message = \Drupal::service('renderer')->render($item_list);
|
Chris@0
|
81 }
|
Chris@0
|
82 elseif ($messages) {
|
Chris@0
|
83 $message = array_shift($messages);
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 if ($message) {
|
Chris@0
|
87 $output .= "<div class=\"$type\">$message</div>";
|
Chris@0
|
88 }
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 return $output;
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * Formats an analysis message.
|
Chris@0
|
96 *
|
Chris@0
|
97 * This tool should be called by any module responding to the analyze hook
|
Chris@0
|
98 * to properly format the message. It is usually used in the form:
|
Chris@0
|
99 * @code
|
Chris@0
|
100 * $ret[] = Analyzer::formatMessage(t('This is the message'), 'ok');
|
Chris@0
|
101 * @endcode
|
Chris@0
|
102 *
|
Chris@0
|
103 * The 'ok' status should be used to provide information about things
|
Chris@0
|
104 * that are acceptable. In general analysis isn't interested in 'ok'
|
Chris@0
|
105 * messages, but instead the 'warning', which is a category for items
|
Chris@0
|
106 * that may be broken unless the user knows what he or she is doing,
|
Chris@0
|
107 * and 'error' for items that are definitely broken are much more useful.
|
Chris@0
|
108 *
|
Chris@0
|
109 * @param string $message
|
Chris@0
|
110 * @param string $type
|
Chris@0
|
111 * The type of message. This should be "ok", "warning" or "error". Other
|
Chris@0
|
112 * values can be used but how they are treated by the output routine
|
Chris@0
|
113 * is undefined.
|
Chris@0
|
114 *
|
Chris@0
|
115 * @return array
|
Chris@0
|
116 * A single formatted message, consisting of a key message and a key type.
|
Chris@0
|
117 */
|
Chris@0
|
118 public static function formatMessage($message, $type = 'error') {
|
Chris@0
|
119 return ['message' => $message, 'type' => $type];
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@0
|
122 }
|