Analyzer.php 3.6 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 drupal_set_message which uses separate
  47. * boxes for "ok", "warning" and "error".
  48. */
  49. public function formatMessages(array $messages) {
  50. if (empty($messages)) {
  51. $messages = [static::formatMessage(t('View analysis can find nothing to report.'), 'ok')];
  52. }
  53. $types = ['ok' => [], 'warning' => [], 'error' => []];
  54. foreach ($messages as $message) {
  55. if (empty($types[$message['type']])) {
  56. $types[$message['type']] = [];
  57. }
  58. $types[$message['type']][] = $message['message'];
  59. }
  60. $output = '';
  61. foreach ($types as $type => $messages) {
  62. $type .= ' messages';
  63. $message = '';
  64. if (count($messages) > 1) {
  65. $item_list = [
  66. '#theme' => 'item_list',
  67. '#items' => $messages,
  68. ];
  69. $message = \Drupal::service('renderer')->render($item_list);
  70. }
  71. elseif ($messages) {
  72. $message = array_shift($messages);
  73. }
  74. if ($message) {
  75. $output .= "<div class=\"$type\">$message</div>";
  76. }
  77. }
  78. return $output;
  79. }
  80. /**
  81. * Formats an analysis message.
  82. *
  83. * This tool should be called by any module responding to the analyze hook
  84. * to properly format the message. It is usually used in the form:
  85. * @code
  86. * $ret[] = Analyzer::formatMessage(t('This is the message'), 'ok');
  87. * @endcode
  88. *
  89. * The 'ok' status should be used to provide information about things
  90. * that are acceptable. In general analysis isn't interested in 'ok'
  91. * messages, but instead the 'warning', which is a category for items
  92. * that may be broken unless the user knows what he or she is doing,
  93. * and 'error' for items that are definitely broken are much more useful.
  94. *
  95. * @param string $message
  96. * @param string $type
  97. * The type of message. This should be "ok", "warning" or "error". Other
  98. * values can be used but how they are treated by the output routine
  99. * is undefined.
  100. *
  101. * @return array
  102. * A single formatted message, consisting of a key message and a key type.
  103. */
  104. public static function formatMessage($message, $type = 'error') {
  105. return ['message' => $message, 'type' => $type];
  106. }
  107. }