Mercurial > hg > isophonics-drupal-site
comparison core/modules/views/src/Analyzer.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\views; | |
4 | |
5 use Drupal\Core\Extension\ModuleHandlerInterface; | |
6 | |
7 /** | |
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. | |
14 */ | |
15 class Analyzer { | |
16 | |
17 /** | |
18 * A module handler that invokes the 'views_analyze' hook. | |
19 * | |
20 * @var \Drupal\Core\Extension\ModuleHandlerInterface | |
21 */ | |
22 protected $moduleHandler; | |
23 | |
24 /** | |
25 * Constructs an Analyzer object. | |
26 * | |
27 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler | |
28 * The module handler that invokes the 'views_analyze' hook. | |
29 */ | |
30 public function __construct(ModuleHandlerInterface $module_handler) { | |
31 $this->moduleHandler = $module_handler; | |
32 } | |
33 | |
34 | |
35 /** | |
36 * Analyzes a review and return the results. | |
37 * | |
38 * @param \Drupal\views\ViewExecutable $view | |
39 * The view to analyze. | |
40 * | |
41 * @return array | |
42 * An array of analyze results organized into arrays keyed by 'ok', | |
43 * 'warning' and 'error'. | |
44 */ | |
45 public function getMessages(ViewExecutable $view) { | |
46 $view->initDisplay(); | |
47 $messages = $this->moduleHandler->invokeAll('views_analyze', [$view]); | |
48 | |
49 return $messages; | |
50 } | |
51 | |
52 /** | |
53 * Formats the analyze result into a message string. | |
54 * | |
55 * This is based upon the format of drupal_set_message which uses separate | |
56 * boxes for "ok", "warning" and "error". | |
57 */ | |
58 public function formatMessages(array $messages) { | |
59 if (empty($messages)) { | |
60 $messages = [static::formatMessage(t('View analysis can find nothing to report.'), 'ok')]; | |
61 } | |
62 | |
63 $types = ['ok' => [], 'warning' => [], 'error' => []]; | |
64 foreach ($messages as $message) { | |
65 if (empty($types[$message['type']])) { | |
66 $types[$message['type']] = []; | |
67 } | |
68 $types[$message['type']][] = $message['message']; | |
69 } | |
70 | |
71 $output = ''; | |
72 foreach ($types as $type => $messages) { | |
73 $type .= ' messages'; | |
74 $message = ''; | |
75 if (count($messages) > 1) { | |
76 $item_list = [ | |
77 '#theme' => 'item_list', | |
78 '#items' => $messages, | |
79 ]; | |
80 $message = \Drupal::service('renderer')->render($item_list); | |
81 } | |
82 elseif ($messages) { | |
83 $message = array_shift($messages); | |
84 } | |
85 | |
86 if ($message) { | |
87 $output .= "<div class=\"$type\">$message</div>"; | |
88 } | |
89 } | |
90 | |
91 return $output; | |
92 } | |
93 | |
94 /** | |
95 * Formats an analysis message. | |
96 * | |
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: | |
99 * @code | |
100 * $ret[] = Analyzer::formatMessage(t('This is the message'), 'ok'); | |
101 * @endcode | |
102 * | |
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. | |
108 * | |
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 | |
113 * is undefined. | |
114 * | |
115 * @return array | |
116 * A single formatted message, consisting of a key message and a key type. | |
117 */ | |
118 public static function formatMessage($message, $type = 'error') { | |
119 return ['message' => $message, 'type' => $type]; | |
120 } | |
121 | |
122 } |