Commit 23522a1f authored by dawehner's avatar dawehner Committed by tim.plunkett

Issue #1642164 by dawehner, amateescu: Convert analyzer into an PSR-0 object.

parent f63d35df
......@@ -8,6 +8,8 @@
* views-revert - Drush command to revert views overridden in the system.
*/
use Drupal\views\Analyzer;
/**
* Implement hook_drush_help().
*/
......@@ -423,9 +425,11 @@ function drush_views_analyze() {
$messages_count = 0;
$total = 0;
$analyzer = new Analyzer();
foreach (views_get_all_views() as $view_name => $view) {
$total++;
if ($messages = views_analyze_view($view)) {
$analyzer->setView($view);
if ($messages = $analyzer->getMessages($view)) {
drush_print($view_name);
foreach ($messages as $message) {
$messages_count++;
......
......@@ -7,6 +7,7 @@
use Drupal\Core\Database\Database;
use Drupal\views\View;
use Drupal\views\Analyzer;
/**
* Create an array of Views admin CSS for adding or attaching.
......@@ -3008,13 +3009,13 @@ function views_ui_analyze_view_form($form, &$form_state) {
$form['#title'] = t('View analysis');
$form['#section'] = 'analyze';
views_include('analyze');
$messages = views_analyze_view($view);
$analyzer = new Analyzer($view);
$messages = $analyzer->getMessages();
$form['analysis'] = array(
'#prefix' => '<div class="form-item">',
'#suffix' => '</div>',
'#markup' => views_analyze_format_result($view, $messages),
'#markup' => $analyzer->formatMessages($messages),
);
// Inform the standard button function that we want an OK button.
......
......@@ -3,92 +3,9 @@
/**
* @file
* 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.
*/
/**
* Analyze a review and return the results.
*
* @return
* An array of analyze results organized into arrays keyed by 'ok',
* 'warning' and 'error'.
*/
function views_analyze_view(&$view) {
$view->init_display();
$messages = module_invoke_all('views_analyze', $view);
return $messages;
}
/**
* Format the analyze result into a message string.
*
* This is based upon the format of drupal_set_message which uses separate
* boxes for "ok", "warning" and "error".
*/
function views_analyze_format_result($view, $messages) {
if (empty($messages)) {
$messages = array(views_ui_analysis(t('View analysis can find nothing to report.'), 'ok'));
}
$types = array('ok' => array(), 'warning' => array(), 'error' => array());
foreach ($messages as $message) {
if (empty($types[$message['type']])) {
$types[$message['type']] = array();
}
$types[$message['type']][] = $message['message'];
}
$output = '';
foreach ($types as $type => $messages) {
$type .= ' messages';
$message = '';
if (count($messages) > 1) {
$message = theme('item_list', array('items' => $messages));
}
elseif ($messages) {
$message = array_shift($messages);
}
if ($message) {
$output .= "<div class=\"$type\">$message</div>";
}
}
return $output;
}
/**
* Format an analysis message.
*
* This tool should be called by any module responding to the analyze hook
* to properly format the message. It is usually used in the form:
* @code
* $ret[] = views_ui_analysis(t('This is the message'), 'ok');
* @endcode
*
* The 'ok' status should be used to provide information about things
* that are acceptable. In general analysis isn't interested in 'ok'
* messages, but instead the 'warning', which is a category for items
* that may be broken unless the user knows what he or she is doing,
* and 'error' for items that are definitely broken are much more useful.
*
* @param $messages
* The message to report.
* @param $type
* The type of message. This should be "ok", "warning" or "error". Other
* values can be used but how they are treated by the output routine
* is undefined.
*/
function views_ui_analysis($message, $type = 'error') {
return array('message' => $message, 'type' => $type);
}
use Drupal\views\Analyzer;
/**
* Implements hook_views_analyze().
......@@ -101,7 +18,7 @@ function views_ui_views_analyze($view) {
$ret = array();
// Check for something other than the default display:
if (count($view->display) < 2) {
$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');
$ret[] = Analyzer::formatMessage(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');
}
// You can give a page display the same path as an alias existing in the
// system, so the alias will not work anymore. Report this to the user,
......@@ -113,7 +30,7 @@ function views_ui_views_analyze($view) {
if ($display->handler->has_path() && $path = $display->handler->get_option('path')) {
$normal_path = drupal_get_normal_path($path);
if ($path != $normal_path) {
$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');
$ret[] = Analyzer::formatMessage(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');
}
}
}
......
<?php
/**
* @file
* Definition of Drupal\views\Analyzer.
*/
namespace Drupal\views;
use Drupal\views\View;
/**
* 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.
*/
class Analyzer {
/**
* The view to analyze.
*
* @var Drupal\views\View.
*/
protected $view;
/**
* Constructs the analyzer object.
*
* @param Drupal\views\View $view
* (optional) The view to analyze.
*/
function __construct(View $view = NULL) {
if (isset($view)) {
$this->view = $view;
}
}
/**
* Gets the view which is analyzed by this analyzer.
*
* @return Drupal\views\View.
* The view to analyze.
*/
public function getView() {
return $this->view;
}
/**
* Sets the view which is analyzed by this analyzer.
*
* @param Drupal\views\View
* The view to analyze.
*/
public function setView(View $view = NULL) {
$this->view = $view;
}
/**
* Analyzes a review and return the results.
*
* @return array
* An array of analyze results organized into arrays keyed by 'ok',
* 'warning' and 'error'.
*/
public function getMessages() {
$this->view->init_display();
$messages = module_invoke_all('views_analyze', $this->view);
return $messages;
}
/**
* Formats the analyze result into a message string.
*
* This is based upon the format of drupal_set_message which uses separate
* boxes for "ok", "warning" and "error".
*/
public function formatMessages(array $messages) {
if (empty($messages)) {
$messages = array($this->formatMessage(t('View analysis can find nothing to report.'), 'ok'));
}
$types = array('ok' => array(), 'warning' => array(), 'error' => array());
foreach ($messages as $message) {
if (empty($types[$message['type']])) {
$types[$message['type']] = array();
}
$types[$message['type']][] = $message['message'];
}
$output = '';
foreach ($types as $type => $messages) {
$type .= ' messages';
$message = '';
if (count($messages) > 1) {
$message = theme('item_list', array('items' => $messages));
}
elseif ($messages) {
$message = array_shift($messages);
}
if ($message) {
$output .= "<div class=\"$type\">$message</div>";
}
}
return $output;
}
/**
* Formats an analysis message.
*
* This tool should be called by any module responding to the analyze hook
* to properly format the message. It is usually used in the form:
* @code
* $ret[] = Analyzer::formatMessage(t('This is the message'), 'ok');
* @endcode
*
* The 'ok' status should be used to provide information about things
* that are acceptable. In general analysis isn't interested in 'ok'
* messages, but instead the 'warning', which is a category for items
* that may be broken unless the user knows what he or she is doing,
* and 'error' for items that are definitely broken are much more useful.
*
* @param string $message
* @param string $type
* The type of message. This should be "ok", "warning" or "error". Other
* values can be used but how they are treated by the output routine
* is undefined.
*
* @return array
* A single formatted message, consisting of a key message and a key type.
*/
static function formatMessage($message, $type = 'error') {
return array('message' => $message, 'type' => $type);
}
}
......@@ -7,6 +7,8 @@
* @ingroup views_module_handlers
*/
use Drupal\views\Analyzer;
/**
* Implements hook_views_data().
*/
......@@ -729,7 +731,7 @@ function node_views_analyze($view) {
$roles[$role->rid] = $role;
}
if (!($roles['anonymous']->safe && $roles['authenticated']->safe)) {
$ret[] = views_ui_analysis(t('Some roles lack permission to access content, but display %display has no access control.', array('%display' => $display->display_title)), 'warning');
$ret[] = Analyzer::formatMessage(t('Some roles lack permission to access content, but display %display has no access control.', array('%display' => $display->display_title)), 'warning');
}
$filters = $display->handler->get_option('filters');
foreach ($filters as $filter) {
......@@ -737,7 +739,7 @@ function node_views_analyze($view) {
continue 2;
}
}
$ret[] = views_ui_analysis(t('Display %display has no access control but does not contain a filter for published nodes.', array('%display' => $display->display_title)), 'warning');
$ret[] = Analyzer::formatMessage(t('Display %display has no access control but does not contain a filter for published nodes.', array('%display' => $display->display_title)), 'warning');
}
}
}
......@@ -745,7 +747,7 @@ function node_views_analyze($view) {
foreach ($view->display as $id => $display) {
if ($display->display_plugin == 'page') {
if ($display->handler->get_option('path') == 'node/%') {
$ret[] = views_ui_analysis(t('Display %display has set node/% as path. This will not produce what you want. If you want to have multiple versions of the node view, use panels.', array('%display' => $display->display_title)), 'warning');
$ret[] = Analyzer::formatMessage(t('Display %display has set node/% as path. This will not produce what you want. If you want to have multiple versions of the node view, use panels.', array('%display' => $display->display_title)), 'warning');
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment