file preprocess.inc

Set up variables to be placed within the template (.html.twig) files.

The variables set up here apply to both templates (.html.twig) files and functions (theme_HOOK). These are also used for providing Twig Template naming conventions.

File

includes/preprocess.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Set up variables to be placed within the template (.html.twig) files.
  5. *
  6. * The variables set up here apply to both templates (.html.twig) files and
  7. * functions (theme_HOOK). These are also used for providing
  8. * @link https://www.drupal.org/node/2354645 Twig Template naming conventions @endlink.
  9. *
  10. * @see process.inc
  11. */
  12. use Drupal\Component\Utility\Html;
  13. use Drupal\Core\Template\Attribute;
  14. use Drupal\Core\Url;
  15. use Drupal\block\Entity\Block;
  16. use Drupal\uikit\UIkit;
  17. /**
  18. * Implements template_preprocess_page().
  19. */
  20. function uikit_preprocess_page(&$variables) {
  21. // Assign new header attributes.
  22. $variables['header_attributes'] = new Attribute();
  23. $variables['header_attributes']['id'] = 'page--header';
  24. $variables['header_attributes']['class'] = [];
  25. // Assign new navbar attributes.
  26. $variables['navbar_attributes'] = new Attribute();
  27. $variables['navbar_attributes']->setAttribute('id', 'page--navbar');
  28. $variables['navbar_attributes']->addClass('uk-navbar-container');
  29. $variables['navbar_attributes']->setAttribute('uk-navbar', '');
  30. // Assign new page attributes.
  31. $variables['page_attributes'] = new Attribute();
  32. $variables['page_attributes']['id'] = 'page';
  33. $variables['page_attributes']['class'] = [];
  34. // Add classes to page attribtues based on theme settings.
  35. $page_container = UIkit::getThemeSetting('page_container');
  36. if ($page_container) {
  37. $variables['page_attributes']['class'][] = 'uk-container';
  38. }
  39. $page_margin = UIkit::getThemeSetting('page_margin');
  40. if ($page_margin) {
  41. $variables['page_attributes']['class'][] = $page_margin;
  42. }
  43. // Get the grid classes for the page content and sidebars.
  44. $left_sidebar = !empty($variables['page']['sidebar_first']);
  45. $right_sidebar = !empty($variables['page']['sidebar_second']);
  46. $grid_classes = UIkit::getGridClasses($left_sidebar, $right_sidebar);
  47. // Add page content grid classes.
  48. $variables['content_attributes']['id'] = 'page--content';
  49. foreach ($grid_classes['content'] as $content_class) {
  50. $variables['content_attributes']['class'][] = $content_class;
  51. }
  52. if ($left_sidebar) {
  53. // Add left sidebar grid classes.
  54. $variables['sidebar_first_attributes'] = new Attribute();
  55. $variables['sidebar_first_attributes']['id'] = 'page--sidebar-first';
  56. $variables['sidebar_first_attributes']['class'] = [];
  57. foreach ($grid_classes['sidebar']['first'] as $sidebar_first_class) {
  58. $variables['sidebar_first_attributes']['class'][] = $sidebar_first_class;
  59. }
  60. }
  61. if ($right_sidebar) {
  62. // Add right sidebar grid classes.
  63. $variables['sidebar_second_attributes'] = new Attribute();
  64. $variables['sidebar_second_attributes']['id'] = 'page--sidebar-second';
  65. $variables['sidebar_second_attributes']['class'] = [];
  66. foreach ($grid_classes['sidebar']['second'] as $sidebar_second_class) {
  67. $variables['sidebar_second_attributes']['class'][] = $sidebar_second_class;
  68. }
  69. }
  70. }
  71. /**
  72. * Implements template_preprocess_region().
  73. */
  74. function uikit_preprocess_region(&$variables) {
  75. // Provide an id attribute to help themers.
  76. $region = $variables['region'];
  77. $variables['attributes']['id'] = "region--$region";
  78. if ($region == 'navbar') {
  79. $variables['content_left'] = [];
  80. $variables['content_center'] = [];
  81. $variables['content_right'] = [];
  82. foreach ($variables['elements'] as $id => $element) {
  83. if (is_array($element)) {
  84. $block = array();
  85. if (isset($element['#id'])) {
  86. $block = Block::load($element['#id']);
  87. }
  88. elseif (isset($element['#markup'])) {
  89. $block = Block::load($id);
  90. }
  91. if ($block) {
  92. $navbar_alignment = $block->getThirdPartySetting('uikit_components', 'uikit_navbar_alignment', 'left');
  93. switch ($navbar_alignment) {
  94. case 'left':
  95. $variables['content_left'][$id] = [
  96. '#markup' => render($element),
  97. ];
  98. break;
  99. case 'center':
  100. $variables['content_center'][$id] = [
  101. '#markup' => render($element),
  102. ];
  103. break;
  104. case 'right':
  105. $variables['content_right'][$id] = [
  106. '#markup' => render($element),
  107. ];
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. elseif ($region != 'offcanvas') {
  115. $region_style = UIkit::getThemeSetting($region . '_style');
  116. $card_style = UIkit::getThemeSetting($region . '_card_style');
  117. $region_style = $region_style ? $region_style : 0;
  118. $region_card_style = $card_style ? $card_style : 0;
  119. if ($region_style) {
  120. // Add the region style classes.
  121. switch ($region_style) {
  122. case 'card':
  123. $variables['attributes']['class'][] = 'uk-card';
  124. $variables['attributes']['class'][] = 'uk-card-body';
  125. if ($region_card_style) {
  126. $variables['attributes']['class'][] = "uk-card-$region_card_style";
  127. }
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. /**
  134. * Implements template_preprocess_block().
  135. */
  136. function uikit_preprocess_block(&$variables) {
  137. // Provide an id attribute to help themers.
  138. $id = $variables['elements']['#id'];
  139. $id = Html::cleanCssIdentifier($id);
  140. $variables['attributes']['id'] = $id;
  141. $base_plugin_id = $variables['base_plugin_id'];
  142. $system_menu_block = $base_plugin_id == 'system_menu_block';
  143. $block = Block::load($variables['elements']['#id']);
  144. $region = $block->getRegion();
  145. switch ($region) {
  146. case 'navbar':
  147. if ($system_menu_block) {
  148. // Define #theme variable for navbar menus.
  149. $variables['content']['#theme'] = 'menu__navbar';
  150. }
  151. break;
  152. case 'offcanvas':
  153. if ($system_menu_block) {
  154. // Define #theme variable for offcanvas menus.
  155. $variables['content']['#theme'] = 'menu__offcanvas';
  156. }
  157. break;
  158. }
  159. }
  160. /**
  161. * Implements template_preprocess_breadcrumb().
  162. */
  163. function uikit_preprocess_breadcrumb(&$variables) {
  164. // Add bool variable for breadcrumb display.
  165. $variables['display_breadcrumbs'] = UIkit::getThemeSetting('display_breadcrumbs');
  166. // Remove "Home" breadcrumb if disabled in the theme settings.
  167. $breadcrumb_home_link = UIkit::getThemeSetting('breakcrumbs_home_link');
  168. if (!$breadcrumb_home_link) {
  169. array_shift($variables['breadcrumb']);
  170. }
  171. if (UIkit::getThemeSetting('breakcrumbs_current_page')) {
  172. foreach ($variables['breadcrumb'] as $key => $breadcrumb) {
  173. // Set all other items as not being active.
  174. $variables['breadcrumb'][$key]['active'] = FALSE;
  175. }
  176. // Display the current page title if enabled in the theme settings and set
  177. // it as being active.
  178. $page_title = UIkit::getPageTitle();
  179. $variables['breadcrumb'][] = [
  180. 'text' => $page_title,
  181. 'active' => TRUE,
  182. ];
  183. }
  184. }
  185. /**
  186. * Implements template_preprocess_comment().
  187. */
  188. function uikit_preprocess_comment(&$variables) {
  189. $indented = $variables['elements']['#comment_indent'];
  190. $variables['indented'] = FALSE;
  191. if ($indented) {
  192. $variables['indented'] = TRUE;
  193. }
  194. }
  195. /**
  196. * Implements template_preprocess_details().
  197. */
  198. function uikit_preprocess_details(&$variables) {
  199. $element = $variables['element'];
  200. $group_details = isset($element['#group_details']) && $element['#group_details'];
  201. $variables['group_details'] = FALSE;
  202. if ($group_details) {
  203. $variables['group_details'] = TRUE;
  204. }
  205. }
  206. /**
  207. * Implements hook_preprocess_HOOK() for feed-icon.html.twig.
  208. *
  209. * Feed icon on the front page is missing the site name, as reported in
  210. * @link https://www.drupal.org/node/2082657 Feed icon on the front page misses site title @endlink.
  211. * We are following this issue so we can make the appropriate changes when the
  212. * issue is fixed.
  213. */
  214. function uikit_preprocess_feed_icon(&$variables) {
  215. $config = \Drupal::config('system.site');
  216. $variables['site_name'] = $config->get('name');
  217. }
  218. /**
  219. * Implements template_preprocess_field() for field--comment.html.twig.
  220. */
  221. function uikit_preprocess_field__comment(&$variables) {
  222. $comments = $variables['comments'];
  223. $comment_count = 0;
  224. foreach ($comments as $comment) {
  225. if (isset($comment['#comment'])) {
  226. $comment_count++;
  227. }
  228. }
  229. $variables['comment_count'] = $comment_count;
  230. }
  231. /**
  232. * Implements template_preprocess_form_element().
  233. */
  234. function uikit_preprocess_form_element(&$variables) {
  235. $element = $variables['element'];
  236. $type = $element['#type'];
  237. // Grouped form elements do not need the uk-margin class. This adds a
  238. // grouped variable to use in form-element.html.twig.
  239. $groups = isset($element['#groups']) && is_array($element['#groups']);
  240. $variables['grouped'] = FALSE;
  241. if ($groups) {
  242. $variables['grouped'] = TRUE;
  243. }
  244. // Perform various changes to the variables for advanced form elements.
  245. $form_advanced = $type == 'checkbox' || $type == 'radio';
  246. $variables['label_text'] = '';
  247. if ($form_advanced && isset($variables['label']['#title']) && $variables['label']['#title'] != '') {
  248. $variables['label_text'] = ['#markup' => $variables['label']['#title']];
  249. }
  250. }
  251. /**
  252. * Implements hook_preprocess_HOOK() for input.html.twig.
  253. */
  254. function uikit_preprocess_input(&$variables) {
  255. $element = $variables['element'];
  256. if (isset($element['#parents']) && isset($element['#errors']) && !empty($element['#validated'])) {
  257. $variables['attributes']['class'][] = 'uk-form-danger';
  258. }
  259. switch ($element['#type']) {
  260. case 'email':
  261. case 'number':
  262. case 'password':
  263. case 'search':
  264. case 'tel':
  265. case 'textfield':
  266. case 'url':
  267. $variables['attributes']['class'][] = 'uk-input';
  268. break;
  269. case 'checkbox':
  270. $variables['attributes']['class'][] = 'uk-checkbox';
  271. break;
  272. case 'date':
  273. $variables['attributes']['class'][] = 'uk-input';
  274. $variables['attributes']['class'][] = 'uk-form-small';
  275. $variables['attributes']['class'][] = 'uk-form-width-medium';
  276. break;
  277. case 'hidden':
  278. $variables['attributes']['class'][] = 'uk-margin';
  279. break;
  280. case 'radio':
  281. $variables['attributes']['class'][] = 'uk-radio';
  282. break;
  283. case 'range':
  284. $variables['attributes']['class'][] = 'uk-range';
  285. break;
  286. case 'button':
  287. case 'submit':
  288. $variables['attributes']['class'][] = 'uk-button';
  289. break;
  290. }
  291. if ($element['#type'] == 'button' || $element['#type'] == 'submit') {
  292. if (is_object($variables['element']['#value'])) {
  293. $value = $variables['element']['#value']->__toString();
  294. }
  295. else {
  296. $value = $variables['element']['#value'];
  297. }
  298. switch (TRUE) {
  299. case preg_match('/Apply.*/', $value):
  300. case preg_match('/Filter.*/', $value):
  301. case preg_match('/Generate.*/', $value):
  302. case preg_match('/Install.*/', $value):
  303. case preg_match('/Save.*/', $value):
  304. $variables['attributes']['class'][] = 'uk-button-primary';
  305. break;
  306. case preg_match('/Delete.*/', $value):
  307. case preg_match('/Remove.*/', $value):
  308. $variables['attributes']['class'][] = 'uk-button-danger';
  309. break;
  310. default:
  311. $variables['attributes']['class'][] = 'uk-button-default';
  312. }
  313. }
  314. }
  315. /**
  316. * Implements template_preprocess_links() for links--comment.html.twig.
  317. */
  318. function uikit_preprocess_links__comment(&$variables) {
  319. foreach ($variables['links'] as $key => $link) {
  320. if (isset($link['link'])) {
  321. $url = $link['link']['#url']->toString();
  322. $title = $link['text']->getUntranslatedString();
  323. $variables['links'][$key]['link'] = [
  324. 'title' => t('@title', ['@title' => $title]),
  325. ];
  326. $variables['links'][$key]['link']['attributes'] = new Attribute();
  327. $variables['links'][$key]['link']['attributes']->setAttribute('href', $url);
  328. $variables['links'][$key]['link']['attributes']->addClass('uk-link-muted');
  329. }
  330. }
  331. }
  332. /**
  333. * Implements template_preprocess_menu_local_action().
  334. */
  335. function uikit_preprocess_menu_local_action(&$variables) {
  336. $link = $variables['element']['#link'];
  337. // Set the link variable for menu-local-action.html.twig.
  338. $variables['link'] = [
  339. 'title' => $link['title'],
  340. 'url' => $link['url'],
  341. ];
  342. }
  343. /**
  344. * Implements hook_preprocess_HOOK() for menu-local-tasks.html.twig.
  345. */
  346. function uikit_preprocess_menu_local_tasks(&$variables) {
  347. $primary_tasks_style = UIkit::getThemeSetting('primary_tasks_style');
  348. $secondary_tasks_style = UIkit::getThemeSetting('secondary_tasks_style');
  349. // Create new primary tasks attributes.
  350. $variables['primary_attributes'] = new Attribute();
  351. $variables['primary_attributes']['class'] = [];
  352. if ($primary_tasks_style == 'uk-tab') {
  353. // Add uk-tab class to primary tasks.
  354. $variables['primary_attributes']['class'][] = 'uk-tab';
  355. }
  356. else {
  357. // Add uk-subnav class to primary tasks.
  358. $variables['primary_attributes']['class'][] = 'uk-subnav';
  359. if ($primary_tasks_style) {
  360. // Add uk-subnav-* class to primary tasks.
  361. $variables['primary_attributes']['class'][] = $primary_tasks_style;
  362. }
  363. }
  364. // Create new secondary tasks attributes.
  365. $variables['secondary_attributes'] = new Attribute();
  366. $variables['secondary_attributes']['class'] = ['uk-subnav'];
  367. if ($secondary_tasks_style) {
  368. // Add uk-subnav-* class to secondary tasks.
  369. $variables['secondary_attributes']['class'][] = $secondary_tasks_style;
  370. }
  371. }
  372. /**
  373. * Implements hook_preprocess_HOOK() for menu--navbar.html.twig
  374. */
  375. function uikit_preprocess_menu__navbar(&$variables) {
  376. $items = $variables['items'];
  377. $current_url = Url::fromRoute('<current>');
  378. $current_path = $current_url->toString();
  379. foreach ($items as $key => $item) {
  380. $variables['items'][$key]['has_url'] = TRUE;
  381. $route = $item['url']->getRouteName();
  382. $is_active = FALSE;
  383. if ($route == '<none>') {
  384. // <none> routes do not have a URL.
  385. $variables['items'][$key]['has_url'] = FALSE;
  386. }
  387. if ($item['url']->toString() == '/user') {
  388. $uid = \Drupal::currentUser()->id();
  389. if ($uid && $current_path == "/user/$uid") {
  390. $is_active = TRUE;
  391. }
  392. }
  393. if ($is_active && !$item['attributes']->hasClass('uk-active')) {
  394. $item['attributes']->addClass('uk-active');
  395. }
  396. if ($item['below']) {
  397. foreach ($item['below'] as $below_key => $below_item) {
  398. // Set is_divider and is_header variables to FALSE.
  399. $variables['items'][$key]['below'][$below_key]['is_divider'] = FALSE;
  400. $variables['items'][$key]['below'][$below_key]['is_header'] = FALSE;
  401. // Get the menu item options and menu_item_type, if set. menu_item_type
  402. // is set by the UIkit Components module.
  403. $options = $below_item['url']->getOptions();
  404. $menu_item_type = isset($options['menu_item_type']) ? $options['menu_item_type'] : 0;
  405. if ($menu_item_type) {
  406. switch ($menu_item_type) {
  407. case 'nav_divider':
  408. // Add uk-nav-divider class and set is_divider to TRUE for
  409. // menu--navbar.html.twig.
  410. $variables['items'][$key]['below'][$below_key]['attributes']->addClass('uk-nav-divider');
  411. $variables['items'][$key]['below'][$below_key]['is_divider'] = TRUE;
  412. break;
  413. case 'nav_header':
  414. // Add uk-nav-header class and set is_header to TRUE for
  415. // menu--navbar.html.twig.
  416. $variables['items'][$key]['below'][$below_key]['attributes']->addClass('uk-nav-header');
  417. $variables['items'][$key]['below'][$below_key]['is_header'] = TRUE;
  418. break;
  419. }
  420. }
  421. }
  422. }
  423. }
  424. }
  425. /**
  426. * Implements hook_preprocess_HOOK() for menu--offcanvas.html.twig
  427. */
  428. function uikit_preprocess_menu__offcanvas(&$variables) {
  429. $items = $variables['items'];
  430. foreach ($items as $key => $item) {
  431. $variables['items'][$key]['display_item'] = TRUE;
  432. $variables['items'][$key]['has_url'] = TRUE;
  433. $route = $item['url']->getRouteName();
  434. if ($route == '<none>') {
  435. $variables['items'][$key]['has_url'] = FALSE;
  436. }
  437. if ($item['below']) {
  438. $variables['items'][$key]['has_url'] = FALSE;
  439. foreach ($item['below'] as $below_key => $below_item) {
  440. // Set is_divider and is_header variables to FALSE.
  441. $variables['items'][$key]['below'][$below_key]['display_item'] = TRUE;
  442. $variables['items'][$key]['below'][$below_key]['has_url'] = TRUE;
  443. // Get the menu item options and menu_item_type, if set. menu_item_type
  444. // is set by the UIkit Components module.
  445. $options = $below_item['url']->getOptions();
  446. $menu_item_type = isset($options['menu_item_type']) ? $options['menu_item_type'] : 0;
  447. if ($menu_item_type) {
  448. switch ($menu_item_type) {
  449. case 'nav_divider':
  450. case 'nav_header':
  451. $variables['items'][$key]['below'][$below_key]['display_item'] = FALSE;
  452. break;
  453. }
  454. }
  455. }
  456. }
  457. }
  458. }

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Log in