file UIkit.php

File

src/UIkit.php
View source
  1. <?php
  2. namespace Drupal\uikit;
  3. /**
  4. * Provides helper functions for the UIkit base theme.
  5. */
  6. class UIkit {
  7. /**
  8. * The UIkit library project page.
  9. *
  10. * @var string
  11. */
  12. const UIKIT_LIBRARY = 'https://getuikit.com/';
  13. /**
  14. * The UIkit library version supported in the UIkit base theme.
  15. *
  16. * @var string
  17. */
  18. const UIKIT_LIBRARY_VERSION = '3.0.0-beta.35';
  19. /**
  20. * The Drupal project page for the UIkit base theme.
  21. *
  22. * @var string
  23. */
  24. const UIKIT_PROJECT = 'https://www.drupal.org/project/uikit';
  25. /**
  26. * The Drupal project branch for the UIkit base theme.
  27. *
  28. * @var string
  29. */
  30. const UIKIT_PROJECT_BRANCH = '8.x-3.x';
  31. /**
  32. * The Drupal project API site for the UIkit base theme.
  33. *
  34. * @var string
  35. */
  36. const UIKIT_PROJECT_API = 'http://uikit-drupal.com';
  37. /**
  38. * Loads a project's include file.
  39. *
  40. * This function essentially does the same as Drupal core's
  41. * module_load_include() function, except targeting theme include files. It also
  42. * allows you to place the include files in a sub-directory of the theme for
  43. * better organization.
  44. *
  45. * Examples:
  46. * @code
  47. * // Load node.admin.inc from the node module.
  48. * UIkitComponents::loadIncludeFile('inc', 'node', 'module', 'node.admin');
  49. *
  50. * // Load includes/alter.inc from the uikit theme.
  51. * UIkitComponents::loadIncludeFile('inc', 'uikit', 'theme', 'preprocess', 'includes');
  52. * @endcode
  53. *
  54. * Do not use this function in a global context since it requires Drupal to be
  55. * fully bootstrapped, use require_once DRUPAL_ROOT . '/path/file' instead.
  56. *
  57. * @param string $type
  58. * The include file's type (file extension).
  59. * @param string $project
  60. * The project to which the include file belongs.
  61. * @param string $project_type
  62. * The project type to which the include file belongs, either "theme" or
  63. * "module". Defaults to "module".
  64. * @param string $name
  65. * (optional) The base file name (without the $type extension). If omitted,
  66. * $theme is used; i.e., resulting in "$theme.$type" by default.
  67. * @param string $sub_directory
  68. * (optional) The sub-directory to which the include file resides.
  69. *
  70. * @return string
  71. * The name of the included file, if successful; FALSE otherwise.
  72. */
  73. public static function loadIncludeFile($type, $project, $project_type = 'module', $name = NULL, $sub_directory = '') {
  74. static $files = [];
  75. if (isset($sub_directory)) {
  76. $sub_directory = '/' . $sub_directory;
  77. }
  78. if (!isset($name)) {
  79. $name = $project;
  80. }
  81. $key = $type . ':' . $project . ':' . $name . ':' . $sub_directory;
  82. if (isset($files[$key])) {
  83. return $files[$key];
  84. }
  85. if (function_exists('drupal_get_path')) {
  86. $file = DRUPAL_ROOT . '/' . drupal_get_path($project_type, $project) . "$sub_directory/$name.$type";
  87. if (is_file($file)) {
  88. require_once $file;
  89. $files[$key] = $file;
  90. return $file;
  91. }
  92. else {
  93. $files[$key] = FALSE;
  94. }
  95. }
  96. return FALSE;
  97. }
  98. /**
  99. * Retrieves the active theme.
  100. *
  101. * @return
  102. * The active theme's machine name.
  103. */
  104. public static function getActiveTheme() {
  105. return \Drupal::theme()->getActiveTheme()->getName();
  106. }
  107. /**
  108. * Retrieves a theme setting.
  109. *
  110. * @param null $setting
  111. * The machine-name of the theme setting to retrieve.
  112. * @param $theme
  113. * The theme to retrieve the setting for. Defaults to the active theme.
  114. *
  115. * @return mixed
  116. * The theme setting's value.
  117. */
  118. public static function getThemeSetting($setting, $theme = NULL) {
  119. if (empty($theme)) {
  120. $theme = self::getActiveTheme();
  121. }
  122. if (!empty($setting)) {
  123. return theme_get_setting($setting, $theme);
  124. }
  125. else {
  126. throw new \LogicException('Missing argument $setting');
  127. }
  128. }
  129. /**
  130. * Retrieves the grid classes used in page.html.twig.
  131. *
  132. * @param bool $sidebar_first
  133. * True if the sidebar_first region has content, false otherwise.
  134. * @param bool $sidebar_second
  135. * True if the sidebar_second region has content, false otherwise.
  136. *
  137. * @return array
  138. * An array of grid classes to use in page.html.twig.
  139. */
  140. public static function getGridClasses($sidebar_first = FALSE, $sidebar_second = FALSE) {
  141. $standard_layout = self::getThemeSetting('standard_sidebar_positions');
  142. $tablet_layout = self::getThemeSetting('tablet_sidebar_positions');
  143. $mobile_layout = self::getThemeSetting('mobile_sidebar_positions');
  144. $grid_classes = [
  145. 'content' => [],
  146. 'sidebar' => [],
  147. ];
  148. if ($sidebar_first) {
  149. $grid_classes['sidebar']['first'] = [];
  150. }
  151. if ($sidebar_second) {
  152. $grid_classes['sidebar']['second'] = [];
  153. }
  154. if ($sidebar_first && $sidebar_second) {
  155. $grid_classes['content'][] = 'uk-width-1-2@l';
  156. $grid_classes['sidebar']['first'][] = 'uk-width-1-4@l';
  157. $grid_classes['sidebar']['second'][] = 'uk-width-1-4@l';
  158. switch ($standard_layout) {
  159. case 'holy-grail':
  160. $grid_classes['content'][] = 'uk-push-large-1-4';
  161. $grid_classes['sidebar']['first'][] = 'uk-pull-1-2@l';
  162. $grid_classes['sidebar']['second'][] = 'uk-push-pull-@l';
  163. break;
  164. case 'sidebars-left':
  165. $grid_classes['content'][] = 'uk-push-1-2@l';
  166. $grid_classes['sidebar']['first'][] = 'uk-pull-1-2@l';
  167. $grid_classes['sidebar']['second'][] = 'uk-pull-1-2@l';
  168. break;
  169. case 'sidebars-right':
  170. $grid_classes['content'][] = 'uk-push-pull-@l';
  171. $grid_classes['sidebar']['first'][] = 'uk-push-pull-@l';
  172. $grid_classes['sidebar']['second'][] = 'uk-push-pull-@l';
  173. break;
  174. }
  175. switch ($tablet_layout) {
  176. case 'holy-grail':
  177. $grid_classes['content'][] = 'uk-width-1-2@m';
  178. $grid_classes['content'][] = 'uk-push-1-4@m';
  179. $grid_classes['sidebar']['first'][] = 'uk-width-1-4@m';
  180. $grid_classes['sidebar']['first'][] = 'uk-pull-1-2@m';
  181. $grid_classes['sidebar']['second'][] = 'uk-width-1-4@m';
  182. $grid_classes['sidebar']['second'][] = 'uk-push-pull-@m';
  183. break;
  184. case 'sidebars-left':
  185. $grid_classes['content'][] = 'uk-width-1-2@m';
  186. $grid_classes['content'][] = 'uk-push-1-2@m';
  187. $grid_classes['sidebar']['first'][] = 'uk-width-1-4@m';
  188. $grid_classes['sidebar']['first'][] = 'uk-pull-1-2@m';
  189. $grid_classes['sidebar']['second'][] = 'uk-width-1-4@m';
  190. $grid_classes['sidebar']['second'][] = 'uk-pull-1-2@m';
  191. break;
  192. case 'sidebars-right':
  193. $grid_classes['content'][] = 'uk-width-1-2@m';
  194. $grid_classes['content'][] = 'uk-push-pull-@m';
  195. $grid_classes['sidebar']['first'][] = 'uk-width-1-4@m';
  196. $grid_classes['sidebar']['first'][] = 'uk-push-pull-@m';
  197. $grid_classes['sidebar']['second'][] = 'uk-width-1-4@m';
  198. $grid_classes['sidebar']['second'][] = 'uk-push-pull-@m';
  199. break;
  200. case 'sidebar-left-stacked':
  201. $grid_classes['content'][] = 'uk-width-3-4@m';
  202. $grid_classes['content'][] = 'uk-push-1-4@m';
  203. $grid_classes['sidebar']['first'][] = 'uk-width-1-4@m';
  204. $grid_classes['sidebar']['first'][] = 'uk-pull-3-4@m';
  205. $grid_classes['sidebar']['second'][] = 'uk-width-1-1@m';
  206. $grid_classes['sidebar']['second'][] = 'uk-push-pull-@m';
  207. break;
  208. case 'sidebar-right-stacked':
  209. $grid_classes['content'][] = 'uk-width-3-4@m';
  210. $grid_classes['content'][] = 'uk-push-pull-@m';
  211. $grid_classes['sidebar']['first'][] = 'uk-width-1-4@m';
  212. $grid_classes['sidebar']['first'][] = 'uk-push-pull-@m';
  213. $grid_classes['sidebar']['second'][] = 'uk-width-1-1@m';
  214. $grid_classes['sidebar']['second'][] = 'uk-push-pull-@m';
  215. break;
  216. }
  217. switch ($mobile_layout) {
  218. case 'sidebars-stacked':
  219. $grid_classes['content'][] = 'uk-width-1-1@s';
  220. $grid_classes['content'][] = 'uk-width-1-1';
  221. $grid_classes['content'][] = 'uk-push-pull-@s';
  222. $grid_classes['sidebar']['first'][] = 'uk-width-1-1@s';
  223. $grid_classes['sidebar']['first'][] = 'uk-width-1-1';
  224. $grid_classes['sidebar']['first'][] = 'uk-push-pull-@s';
  225. $grid_classes['sidebar']['second'][] = 'uk-width-1-1@s';
  226. $grid_classes['sidebar']['second'][] = 'uk-width-1-1';
  227. $grid_classes['sidebar']['second'][] = 'uk-push-pull-@s';
  228. break;
  229. case 'sidebars-vertical':
  230. $grid_classes['content'][] = 'uk-width-1-1@s';
  231. $grid_classes['content'][] = 'uk-width-1-1';
  232. $grid_classes['content'][] = 'uk-push-pull-@s';
  233. $grid_classes['sidebar']['first'][] = 'uk-width-1-2@s';
  234. $grid_classes['sidebar']['first'][] = 'uk-width-1-2';
  235. $grid_classes['sidebar']['first'][] = 'uk-push-pull-@s';
  236. $grid_classes['sidebar']['second'][] = 'uk-width-1-2@s';
  237. $grid_classes['sidebar']['second'][] = 'uk-width-1-2';
  238. $grid_classes['sidebar']['second'][] = 'uk-push-pull-@s';
  239. break;
  240. }
  241. }
  242. elseif ($sidebar_first) {
  243. $grid_classes['content'][] = 'uk-width-3-4@l';
  244. $grid_classes['sidebar']['first'][] = 'uk-width-1-4@l';
  245. $grid_classes['content'][] = 'uk-width-3-4@m';
  246. $grid_classes['sidebar']['first'][] = 'uk-width-1-4@m';
  247. switch ($standard_layout) {
  248. case 'holy-grail':
  249. $grid_classes['content'][] = 'uk-push-1-4@l';
  250. $grid_classes['sidebar']['first'][] = 'uk-pull-3-4@l';
  251. break;
  252. case 'sidebars-left':
  253. $grid_classes['content'][] = 'uk-push-1-4@l';
  254. $grid_classes['sidebar']['first'][] = 'uk-pull-3-4@l';
  255. break;
  256. case 'sidebars-right':
  257. $grid_classes['content'][] = 'uk-push-pull-@l';
  258. $grid_classes['sidebar']['first'][] = 'uk-push-pull-@l';
  259. break;
  260. }
  261. switch ($tablet_layout) {
  262. case 'holy-grail':
  263. $grid_classes['content'][] = 'uk-push-1-4@m';
  264. $grid_classes['sidebar']['first'][] = 'uk-pull-3-4@m';
  265. break;
  266. case 'sidebars-left':
  267. $grid_classes['content'][] = 'uk-push-1-4@m';
  268. $grid_classes['sidebar']['first'][] = 'uk-pull-3-4@m';
  269. break;
  270. case 'sidebars-right':
  271. $grid_classes['content'][] = 'uk-push-pull-@m';
  272. $grid_classes['sidebar']['first'][] = 'uk-push-pull-@m';
  273. break;
  274. case 'sidebar-left-stacked':
  275. $grid_classes['content'][] = 'uk-push-1-4@m';
  276. $grid_classes['sidebar']['first'][] = 'uk-pull-3-4@m';
  277. break;
  278. case 'sidebar-right-stacked':
  279. $grid_classes['content'][] = 'uk-push-pull-@m';
  280. $grid_classes['sidebar']['first'][] = 'uk-push-pull-@m';
  281. break;
  282. }
  283. }
  284. elseif ($sidebar_second) {
  285. $grid_classes['content'][] = 'uk-width-3-4@l';
  286. $grid_classes['sidebar']['second'][] = 'uk-width-1-4@l';
  287. switch ($standard_layout) {
  288. case 'holy-grail':
  289. $grid_classes['content'][] = 'uk-push-pull-@l';
  290. $grid_classes['sidebar']['second'][] = 'uk-push-pull-@l';
  291. break;
  292. case 'sidebars-left':
  293. $grid_classes['content'][] = 'uk-push-1-4@l';
  294. $grid_classes['sidebar']['second'][] = 'uk-pull-3-4@l';
  295. break;
  296. case 'sidebars-right':
  297. $grid_classes['content'][] = 'uk-push-pull-@l';
  298. $grid_classes['sidebar']['second'][] = 'uk-push-pull-@l';
  299. break;
  300. }
  301. switch ($tablet_layout) {
  302. case 'holy-grail':
  303. $grid_classes['content'][] = 'uk-width-3-4@m';
  304. $grid_classes['sidebar']['second'][] = 'uk-width-1-4@m';
  305. $grid_classes['content'][] = 'uk-push-pull-@m';
  306. $grid_classes['sidebar']['first'][] = 'uk-push-pull-@m';
  307. break;
  308. case 'sidebars-left':
  309. $grid_classes['content'][] = 'uk-width-3-4@m';
  310. $grid_classes['sidebar']['second'][] = 'uk-width-1-4@m';
  311. $grid_classes['content'][] = 'uk-push-1-4@m';
  312. $grid_classes['sidebar']['second'][] = 'uk-pull-3-4@m';
  313. break;
  314. case 'sidebars-right':
  315. $grid_classes['content'][] = 'uk-width-3-4@m';
  316. $grid_classes['sidebar']['second'][] = 'uk-width-1-4@m';
  317. $grid_classes['content'][] = 'uk-push-pull-@m';
  318. $grid_classes['sidebar']['first'][] = 'uk-push-pull-@m';
  319. break;
  320. case 'sidebar-left-stacked':
  321. $grid_classes['content'][] = 'uk-width-1-1@m';
  322. $grid_classes['sidebar']['second'][] = 'uk-width-1-1@m';
  323. $grid_classes['content'][] = 'uk-push-pull-@m';
  324. $grid_classes['sidebar']['second'][] = 'uk-push-pull-@m';
  325. break;
  326. case 'sidebar-right-stacked':
  327. $grid_classes['content'][] = 'uk-width-1-1@m';
  328. $grid_classes['sidebar']['second'][] = 'uk-width-1-1@m';
  329. $grid_classes['content'][] = 'uk-push-pull-@m';
  330. $grid_classes['sidebar']['second'][] = 'uk-push-pull-@m';
  331. break;
  332. }
  333. }
  334. else {
  335. $grid_classes['content'][] = 'uk-width-1-1';
  336. }
  337. return $grid_classes;
  338. }
  339. /**
  340. * Retrieves the current page title.
  341. *
  342. * @return string
  343. * The current page title.
  344. */
  345. public static function getPageTitle() {
  346. $request = \Drupal::request();
  347. $route_match = \Drupal::routeMatch();
  348. return \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
  349. }
  350. }

Classes

Name Description
UIkitProvides helper functions for the UIkit base theme.

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