function ViewsThemeHooks::theme

Implements hook_theme().

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

Attributes

#[Hook('theme')]

File

core/modules/views/src/Hook/ViewsThemeHooks.php, line 53

Class

ViewsThemeHooks
Hook implementations for views.

Namespace

Drupal\views\Hook

Code

public function theme($existing, $type, $theme, $path) : array {
  // Our extra version of pager.
  $hooks['views_mini_pager'] = [
    'variables' => [
      'tags' => [],
      'quantity' => 9,
      'element' => 0,
      'pagination_heading_level' => 'h4',
      'parameters' => [],
    ],
    'initial preprocess' => static::class . ':preprocessViewsMiniPager',
  ];
  $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,
      'pagination_heading_level' => 'h4',
      'parameters' => [],
    ],
  ];
  // Default view themes.
  $hooks['views_view_field'] = [
    'variables' => [
      'view' => NULL,
      'field' => NULL,
      'row' => NULL,
    ],
    'initial preprocess' => static::class . ':preprocessViewsViewField',
  ];
  $hooks['views_view_grouping'] = [
    '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();
  }
  // Register theme functions for all style plugins. It provides a basic auto
  // implementation of 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']) || !$this->moduleHandler
        ->moduleExists($def['provider'])) {
        continue;
      }
      $hooks[$def['theme']] = [
        'variables' => $variables[$type],
      ];
      // We always use the module directory as base dir.
      $module_dir = $this->moduleExtensionList
        ->getPath($def['provider']);
      $hooks[$def['theme']]['path'] = $module_dir;
      if (!empty($def['theme_file'])) {
        @trigger_error('Providing a theme_file definition for plugin ' . $def['id'] . ' is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Theme hook include files are deprecated. See https://www.drupal.org/node/3548325', E_USER_DEPRECATED);
        $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.
        $include = \Drupal::root() . '/' . $module_dir . '/' . $def['theme_file'];
        if (is_file($include)) {
          require_once $include;
        }
      }
      $initial_preprocess_class = 'Drupal\\' . $def['provider'] . '\\Hook\\' . Container::camelize($def['provider']) . 'ThemeHooks';
      $initial_preprocess_method = 'preprocess' . Container::camelize($def['theme']);
      if (method_exists($initial_preprocess_class, $initial_preprocess_method)) {
        $hooks[$def['theme']]['initial preprocess'] = $initial_preprocess_class . ':' . $initial_preprocess_method;
      }
      // By default any templates for a module are 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.
      $hooks[$def['theme']]['path'] .= '/templates';
      $hooks[$def['theme']]['template'] = Html::cleanCssIdentifier($def['theme']);
    }
  }
  $hooks['views_form_views_form'] = [
    'render element' => 'form',
  ];
  $hooks['views_exposed_form'] = [
    'render element' => 'form',
    'initial preprocess' => static::class . ':preprocessViewsExposedForm',
  ];
  return $hooks;
}

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