You are here

function toolbar_themes_preprocess_toolbar_themes__toolbar in Toolbar Themes 8

Implements hook_preprocess_HOOK().

Piling everything into preprocess like this is probably lazy programming, but it's what I'm familiar with, if you have a better way to do some of this stuff patches are most welcome!

Parameters

$variables:

1 string reference to 'toolbar_themes_preprocess_toolbar_themes__toolbar'
toolbar_themes_theme_registry_alter in ./toolbar_themes.module
Implements hook_theme_registry_alter(). ...well this seems repetitive. TODO, abstract something or?

File

./toolbar_themes.module, line 232

Code

function toolbar_themes_preprocess_toolbar_themes__toolbar(&$variables) {
  $config = \Drupal::config('toolbar_themes.settings');
  $path = base_path() . drupal_get_path('module', 'toolbar_themes');

  // Attributes for advanced styles.
  $variables['tray.content_attributes'] = new Attribute();

  // Wrapper class.
  $variables['attributes']['class'][] = 'toolbar-themes-toolbar';

  // Font size. Set as an attribute in toolbar-themes--toolbar.html.twig.
  //  if ($font_size = $config->get('font_size')) {
  //    $ems = $font_size/16;
  //    $variables['font_size'] = 'font-size:' . $ems . 'em';
  //  }
  // Icons.
  $variables['show_icons'] = $config
    ->get('icons');

  // Tabs.
  $variables['show_tabs'] = $config
    ->get('tabs');
  if ($variables['show_tabs'] === 1) {
    if (isset($variables['tabs'])) {
      foreach ($variables['tabs'] as $item_key => $item_values) {

        // Home, hide it.
        if ($item_key === 'home') {
          $variables['tabs'][$item_key]['attributes']
            ->addClass('hidden');
        }

        // Hide empty items.
        if (isset($item_values['link']['#markup']) && $item_values['link']['#markup'] === '<a href="/" class="toolbar-item"></a>') {
          $variables['tabs'][$item_key]['attributes']
            ->addClass('hidden');
        }
      }
    }
  }

  // Load the default themes libraries.
  $default_theme = $config
    ->get('default_theme');
  $library_names = toolbar_themes_get_library_names();
  foreach ($library_names as $library_key => $libraries) {
    if ($default_theme === $library_key) {
      foreach ($libraries as $type => $library_name) {
        if ($type !== 'icons') {
          $variables['#attached']['library'][] = $library_name;
        }
        if ($variables['show_icons'] === 1) {
          $variables['#attached']['library'][] = $library_name;
        }
      }
    }
  }

  // Attach drupalSettings.
  $drupalSettings = [
    'icons' => $variables['show_icons'],
    'tabs' => $variables['show_tabs'],
    'path' => $path,
  ];
  $variables['#attached']['drupalSettings']['toolbar'] = $drupalSettings;

  // The module base library.
  $variables['#attached']['library'][] = 'toolbar_themes/base';

  // Inject theme definitions.
  $variables['base_theme'] = [];
  $variables['default_theme'] = toolbar_themes_get_theme_definition($default_theme);
  if (isset($variables['default_theme']['base_theme'])) {
    $variables['base_theme'] = toolbar_themes_get_theme_definition($variables['default_theme']['base_theme']);
  }
}