views_theme

views_theme($existing, $type, $theme, $path)

Implements hook_theme().

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

File

core/modules/views/views.module, line 84
Primarily Drupal hooks and global API functions to manipulate views.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
function views_theme($existing, $type, $theme, $path) {
  \Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.theme');
 
  // Some quasi clever array merging here.
  $base = array(
    'file' => 'views.theme.inc',
  );
 
  // Our extra version of pager from pager.inc
  $hooks['views_mini_pager'] = $base + array(
    'variables' => array('tags' => array(), 'quantity' => 9, 'element' => 0, 'parameters' => array()),
  );
 
  $variables = array(
    // 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' => array(
      'view_array' => array(),
      'view' => NULL,
      'rows' => array(),
      'header' => array(),
      'footer' => array(),
      'empty' => array(),
      'exposed' => array(),
      'more' => array(),
      'feed_icons' => array(),
      'pager' => array(),
      'title' => '',
      'attachment_before' => array(),
      'attachment_after' => array(),
    ),
    'style' => array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL),
    'row' => array('view' => NULL, 'options' => NULL, 'row' => NULL, 'field_alias' => NULL),
    'exposed_form' => array('view' => NULL, 'options' => NULL),
    'pager' => array(
      'view' => NULL, 'options' => NULL,
      'tags' => array(), 'quantity' => 9, 'element' => 0, 'parameters' => array()
    ),
  );
 
  // Default view themes
  $hooks['views_view_field'] = $base + array(
    'variables' => array('view' => NULL, 'field' => NULL, 'row' => NULL),
  );
  $hooks['views_view_grouping'] = $base + array(
    'variables' => array('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 = array();
  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']] = array(
        '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'] = array();
        }
        if (!in_array('views.theme.inc', $hooks[$def['theme']]['includes'])) {
          $hooks[$def['theme']]['includes'][] = $module_dir . '/views.theme.inc';
        }
      }
      // The theme_file definition is always relative to the modules directory.
      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 + array(
    'render element' => 'form',
  );
 
  $hooks['views_exposed_form'] = $base + array(
    'render element' => 'form',
  );
 
  return $hooks;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.