function views_theme

Same name in other branches
  1. 9 core/modules/views/views.module \views_theme()
  2. 10 core/modules/views/views.module \views_theme()

Implements hook_theme().

Register views theming functions and those that are defined via views plugin definitions.

File

core/modules/views/views.module, line 84

Code

function views_theme($existing, $type, $theme, $path) {
    \Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.theme');
    // Some quasi clever array merging here.
    $base = [
        'file' => 'views.theme.inc',
    ];
    // Our extra version of pager from pager.inc
    $hooks['views_mini_pager'] = $base + [
        'variables' => [
            'tags' => [],
            'quantity' => 9,
            'element' => 0,
            'parameters' => [],
        ],
    ];
    $variables = [
        // For displays, we pass in a dummy array as the first parameter, since
        // $view is an object but the core contextual_preprocess() function only
        // attaches contextual links when the primary theme argument is an array.
'display' => [
            'view_array' => [],
            'view' => NULL,
            'rows' => [],
            'header' => [],
            'footer' => [],
            'empty' => [],
            'exposed' => [],
            'more' => [],
            'feed_icons' => [],
            'pager' => [],
            'title' => '',
            'attachment_before' => [],
            'attachment_after' => [],
        ],
        'style' => [
            'view' => NULL,
            'options' => NULL,
            'rows' => NULL,
            'title' => NULL,
        ],
        'row' => [
            'view' => NULL,
            'options' => NULL,
            'row' => NULL,
            'field_alias' => NULL,
        ],
        'exposed_form' => [
            'view' => NULL,
            'options' => NULL,
        ],
        'pager' => [
            'view' => NULL,
            'options' => NULL,
            'tags' => [],
            'quantity' => 9,
            'element' => 0,
            'parameters' => [],
        ],
    ];
    // Default view themes
    $hooks['views_view_field'] = $base + [
        'variables' => [
            'view' => NULL,
            'field' => NULL,
            'row' => NULL,
        ],
    ];
    $hooks['views_view_grouping'] = $base + [
        'variables' => [
            'view' => NULL,
            'grouping' => NULL,
            'grouping_level' => NULL,
            'rows' => NULL,
            'title' => NULL,
        ],
    ];
    // Only display, pager, row, and style plugins can provide theme hooks.
    $plugin_types = [
        'display',
        'pager',
        'row',
        'style',
        'exposed_form',
    ];
    $plugins = [];
    foreach ($plugin_types as $plugin_type) {
        $plugins[$plugin_type] = Views::pluginManager($plugin_type)->getDefinitions();
    }
    $module_handler = \Drupal::moduleHandler();
    // Register theme functions for all style plugins. It provides a basic auto
    // implementation of theme functions or template files by using the plugin
    // definitions (theme, theme_file, module, register_theme). Template files are
    // assumed to be located in the templates folder.
    foreach ($plugins as $type => $info) {
        foreach ($info as $def) {
            // Not all plugins have theme functions, and they can also explicitly
            // prevent a theme function from being registered automatically.
            if (!isset($def['theme']) || empty($def['register_theme'])) {
                continue;
            }
            // For each theme registration, we have a base directory to check for the
            // templates folder. This will be relative to the root of the given module
            // folder, so we always need a module definition.
            // @todo: watchdog or exception?
            if (!isset($def['provider']) || !$module_handler->moduleExists($def['provider'])) {
                continue;
            }
            $hooks[$def['theme']] = [
                'variables' => $variables[$type],
            ];
            // We always use the module directory as base dir.
            $module_dir = drupal_get_path('module', $def['provider']);
            $hooks[$def['theme']]['path'] = $module_dir;
            // For the views module we ensure views.theme.inc is included.
            if ($def['provider'] == 'views') {
                if (!isset($hooks[$def['theme']]['includes'])) {
                    $hooks[$def['theme']]['includes'] = [];
                }
                if (!in_array('views.theme.inc', $hooks[$def['theme']]['includes'])) {
                    $hooks[$def['theme']]['includes'][] = $module_dir . '/views.theme.inc';
                }
            }
            elseif (!empty($def['theme_file'])) {
                $hooks[$def['theme']]['file'] = $def['theme_file'];
            }
            // Whenever we have a theme file, we include it directly so we can
            // auto-detect the theme function.
            if (isset($def['theme_file'])) {
                $include = \Drupal::root() . '/' . $module_dir . '/' . $def['theme_file'];
                if (is_file($include)) {
                    require_once $include;
                }
            }
            // If there is no theme function for the given theme definition, it must
            // be a template file. By default this file is located in the /templates
            // directory of the module's folder. If a module wants to define its own
            // location it has to set register_theme of the plugin to FALSE and
            // implement hook_theme() by itself.
            if (!function_exists('theme_' . $def['theme'])) {
                $hooks[$def['theme']]['path'] .= '/templates';
                $hooks[$def['theme']]['template'] = Html::cleanCssIdentifier($def['theme']);
            }
            else {
                $hooks[$def['theme']]['function'] = 'theme_' . $def['theme'];
            }
        }
    }
    $hooks['views_form_views_form'] = $base + [
        'render element' => 'form',
    ];
    $hooks['views_exposed_form'] = $base + [
        'render element' => 'form',
    ];
    return $hooks;
}

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