analyze.inc

  1. 3.x includes/analyze.inc
  2. 2.x includes/analyze.inc

Contains the view analyze tool code.

This tool is a small plugin manager to perform analysis on a view and report results to the user. This tool is meant to let modules that provide data to Views also help users properly use that data by detecting invalid configurations. Views itself comes with only a small amount of analysis tools, but more could easily be added either by modules or as patches to Views itself.

File

includes/analyze.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains the view analyze tool code.
  5. *
  6. * This tool is a small plugin manager to perform analysis on a view and
  7. * report results to the user. This tool is meant to let modules that
  8. * provide data to Views also help users properly use that data by
  9. * detecting invalid configurations. Views itself comes with only a
  10. * small amount of analysis tools, but more could easily be added either
  11. * by modules or as patches to Views itself.
  12. */
  13. /**
  14. * Analyze a review and return the results.
  15. *
  16. * @return
  17. * An array of analyze results organized into arrays keyed by 'ok',
  18. * 'warning' and 'error'.
  19. */
  20. function views_analyze_view(&$view) {
  21. $view->init_display();
  22. $messages = module_invoke_all('views_analyze', $view);
  23. return $messages;
  24. }
  25. /**
  26. * Format the analyze result into a message string.
  27. *
  28. * This is based upon the format of drupal_set_message which uses separate
  29. * boxes for "ok", "warning" and "error".
  30. */
  31. function views_analyze_format_result($view, $messages) {
  32. if (empty($messages)) {
  33. $messages = array(views_ui_analysis(t('View analysis can find nothing to report.'), 'ok'));
  34. }
  35. $types = array('ok' => array(), 'warning' => array(), 'error' => array());
  36. foreach ($messages as $message) {
  37. if (empty($types[$message['type']])) {
  38. $types[$message['type']] = array();
  39. }
  40. $types[$message['type']][] = $message['message'];
  41. }
  42. $output = '';
  43. foreach ($types as $type => $messages) {
  44. $message = '';
  45. if (count($messages) > 1) {
  46. $message = theme('item_list', $messages);
  47. }
  48. else if ($messages) {
  49. $message = array_shift($messages);
  50. }
  51. if ($message) {
  52. $output .= "<div class=\"$type\">$message</div>";
  53. }
  54. }
  55. return $output;
  56. }
  57. /**
  58. * Format an analysis message.
  59. *
  60. * This tool should be called by any module responding to the analyze hook
  61. * to properly format the message. It is usually used in the form:
  62. * @code
  63. * $ret[] = views_ui_analysis(t('This is the message'), 'ok');
  64. * @endcode
  65. *
  66. * The 'ok' status should be used to provide information about things
  67. * that are acceptable. In general analysis isn't interested in 'ok'
  68. * messages, but instead the 'warning', which is a category for items
  69. * that may be broken unless the user knows what he or she is doing,
  70. * and 'error' for items that are definitely broken are much more useful.
  71. *
  72. * @param $messages
  73. * The message to report.
  74. * @param $type
  75. * The type of message. This should be "ok", "warning" or "error". Other
  76. * values can be used but how they are treated by the output routine
  77. * is undefined.
  78. */
  79. function views_ui_analysis($message, $type = 'error') {
  80. return array('message' => $message, 'type' => $type);
  81. }
  82. /**
  83. * Implementation of hook_views_analyze().
  84. *
  85. * This is the basic views analysis that checks for very minimal problems.
  86. * There are other analysis tools in core specific sections, such as
  87. * node.views.inc as well.
  88. */
  89. function views_ui_views_analyze($view) {
  90. $ret = array();
  91. // Check for something other than the default display:
  92. if (count($view->display) < 2) {
  93. $ret[] = views_ui_analysis(t('This view has only a default display and therefore will not be placed anywhere on your site; perhaps you want to add a page or a block display.'), 'warning');
  94. }
  95. return $ret;
  96. }