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. $type .= ' messages';
  45. $message = '';
  46. if (count($messages) > 1) {
  47. $message = theme('item_list', array('items' => $messages));
  48. }
  49. elseif ($messages) {
  50. $message = array_shift($messages);
  51. }
  52. if ($message) {
  53. $output .= "<div class=\"$type\">$message</div>";
  54. }
  55. }
  56. return $output;
  57. }
  58. /**
  59. * Format an analysis message.
  60. *
  61. * This tool should be called by any module responding to the analyze hook
  62. * to properly format the message. It is usually used in the form:
  63. * @code
  64. * $ret[] = views_ui_analysis(t('This is the message'), 'ok');
  65. * @endcode
  66. *
  67. * The 'ok' status should be used to provide information about things
  68. * that are acceptable. In general analysis isn't interested in 'ok'
  69. * messages, but instead the 'warning', which is a category for items
  70. * that may be broken unless the user knows what he or she is doing,
  71. * and 'error' for items that are definitely broken are much more useful.
  72. *
  73. * @param $messages
  74. * The message to report.
  75. * @param $type
  76. * The type of message. This should be "ok", "warning" or "error". Other
  77. * values can be used but how they are treated by the output routine
  78. * is undefined.
  79. */
  80. function views_ui_analysis($message, $type = 'error') {
  81. return array('message' => $message, 'type' => $type);
  82. }
  83. /**
  84. * Implements hook_views_analyze().
  85. *
  86. * This is the basic views analysis that checks for very minimal problems.
  87. * There are other analysis tools in core specific sections, such as
  88. * node.views.inc as well.
  89. */
  90. function views_ui_views_analyze($view) {
  91. $ret = array();
  92. // Check for something other than the default display:
  93. if (count($view->display) < 2) {
  94. $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');
  95. }
  96. // You can give a page display the same path as an alias existing in the
  97. // system, so the alias will not work anymore. Report this to the user,
  98. // because he probably wanted something else.
  99. foreach ($view->display as $id => $display) {
  100. if (empty($display->handler)) {
  101. continue;
  102. }
  103. if ($display->handler->has_path() && $path = $display->handler->get_option('path')) {
  104. $normal_path = drupal_get_normal_path($path);
  105. if ($path != $normal_path) {
  106. $ret[] = views_ui_analysis(t('You have configured display %display with a path which is an path alias as well. This might lead to unwanted effects so better use an internal path.', array('%display' => $display->display_title)), 'warning');
  107. }
  108. }
  109. }
  110. return $ret;
  111. }