basic.theme 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * @file
  4. * Preprocess functions for Basic.
  5. */
  6. use Drupal\Core\Cache\CacheableMetadata;
  7. /**
  8. * Prepares variables for the html.html.twig template.
  9. */
  10. function basic_preprocess_html(&$variables) {
  11. try {
  12. $variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
  13. }
  14. catch (Exception $e) {
  15. // If the database is not yet available, set default values for these
  16. // variables.
  17. $variables['is_front'] = FALSE;
  18. }
  19. // If we're on the front page.
  20. if (!$variables['is_front']) {
  21. // Add unique classes for each page and website section.
  22. $path = \Drupal::service('path.current')->getPath();
  23. $alias = \Drupal::service('path.alias_manager')->getAliasByPath($path);
  24. $alias = trim($alias, '/');
  25. if (!empty($alias)) {
  26. $name = str_replace('/', '-', $alias);
  27. $variables['attributes']['class'][] = 'page-' . $name;
  28. list($section,) = explode('/', $alias, 2);
  29. if (!empty($section)) {
  30. $variables['attributes']['class'][] = 'section-' . $section;
  31. }
  32. }
  33. }
  34. // Add cachability metadata.
  35. $theme_name = \Drupal::theme()->getActiveTheme()->getName();
  36. $theme_settings = \Drupal::config($theme_name . '.settings');
  37. CacheableMetadata::createFromRenderArray($variables)
  38. ->addCacheableDependency($theme_settings)
  39. ->applyTo($variables);
  40. // Union all theme setting variables to the html.html.twig template.
  41. $variables += $theme_settings->getOriginal();
  42. }
  43. /**
  44. * Prepares variables for the field.html.twig template.
  45. */
  46. function basic_preprocess_field(&$variables, $hook) {
  47. // Make additional variables available to the template.
  48. $variables['bundle'] = $variables['element']['#bundle'];
  49. }