Analyzer.php 3.7 KB

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