You are here

function toolbar_themes_theme_registry_alter in Toolbar Themes 8

Implements hook_theme_registry_alter(). ...well this seems repetitive. TODO, abstract something or?

Parameters

$theme_registry:

File

./toolbar_themes.module, line 103

Code

function toolbar_themes_theme_registry_alter(&$theme_registry) {
  $config = \Drupal::config('toolbar_themes.settings');
  $default_theme = $config
    ->get('default_theme');
  $path = drupal_get_path('module', 'toolbar_themes');

  // Use our own template for toolbar.
  $theme_registry['toolbar']['template'] = 'toolbar-themes--toolbar';
  $theme_registry['toolbar']['path'] = $path . '/templates';
  $theme_registry['toolbar']['preprocess functions'][] = 'toolbar_themes_preprocess_toolbar_themes__toolbar';

  // Use our own template for toolbar menus.
  $theme_registry['menu__toolbar']['template'] = 'toolbar-themes--menu';
  $theme_registry['menu__toolbar']['path'] = $path . '/templates';
  $theme_registry['menu__toolbar']['preprocess functions'][] = 'toolbar_themes_preprocess_toolbar_themes__menu';
  $theme_registry['menu__toolbar__admin'] = $theme_registry['menu__toolbar'];

  // Register templates and preprocess function for themes.
  $theme_handler = \Drupal::service('theme_handler');
  $base_theme = [];
  $toolbar_implementation = '';
  $menu_implementation = '';

  // Check for a base theme.
  $default_theme = toolbar_themes_get_theme_definition($default_theme);
  if (isset($default_theme['base_theme'])) {
    $base_theme = toolbar_themes_get_theme_definition($default_theme['base_theme']);
  }

  // Toolbar template.
  if (isset($default_theme['template_toolbar'])) {
    $toolbar_implementation = $default_theme;
  }
  elseif (!empty($base_theme)) {
    if (isset($base_theme['template_toolbar'])) {
      $toolbar_implementation = $base_theme;
    }
  }
  if (!empty($toolbar_implementation)) {
    $provider_toolbar = $toolbar_implementation['provider'];
    if ($theme_handler
      ->themeExists($provider_toolbar)) {
      $provider_path = drupal_get_path('theme', $provider_toolbar);
    }
    else {
      $provider_path = drupal_get_path('module', $provider_toolbar);
    }
    if (isset($toolbar_implementation['path']) && !empty($toolbar_implementation['path'])) {
      $template_toolbar_parts = explode('/', $toolbar_implementation['template_toolbar']);
      $template_toolbar = array_pop($template_toolbar_parts);
      $template_toolbar_path = implode('/', $template_toolbar_parts);
      $preprocess_toolbar = $provider_toolbar . '_preprocess_' . strtr($template_toolbar, '-', '_');
      $theme_registry['toolbar']['template'] = $template_toolbar;
      $theme_registry['toolbar']['path'] = $provider_path . '/' . $toolbar_implementation['path'] . '/' . $template_toolbar_path;
      $theme_registry['toolbar']['preprocess functions'][] = $preprocess_toolbar;
    }
  }

  // Menu template.
  if (isset($default_theme['template_toolbar_menu'])) {
    $menu_implementation = $default_theme;
  }
  elseif (!empty($base_theme)) {
    if (isset($base_theme['template_toolbar_menu'])) {
      $menu_implementation = $base_theme;
    }
  }
  if (!empty($menu_implementation)) {
    $provider_menu = $menu_implementation['provider'];
    if ($theme_handler
      ->themeExists($provider_menu)) {
      $provider_path = drupal_get_path('theme', $provider_menu);
    }
    else {
      $provider_path = drupal_get_path('module', $provider_menu);
    }
    if (isset($menu_implementation['path']) && !empty($menu_implementation['path'])) {
      $template_menu_parts = explode('/', $menu_implementation['template_toolbar_menu']);
      $template_menu = array_pop($template_menu_parts);
      $template_menu_path = implode('/', $template_menu_parts);
      $preprocess_menu = $provider_menu . '_preprocess_' . strtr($template_menu, '-', '_');
      $theme_registry['menu__toolbar']['template'] = $template_menu;
      $theme_registry['menu__toolbar']['path'] = $provider_path . '/' . $menu_implementation['path'] . '/' . $template_menu_path;
      $theme_registry['menu__toolbar']['preprocess functions'][] = $preprocess_menu;
      $theme_registry['menu__toolbar__admin'] = $theme_registry['menu__toolbar'];
    }
  }
}