class Analyzer

Same name in other branches
  1. 9 core/modules/views/src/Analyzer.php \Drupal\views\Analyzer
  2. 8.9.x core/modules/views/src/Analyzer.php \Drupal\views\Analyzer
  3. 10 core/modules/views/src/Analyzer.php \Drupal\views\Analyzer

View analyzer plugin manager.

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.

Hierarchy

Expanded class hierarchy of Analyzer

6 files declare their use of Analyzer
node.views.inc in core/modules/node/node.views.inc
Provide views data for node.module.
NodeViewsHooks.php in core/modules/node/src/Hook/NodeViewsHooks.php
views.api.php in core/modules/views/views.api.php
Describes hooks and plugins provided by the Views module.
ViewsTestDataViewsHooks.php in core/modules/views/tests/modules/views_test_data/src/Hook/ViewsTestDataViewsHooks.php
ViewsUiHooks.php in core/modules/views_ui/src/Hook/ViewsUiHooks.php

... See full list

1 string reference to 'Analyzer'
views.services.yml in core/modules/views/views.services.yml
core/modules/views/views.services.yml
1 service uses Analyzer
views.analyzer in core/modules/views/views.services.yml
Drupal\views\Analyzer

File

core/modules/views/src/Analyzer.php, line 17

Namespace

Drupal\views
View source
class Analyzer {
    
    /**
     * A module handler that invokes the 'views_analyze' hook.
     *
     * @var \Drupal\Core\Extension\ModuleHandlerInterface
     */
    protected $moduleHandler;
    
    /**
     * Constructs an Analyzer object.
     *
     * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
     *   The module handler that invokes the 'views_analyze' hook.
     */
    public function __construct(ModuleHandlerInterface $module_handler) {
        $this->moduleHandler = $module_handler;
    }
    
    /**
     * Analyzes a review and return the results.
     *
     * @param \Drupal\views\ViewExecutable $view
     *   The view to analyze.
     *
     * @return array
     *   An array of analyze results organized into arrays keyed by 'ok',
     *   'warning' and 'error'.
     */
    public function getMessages(ViewExecutable $view) {
        $view->initDisplay();
        $messages = $this->moduleHandler
            ->invokeAll('views_analyze', [
            $view,
        ]);
        return $messages;
    }
    
    /**
     * Formats the analyze result into a message string.
     *
     * This is based upon the format of
     * \Drupal\Core\Messenger\MessengerInterface::addMessage() which uses separate
     * boxes for "ok", "warning" and "error".
     */
    public function formatMessages(array $messages) {
        if (empty($messages)) {
            $messages = [
                static::formatMessage(t('View analysis can find nothing to report.'), 'ok'),
            ];
        }
        $types = [
            'ok' => [],
            'warning' => [],
            'error' => [],
        ];
        foreach ($messages as $message) {
            if (empty($types[$message['type']])) {
                $types[$message['type']] = [];
            }
            $types[$message['type']][] = $message['message'];
        }
        $output = '';
        foreach ($types as $type => $messages) {
            $type .= ' messages';
            $message = '';
            if (count($messages) > 1) {
                $item_list = [
                    '#theme' => 'item_list',
                    '#items' => $messages,
                ];
                $message = \Drupal::service('renderer')->render($item_list);
            }
            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 they are doing, and 'error'
     * for items that are definitely broken are much more useful.
     *
     * @param string $message
     *   The 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.
     */
    public static function formatMessage($message, $type = 'error') {
        return [
            'message' => $message,
            'type' => $type,
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary
Analyzer::$moduleHandler protected property A module handler that invokes the &#039;views_analyze&#039; hook.
Analyzer::formatMessage public static function Formats an analysis message.
Analyzer::formatMessages public function Formats the analyze result into a message string.
Analyzer::getMessages public function Analyzes a review and return the results.
Analyzer::__construct public function Constructs an Analyzer object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.