styleguide.module 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <?php
  2. // $Id: styleguide.module,v 1.12 2010/10/23 23:28:43 agentken Exp $
  3. /**
  4. * Implements hook_menu().
  5. */
  6. function styleguide_menu() {
  7. $default = variable_get('theme_default', 'bartik');
  8. $items['admin/appearance/styleguide'] = array(
  9. 'title' => 'Style guide',
  10. 'page callback' => 'styleguide_page',
  11. 'weight' => 40,
  12. 'access arguments' => array('view style guides'),
  13. 'theme callback' => '_styleguide_page_theme',
  14. 'theme arguments' => array($default),
  15. );
  16. foreach (list_themes() as $theme) {
  17. $is_default = $theme->name == $default;
  18. $items['admin/appearance/styleguide/' . $theme->name] = array(
  19. 'title' => $theme->info['name'],
  20. 'page callback' => 'styleguide_page',
  21. 'page arguments' => array($theme->name),
  22. 'type' => $is_default ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
  23. 'access callback' => 'styleguide_access_check',
  24. 'access arguments' => array($theme),
  25. 'weight' => $is_default ? -10 : 0,
  26. 'theme callback' => '_styleguide_page_theme',
  27. 'theme arguments' => array($theme->name),
  28. );
  29. }
  30. return $items;
  31. }
  32. /**
  33. * Theme callback for the styleguide pages.
  34. */
  35. function _styleguide_page_theme($theme) {
  36. return $theme;
  37. }
  38. /**
  39. * Implements hook_theme().
  40. */
  41. function styleguide_theme($existing, $type, $theme, $path) {
  42. $themes = array(
  43. 'styleguide_header' => array(
  44. 'variables' => array('theme_info' => array()),
  45. ),
  46. 'styleguide_links' => array(
  47. 'variables' => array('items' => array()),
  48. ),
  49. 'styleguide_item' => array(
  50. 'variables' => array('key' => NULL, 'item' => array(), 'content' => NULL),
  51. ),
  52. 'styleguide_content' => array(
  53. 'variables' => array('content' => NULL),
  54. ),
  55. );
  56. foreach ($themes as $theme => $data) {
  57. $themes[$theme]['file'] = 'styleguide.theme.inc';
  58. }
  59. return $themes;
  60. }
  61. /**
  62. * Implements hook_permission().
  63. */
  64. function styleguide_permission() {
  65. $permissions = array(
  66. 'view style guides' => array(
  67. 'title' => t('View theme style guides'),
  68. )
  69. );
  70. return $permissions;
  71. }
  72. /**
  73. * Menu access callback.
  74. */
  75. function styleguide_access_check($theme) {
  76. if (!user_access('view style guides')) {
  77. return FALSE;
  78. }
  79. if (drupal_theme_access($theme)) {
  80. return TRUE;
  81. }
  82. return FALSE;
  83. }
  84. /**
  85. * The styleguide page.
  86. */
  87. function styleguide_page($theme = NULL) {
  88. drupal_add_css(drupal_get_path('module', 'styleguide') . '/styleguide.css');
  89. // TODO: notice about the Overlay module?
  90. // Check the theme.
  91. if (is_null($theme)) {
  92. $theme = variable_get('theme_default', 'bartik');
  93. }
  94. // Get theme data.
  95. $themes = list_themes();
  96. $active = $themes[$theme];
  97. // Get visual testing elements.
  98. $items = module_invoke_all('styleguide');
  99. drupal_alter('styleguide', $items);
  100. // Get theme style information.
  101. $theme_info = $active->info;
  102. drupal_alter('styleguide_theme_info', $theme_info, $theme);
  103. $groups = array();
  104. foreach ($items as $key => $item) {
  105. if (!isset($item['group'])) {
  106. $item['group'] = t('Common');
  107. }
  108. else {
  109. $item['group'] = t('@group', array('@group' => $item['group']));
  110. }
  111. $item['title'] = t('@title', array('@title' => $item['title']));
  112. $groups[$item['group']][$key] = $item;
  113. }
  114. ksort($groups);
  115. // Create a navigation header.
  116. $header = array();
  117. $head = '';
  118. $content = '';
  119. // Process the elements, by group.
  120. foreach ($groups as $group => $elements) {
  121. foreach ($elements as $key => $item) {
  122. $display = '';
  123. // Output a standard theme item.
  124. if (isset($item['theme'])) {
  125. $display = theme($item['theme'], $item['variables']);
  126. }
  127. // Output a standard HTML tag. In Drupal 7, the preference
  128. // is to pass theme('html_tag') instead. This is kept for API
  129. // compatibility with Drupal 6.
  130. elseif (isset($item['tag']) && isset($item['content'])) {
  131. if (empty($item['attributes'])) {
  132. $display = '<' . $item['tag'] . '>' . $item['content'] . '</' . $item['tag'] . '>';
  133. }
  134. else {
  135. $display = '<' . $item['tag'] . ' ' . drupal_attributes($item['attributes']) . '>' . $item['content'] . '</' . $item['tag'] . '>';
  136. }
  137. }
  138. // Support a renderable array for content.
  139. elseif (isset($item['content']) && is_array($item['content'])) {
  140. $display = drupal_render($item['content']);
  141. }
  142. // Just print the provided content.
  143. elseif (isset($item['content'])) {
  144. $display = $item['content'];
  145. }
  146. // Add the content.
  147. $content .= theme('styleguide_item', array('key' => $key, 'item' => $item, 'content' => $display));
  148. // Prepare the header link.
  149. $header[$group][] = l($item['title'], $_GET['q'], array('fragment' => $key));
  150. }
  151. $head .= theme('item_list', array('items' => $header[$group], 'title' => $group));
  152. }
  153. $output = theme('styleguide_header', array('theme_info' => $theme_info));
  154. $output .= theme('styleguide_links', array('items' => $head));
  155. $output .= theme('styleguide_content', array('content' => $content));
  156. // Return the page.
  157. return $output;
  158. }
  159. /**
  160. * Implements hook_hook_info().
  161. */
  162. function styleguide_hook_info() {
  163. $hooks['styleguide'] = array(
  164. 'group' => 'styleguide',
  165. );
  166. return $hooks;
  167. }
  168. /**
  169. * Return a simple array of words.
  170. *
  171. * @param $size
  172. * The size of the list to return.
  173. * @return
  174. * An array of words.
  175. */
  176. function styleguide_list($size = 5) {
  177. $items = array();
  178. for ($i = 0; $i < $size; $i++) {
  179. $items[] = styleguide_word();
  180. }
  181. return $items;
  182. }
  183. /**
  184. * Return a random word.
  185. */
  186. function styleguide_word($size = 1) {
  187. return styleguide_lorem(1, $size, 'lower', FALSE, FALSE);
  188. }
  189. /**
  190. * Return a random table header array.
  191. *
  192. * @param $size
  193. * The size of the list to return.
  194. * @return
  195. * An array of header elements.
  196. */
  197. function styleguide_header($size = 5) {
  198. $header = styleguide_list($size);
  199. return $header;
  200. }
  201. /**
  202. * Return a random table row array.
  203. *
  204. * @param $size
  205. * The size of the list to return.
  206. * @return
  207. * An array of row elements.
  208. */
  209. function styleguide_rows($size = 5) {
  210. $rows = array();
  211. for ($i = 0; $i < $size; $i++) {
  212. $rows[] = styleguide_list($size);
  213. }
  214. return $rows;
  215. }
  216. /**
  217. * Lorum ipsum text, used to generate words and phrases.
  218. *
  219. * @param $size
  220. * The size of the list to return.
  221. * @param $words
  222. * The number of words to return. Pass 0 for a whole paragraph.
  223. * @param $case
  224. * The case of the text. Options are 'mixed', 'upper' and 'lower'.
  225. * @param $returns
  226. * Indicates whether line returns should not be stripped out of the result.
  227. * @param $punctuation
  228. * Indicates whether punctuation should not be stripped out of the result.
  229. * @param $array
  230. * Indicates that the return value should be an array instead of a string.
  231. * @return
  232. * A string or array of content.
  233. */
  234. function styleguide_lorem($size = 5, $words = 0, $case = 'mixed', $returns = TRUE, $punctuation = TRUE, $array = FALSE) {
  235. $text = <<<EOT
  236. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam iaculis, velit gravida convallis tincidunt, felis enim venenatis lorem, nec lobortis nisl urna et mi. Pellentesque ac dictum ante. Fusce dignissim tempor elementum. Ut dignissim convallis eros, viverra luctus lacus consequat ac. Sed feugiat velit sed magna aliquam accumsan. Nam vitae porta tortor. Nam auctor dui a neque iaculis in aliquam erat viverra. Duis orci nunc, lacinia in malesuada et, euismod id turpis. Cras mattis vulputate erat, eget tempor magna egestas eu. Vestibulum sit amet massa est.
  237. Vivamus pretium placerat lorem, in tempor massa convallis sit amet. Aliquam sed quam eget ligula luctus aliquam sed vitae nulla. Aliquam dui dolor, ullamcorper eget rutrum ut, hendrerit ac lorem. Donec magna est, sollicitudin vel ultrices vel, mattis ut odio. Integer vel felis laoreet purus sollicitudin varius sed id ipsum. Suspendisse potenti. Praesent ut justo vitae metus luctus vehicula a et purus. Suspendisse potenti. Sed viverra, quam non hendrerit laoreet, massa odio blandit arcu, ac molestie metus diam eu tortor. Donec erat arcu, ultrices sit amet placerat non, feugiat in arcu. Mauris eros quam, varius eget volutpat vel, tristique sed est. In faucibus feugiat urna sit amet elementum. Integer consequat rhoncus libero, in molestie augue posuere et. Phasellus ac eleifend magna. Proin vulputate dui ac justo pharetra consequat. In vel iaculis ligula.
  238. Cras vestibulum lacus sit amet sem commodo ullamcorper aliquet eros vestibulum. Sed fermentum nulla quis risus suscipit dapibus. Sed vitae velit ut dolor varius semper at id lectus. Aenean quis leo sit amet tellus tempus cursus. Vivamus semper vehicula ante eget semper. In ac ipsum erat. Suspendisse lectus erat, commodo nec fringilla quis, interdum non leo. Vivamus et lectus vitae risus porta sollicitudin luctus eget est. Etiam quis elit vel est suscipit tristique. Nullam fringilla purus ac velit gravida ullamcorper. Praesent porttitor ante non lacus suscipit porta. Nunc fermentum sem et metus aliquam ultricies non sollicitudin nibh. Vestibulum ut ligula dolor, in placerat tortor. Sed nec lacus sed nibh iaculis luctus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Curabitur rutrum, diam vel tempor commodo, augue nunc viverra risus, in pellentesque neque justo eget dolor. Maecenas quis odio leo, a auctor lorem.
  239. Curabitur dapibus odio quis enim hendrerit eu placerat lorem accumsan. Phasellus sagittis, orci vel laoreet molestie, urna orci imperdiet elit, quis ultricies orci mauris vel ante. Cras pharetra, nisl a sagittis feugiat, turpis magna placerat sem, sed euismod erat elit in magna. Phasellus blandit ullamcorper diam vel porta. Vivamus mollis, metus nec tincidunt venenatis, risus odio sodales risus, vitae ultrices est nisi eget ante. Aenean eget nisi mi. Nulla non nulla nec metus rhoncus congue. Curabitur quis nunc nibh. Cras metus lorem, euismod ornare mattis sagittis, ultrices eget turpis. Integer quis dui tellus. Morbi vel dolor sit amet metus eleifend fringilla. Fusce nunc neque, ultricies et commodo semper, dignissim vitae tortor. Phasellus et ipsum quis sapien accumsan auctor. Morbi congue nulla vel tortor aliquet imperdiet. Morbi eget odio elit, et cursus odio. Quisque a velit diam. Duis urna libero, tempus non mattis a, convallis ac erat. Etiam vel dui posuere lectus auctor viverra vitae id eros. Maecenas mollis eros non elit sollicitudin quis fermentum diam lacinia. Quisque at ante nibh, a molestie ligula.
  240. Sed et enim nunc, nec vehicula sem. Sed risus orci, auctor et dictum at, hendrerit eu augue. Curabitur sed ante non quam fermentum vehicula. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam tincidunt dictum molestie. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Phasellus nec urna ut lorem tempus aliquet eget nec lectus. Phasellus quis venenatis tortor. Integer elementum, sapien at feugiat cursus, tortor sapien adipiscing massa, non molestie elit lacus vel velit. Suspendisse sit amet sem id libero auctor pharetra sit amet ut dui. Aenean sit amet tellus sit amet ante congue faucibus. Nullam hendrerit, justo et iaculis tristique, ligula risus pretium erat, sed tempus lacus felis ut nulla.
  241. EOT;
  242. if (!$punctuation) {
  243. $text = str_replace(array(',', '.'), '', $text);
  244. }
  245. switch ($case) {
  246. case 'mixed':
  247. break;
  248. case 'upper':
  249. $text = strtoupper($text);
  250. break;
  251. case 'lower':
  252. $text = strtolower($text);
  253. break;
  254. }
  255. $graphs = explode("\n\n", $text);
  256. $text = array_slice($graphs, 0, $size);
  257. $spacer = ' ';
  258. if ($returns) {
  259. $spacer = "\n\n";
  260. }
  261. if ($words > 0) {
  262. $elements = explode(' ', implode(' ', $text));
  263. $output = array();
  264. for ($i = 0; $i < $words; $i++) {
  265. $val = array_rand($elements);
  266. $output[] = $elements[$val];
  267. }
  268. return implode(' ', $output);
  269. }
  270. if (!$array) {
  271. return implode($spacer, $text);
  272. }
  273. return $text;
  274. }
  275. /**
  276. * Generate paragraph(s) of random text.
  277. *
  278. * @param $size
  279. * The number of paragraphs to return.
  280. * @return
  281. * HTML paragraphs.
  282. */
  283. function styleguide_paragraph($size = 5) {
  284. $text = styleguide_lorem($size, 0, 'mixed', TRUE, TRUE, TRUE);
  285. $output = '';
  286. foreach ($text as $item) {
  287. $output .= '<p>' . trim($item) . '</p>';
  288. }
  289. return $output;
  290. }
  291. /**
  292. * Provide a default image for display.
  293. *
  294. * Images should be in the assets directory. The current images are
  295. * (c) Ken Rickard and used by permission.
  296. *
  297. * @param $image
  298. * The name of the image. Will be prefixed with 'image-'.
  299. * @param $type
  300. * The file type, (jpg, png, gif). Do not include a dot.
  301. * @return
  302. * The Drupal path to the file.
  303. */
  304. function styleguide_image($image = 'vertical', $type = 'jpg') {
  305. $path = drupal_get_path('module', 'styleguide');
  306. $filepath = $path . '/assets/image-' . $image . '.' . $type;
  307. if (file_exists($filepath)) {
  308. return $filepath;
  309. }
  310. }
  311. /**
  312. * Implements hook_system_theme_page_alter().
  313. */
  314. function styleguide_system_themes_page_alter(&$theme_groups) {
  315. if (!user_access('view style guides')) {
  316. return;
  317. }
  318. foreach ($theme_groups as $group => $members) {
  319. if (empty($group)) {
  320. continue;
  321. }
  322. foreach ($members as $key => $theme) {
  323. if (!styleguide_access_check($theme)) {
  324. continue;
  325. }
  326. $theme_groups[$group][$key]->operations[] = array(
  327. 'title' => t('Style guide'),
  328. 'href' => 'admin/appearance/styleguide/' . $theme->name,
  329. 'attributes' => array('title' => t('Style guide for @theme', array('@theme' => $theme->info['name']))),
  330. );
  331. }
  332. }
  333. }
  334. /**
  335. * Generate a random sentence.
  336. */
  337. function styleguide_sentence() {
  338. $graph = strip_tags(styleguide_paragraph());
  339. $explode = explode('.', $graph);
  340. $rand = array_rand($explode);
  341. return trim($explode[$rand]) . '.';
  342. }
  343. /**
  344. * Sample form, showing all elements.
  345. */
  346. function styleguide_form($form, &$form_state, $form_keys = array()) {
  347. $form = array();
  348. $options = array();
  349. $list = styleguide_list();
  350. foreach ($list as $item) {
  351. $options[$item] = $item;
  352. }
  353. $form['select'] = array(
  354. '#type' => 'select',
  355. '#title' => t('Select'),
  356. '#options' => $options,
  357. '#description' => styleguide_sentence(),
  358. );
  359. $form['checkbox'] = array(
  360. '#type' => 'checkbox',
  361. '#title' => t('Checkbox'),
  362. '#value' => 1,
  363. '#default_value' => 1,
  364. '#description' => styleguide_sentence(),
  365. );
  366. $form['checkboxes'] = array(
  367. '#type' => 'checkboxes',
  368. '#title' => t('Checkboxes'),
  369. '#options' => $options,
  370. '#description' => styleguide_sentence(),
  371. );
  372. $form['radios'] = array(
  373. '#type' => 'radios',
  374. '#title' => t('Radios'),
  375. '#options' => $options,
  376. '#description' => styleguide_sentence(),
  377. );
  378. $form['textfield'] = array(
  379. '#type' => 'textfield',
  380. '#title' => t('Textfield'),
  381. '#default_value' => styleguide_word(),
  382. '#description' => styleguide_sentence(),
  383. );
  384. $form['autocomplete'] = array(
  385. '#type' => 'textfield',
  386. '#title' => t('Autocomplete textfield'),
  387. '#default_value' => styleguide_word(),
  388. '#description' => styleguide_sentence(),
  389. '#autocomplete_path' => 'user/autocomplete',
  390. );
  391. $form['textfield-machine'] = array(
  392. '#type' => 'textfield',
  393. '#title' => t('Textfield, with machine name'),
  394. '#default_value' => styleguide_word() . ' ' . styleguide_word() . ' ' . styleguide_word(),
  395. '#description' => styleguide_sentence(),
  396. );
  397. $form['machine_name'] = array(
  398. '#type' => 'machine_name',
  399. '#title' => t('Machine name'),
  400. '#machine_name' => array(
  401. 'source' => array('textfield-machine'),
  402. ),
  403. '#description' => styleguide_sentence(),
  404. );
  405. $form['textarea'] = array(
  406. '#type' => 'textarea',
  407. '#title' => t('Textarea'),
  408. '#default_value' => styleguide_paragraph(),
  409. '#description' => styleguide_sentence(),
  410. );
  411. $form['date'] = array(
  412. '#type' => 'date',
  413. '#title' => t('Date'),
  414. '#description' => styleguide_sentence(),
  415. );
  416. $form['file'] = array(
  417. '#type' => 'file',
  418. '#title' => t('File'),
  419. '#description' => styleguide_sentence(),
  420. );
  421. $form['managed_file'] = array(
  422. '#type' => 'managed_file',
  423. '#title' => t('Managed file'),
  424. '#description' => styleguide_sentence(),
  425. );
  426. $form['password'] = array(
  427. '#type' => 'password',
  428. '#title' => t('Password'),
  429. '#default_value' => styleguide_word(),
  430. '#description' => styleguide_sentence(),
  431. );
  432. $form['password_confirm'] = array(
  433. '#type' => 'password_confirm',
  434. '#title' => t('Password confirm'),
  435. );
  436. $form['weight'] = array(
  437. '#type' => 'weight',
  438. '#title' => t('Weight'),
  439. '#delta' => 10,
  440. '#description' => styleguide_sentence(),
  441. );
  442. $form['fieldset-collapsed'] = array(
  443. '#type' => 'fieldset',
  444. '#title' => t('Fieldset collapsed'),
  445. '#collapsible' => TRUE,
  446. '#collapsed' => TRUE,
  447. '#description' => styleguide_sentence(),
  448. );
  449. $form['fieldset-collapsible'] = array(
  450. '#type' => 'fieldset',
  451. '#title' => t('Fieldset collapsible'),
  452. '#collapsible' => TRUE,
  453. '#description' => styleguide_sentence(),
  454. );
  455. $form['fieldset'] = array(
  456. '#type' => 'fieldset',
  457. '#title' => t('Fieldset'),
  458. '#collapsible' => FALSE,
  459. '#description' => styleguide_sentence(),
  460. );
  461. $fieldsets = array('fieldset', 'fieldset-collapsed', 'fieldset-collapsible');
  462. $count = 0;
  463. foreach ($form as $key => $value) {
  464. if ($value['#type'] != 'fieldset' && $value['#type'] != 'checkbox' && $count < 2) {
  465. $count++;
  466. foreach ($fieldsets as $item) {
  467. $form[$item][$key . '-' . $item] = $value;
  468. }
  469. }
  470. }
  471. $form['vertical_tabs'] = array(
  472. '#type' => 'vertical_tabs',
  473. );
  474. foreach ($fieldsets as $fieldset) {
  475. $form['vertical_tabs'][$fieldset] = $form[$fieldset];
  476. }
  477. $form['markup'] = array(
  478. '#markup' => t('<p><em>Markup</em>: Note that markup does not allow titles or descriptions. Use "item" for those options.</p>') . styleguide_paragraph(1),
  479. );
  480. $form['item'] = array(
  481. '#type' => 'item',
  482. '#title' => t('Item'),
  483. '#markup' => styleguide_paragraph(1),
  484. '#description' => styleguide_sentence(),
  485. );
  486. $form['image_button'] = array(
  487. '#type' => 'image_button',
  488. '#src' => 'misc/druplicon.png',
  489. '#attributes' => array('height' => 20),
  490. '#name' => t('Image button'),
  491. );
  492. $form['submit'] = array(
  493. '#type' => 'submit',
  494. '#value' => t('Submit'),
  495. );
  496. $form['button'] = array(
  497. '#type' => 'button',
  498. '#value' => t('Button'),
  499. );
  500. if (!empty($form_keys)) {
  501. $items = array();
  502. foreach ($form_keys as $key) {
  503. if (isset($form[$key])) {
  504. $items[$key] = $form[$key];
  505. }
  506. }
  507. return $items;
  508. }
  509. return $form;
  510. }