template.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. <?php
  2. /**
  3. * Include all necessary files.
  4. */
  5. include_once __DIR__ . '/templates/system/page.vars.php';
  6. /**
  7. * Implements hook_menu_alter().
  8. *
  9. * @param $items
  10. */
  11. function hardwood_menu_alter(&$items) {
  12. if (isset($items['user/%/galaxy-jobs'])) {
  13. $items['user/%/galaxy-jobs']['title'] = t('My Galaxy Workflows');
  14. }
  15. if (isset($items['user/%/data-collections'])) {
  16. $items['user/%/data-collections']['title'] = t('My Data Collections');
  17. }
  18. if (isset($items['user/%/files'])) {
  19. $items['user/%/files']['title'] = t('My Files');
  20. }
  21. }
  22. /**
  23. * Add `btn` class to all buttons.
  24. *
  25. * @param $variables
  26. */
  27. function hardwood_preprocess_button(&$variables) {
  28. $variables['element']['#attributes']['class'][] = 'btn';
  29. $variables['element']['#attributes']['class'][] = 'mb-2';
  30. if (is_array($variables['element']['#attributes']['class'])) {
  31. if (in_array('btn-default',
  32. $variables['element']['#attributes']['class']) || in_array('btn-danger',
  33. $variables['element']['#attributes']['class']) || in_array('btn-warning',
  34. $variables['element']['#attributes']['class']) || in_array('btn-info',
  35. $variables['element']['#attributes']['class'])) {
  36. return;
  37. }
  38. }
  39. // Special styles for Delete/Destructive Buttons.
  40. if (stristr($variables['element']['#value'], 'Delete') !== FALSE) {
  41. $variables['element']['#attributes']['class'][] = 'btn-danger';
  42. }
  43. else {
  44. $variables['element']['#attributes']['class'][] = 'btn-primary';
  45. }
  46. }
  47. /**
  48. * Add class `form-control` to text fields
  49. *
  50. * @param $variables
  51. */
  52. function hardwood_preprocess_textfield(&$variables) {
  53. $variables['element']['#attributes']['class'][] = 'form-control';
  54. }
  55. /**
  56. * Add class `form-control` to file fields
  57. *
  58. * @param $variables
  59. */
  60. function hardwood_preprocess_file(&$variables) {
  61. $variables['element']['#attributes']['class'][] = 'form-control';
  62. }
  63. /**
  64. * Implements theme_textfield()
  65. *
  66. * The only reason we need this is to remove the default
  67. * `form-text` class that drupal adds to text fields.
  68. *
  69. * @param $variables
  70. *
  71. * @return string
  72. */
  73. function hardwood_textfield($variables) {
  74. $element = $variables['element'];
  75. $element['#attributes']['type'] = 'text';
  76. element_set_attributes($element, [
  77. 'id',
  78. 'name',
  79. 'value',
  80. 'size',
  81. 'maxlength',
  82. ]);
  83. //_form_set_class($element, array('form-text'));
  84. $extra = '';
  85. if ($element['#autocomplete_path'] && !empty($element['#autocomplete_input'])) {
  86. drupal_add_library('system', 'drupal.autocomplete');
  87. $element['#attributes']['class'][] = 'form-autocomplete';
  88. $attributes = [];
  89. $attributes['type'] = 'hidden';
  90. $attributes['id'] = $element['#autocomplete_input']['#id'];
  91. $attributes['value'] = $element['#autocomplete_input']['#url_value'];
  92. $attributes['disabled'] = 'disabled';
  93. $attributes['class'][] = 'autocomplete';
  94. $extra = '<input' . drupal_attributes($attributes) . ' />';
  95. }
  96. $output = '<input' . drupal_attributes($element['#attributes']) . ' />';
  97. return $output . $extra;
  98. }
  99. /**
  100. * Implements theme_textarea()
  101. *
  102. * Add .form-control to textarea fields
  103. *
  104. * @param $variables
  105. *
  106. * @return string
  107. */
  108. function hardwood_textarea($variables) {
  109. $element = $variables['element'];
  110. element_set_attributes($element, ['id', 'name']);
  111. _form_set_class($element, ['form-control']);
  112. $wrapper_attributes = [
  113. 'class' => ['form-textarea-wrapper'],
  114. ];
  115. // Add resizable behavior.
  116. if (!empty($element['#resizable'])) {
  117. drupal_add_library('system', 'drupal.textarea');
  118. $wrapper_attributes['class'][] = 'resizable';
  119. }
  120. $output = '<div' . drupal_attributes($wrapper_attributes) . '>';
  121. $output .= '<textarea' . drupal_attributes($element['#attributes']) . '>' . check_plain($element['#value']) . '</textarea>';
  122. $output .= '</div>';
  123. return $output;
  124. }
  125. /**
  126. * Implements theme_password()
  127. *
  128. * Add .form-control to password fields
  129. *
  130. * @param $variables
  131. *
  132. * @return string
  133. */
  134. function hardwood_password($variables) {
  135. $element = $variables['element'];
  136. $element['#attributes']['type'] = 'password';
  137. element_set_attributes($element, ['id', 'name', 'size', 'maxlength']);
  138. _form_set_class($element, ['form-control']);
  139. return '<input' . drupal_attributes($element['#attributes']) . ' />';
  140. }
  141. /**
  142. * Implements theme_form_search_block_form_alter()
  143. *
  144. * Alter the search form.
  145. *
  146. * @param $form
  147. */
  148. function hardwood_form_search_block_form_alter(&$form) {
  149. $form['search_block_form']['#attributes']['placeholder'] = "Search...";
  150. $form['search_block_form']['#field_prefix'] = FALSE;
  151. $form['search_block_form']['#field_suffix'] = FALSE;
  152. }
  153. /**
  154. * Implements theme_form_element().
  155. *
  156. * Apply bootstrap wrappings and classes on all form elements.
  157. *
  158. * @param $variables
  159. *
  160. * @return string
  161. */
  162. function hardwood_form_element($variables) {
  163. $element = &$variables['element'];
  164. // This function is invoked as theme wrapper, but the rendered form element
  165. // may not necessarily have been processed by form_builder().
  166. //$element['#title_display'] = 'before';
  167. // Add a class for disabled elements to facilitate cross-browser styling.
  168. if (!empty($element['#attributes']['disabled'])) {
  169. $attributes['class'][] = 'disabled';
  170. }
  171. // Add element #id for #type 'item'.
  172. if (isset($element['#markup']) && !empty($element['#id'])) {
  173. $attributes['id'] = $element ['#id'];
  174. }
  175. // Add element's #type and #name as class to aid with JS/CSS selectors.
  176. $attributes['class'] = ['form-item'];
  177. if (!empty($element['#type'])) {
  178. $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
  179. }
  180. if (!empty($element['#name'])) {
  181. $attributes['class'][] = 'form-item-' . strtr($element['#name'], [
  182. ' ' => '-',
  183. '_' => '-',
  184. '[' => '-',
  185. ']' => '',
  186. ]);
  187. }
  188. $output = '<div' . drupal_attributes($attributes) . '>' . "\n";
  189. // If #title is not set, we don't display any label or required marker.
  190. if (!isset($element['#title'])) {
  191. $element['#title_display'] = 'none';
  192. }
  193. if (!isset($element['#title_display'])) {
  194. $element['#title_display'] = 'before';
  195. }
  196. $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . $element['#field_prefix'] . '</span>' : '';
  197. $suffix = isset($element['#field_suffix']) ? '<span class="field-prefix">' . $element['#field_suffix'] . '</span>' : '';
  198. switch ($element['#title_display']) {
  199. case 'before':
  200. case 'invisible':
  201. $output .= ' ' . theme('form_element_label', $variables);
  202. if (!empty($element['#description']) && isset($element['#type']) && in_array($element['#type'],
  203. [
  204. 'radios',
  205. 'checkboxes',
  206. ])) {
  207. $output .= '<div class="form-text text-muted mb-2">' . $element['#description'] . "</div>\n";
  208. }
  209. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  210. break;
  211. case 'after':
  212. $output .= ' ' . $prefix . $element['#children'] . $suffix;
  213. $output .= ' ' . theme('form_element_label', $variables) . "\n";
  214. if (!empty($element['#description']) && isset($element['#type']) && in_array($element['#type'],
  215. [
  216. 'radios',
  217. 'checkboxes',
  218. ])) {
  219. $output .= '<div class="form-text text-muted mb-2">' . $element['#description'] . "</div>\n";
  220. }
  221. break;
  222. case 'none':
  223. case 'attribute':
  224. // Output no label and no required marker, only the children.
  225. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  226. break;
  227. }
  228. if (!empty($element['#description']) && (!isset($element['#type']) || !in_array($element['#type'],
  229. [
  230. 'radios',
  231. 'checkboxes',
  232. ]))) {
  233. $output .= '<div class="form-text text-muted">' . $element['#description'] . "</div>\n";
  234. }
  235. $output .= "</div>\n";
  236. return '<div class="form-group">' . $output . '</div>';
  237. }
  238. /**
  239. * Implements theme_button()
  240. *
  241. * @param $variables
  242. *
  243. * @return string
  244. */
  245. function hardwood_button($variables) {
  246. $element = $variables['element'];
  247. $element['#attributes']['type'] = 'submit';
  248. element_set_attributes($element, ['id', 'name', 'value']);
  249. $element ['#attributes']['class'][] = 'form-' . $element ['#button_type'];
  250. if (!empty($element['#attributes']['disabled'])) {
  251. $element['#attributes']['class'][] = 'disabled';
  252. }
  253. return '<input' . drupal_attributes($element['#attributes']) . ' />';
  254. }
  255. /**
  256. * Alter status messages to use bootstrap alerts.
  257. *
  258. * @param $variables
  259. *
  260. * @return string
  261. */
  262. function hardwood_status_messages($variables) {
  263. $display = $variables['display'];
  264. $output = '';
  265. $status_heading = [
  266. 'status' => t('Status message'),
  267. 'error' => t('Error message'),
  268. 'warning' => t('Warning message'),
  269. ];
  270. foreach (drupal_get_messages($display) as $type => $messages) {
  271. switch ($type) {
  272. case "error":
  273. $cc = 'alert-danger';
  274. break;
  275. case "warning":
  276. $cc = 'alert-warning';
  277. break;
  278. case 'status':
  279. $cc = 'alert-info';
  280. break;
  281. default:
  282. $cc = 'alert-info';
  283. break;
  284. }
  285. $output .= "<div class=\"alert $cc\">\n";
  286. if (!empty($status_heading[$type])) {
  287. $output .= '<h2 class="element-invisible">' . $status_heading[$type] . "</h2>\n";
  288. }
  289. if (count($messages) > 1) {
  290. $output .= " <ul>\n";
  291. foreach ($messages as $message) {
  292. $output .= ' <li>' . $message . "</li>\n";
  293. }
  294. $output .= " </ul>\n";
  295. }
  296. else {
  297. $output .= reset($messages);
  298. }
  299. $output .= "</div>\n";
  300. }
  301. return $output;
  302. }
  303. /**
  304. * Add the menu menu wrapper.
  305. *
  306. * @param $variables
  307. *
  308. * @return string
  309. */
  310. function hardwood_menu_tree__primary(array &$variables) {
  311. return '<ul class="navbar-nav ml-lg-auto mr-md-auto mr-lg-0">' . $variables['tree'] . '</ul>';
  312. }
  313. /**
  314. * Add the secondary menu wrapper.
  315. *
  316. * @param array $variables
  317. *
  318. * @return string
  319. */
  320. function hardwood_menu_tree__secondary(array &$variables) {
  321. return '<ul class="navbar-nav mr-md-auto mr-lg-0">' . $variables['tree'] . '</ul>';
  322. }
  323. /**
  324. * Returns HTML for a menu link and submenu.
  325. *
  326. * @param array $variables
  327. * An associative array containing:
  328. * - element: Structured array data for a menu link.
  329. *
  330. * @return string
  331. * The constructed HTML.
  332. *
  333. * @see theme_menu_link()
  334. *
  335. * @ingroup theme_functions
  336. */
  337. function hardwood_menu_link__main_menu(array $variables) {
  338. $element = $variables['element'];
  339. $sub_menu = '';
  340. $title = $element['#title'];
  341. $href = $element['#href'];
  342. $options = !empty($element['#localized_options']) ? $element['#localized_options'] : [];
  343. $attributes = !empty($element['#attributes']) ? $element['#attributes'] : [];
  344. $attributes['class'][] = 'nav-item';
  345. $options['attributes']['class'][] = 'nav-link';
  346. if ($element['#below']) {
  347. // Prevent dropdown functions from being added to management menu so it
  348. // does not affect the navbar module.
  349. if (($element['#original_link']['menu_name'] == 'management') && (module_exists('navbar'))) {
  350. $sub_menu = drupal_render($element['#below']);
  351. }
  352. elseif ((!empty($element['#original_link']['depth'])) && ($element['#original_link']['depth'] == 1)) {
  353. // Add our own wrapper.
  354. //$element['#below']['#theme_wrappers'] = array('menu_tree__sub_menu');
  355. $sub_menu = '<div class="dropdown-menu">' . drupal_render($element['#below']) . '</div>';
  356. // Generate as standard dropdown.
  357. $attributes['class'][] = 'dropdown';
  358. $options['html'] = TRUE;
  359. // Set dropdown trigger element to # to prevent inadvertant page loading
  360. // when a submenu link is clicked.
  361. $options['attributes']['class'][] = 'dropdown-toggle';
  362. $options['attributes']['data-toggle'] = 'dropdown';
  363. }
  364. }
  365. return '<li' . drupal_attributes($attributes) . '>' . l($title, $href,
  366. $options) . $sub_menu . "</li>\n";
  367. }
  368. /**
  369. * @param array $variables
  370. *
  371. * @return string
  372. * @see \theme_menu_link()
  373. */
  374. function hardwood_menu_link__user_menu(array &$variables) {
  375. return hardwood_menu_link__main_menu($variables);
  376. }
  377. /**
  378. * Generates dropdown menu wrapper.
  379. *
  380. * @param array $variables
  381. *
  382. * @return string
  383. */
  384. function hardwood_menu_tree_link__sub_menu(array &$variables) {
  385. return '<div class="dropdown-menu">' . $variables['tree'] . '</div>';
  386. }
  387. /**
  388. * Create themeable objects.
  389. *
  390. * Implements hook_theme()
  391. *
  392. * @param array $existing
  393. * @param string $type
  394. * @param string $theme
  395. * @param string $path
  396. *
  397. * @return array
  398. *
  399. * @see \hook_theme()
  400. */
  401. function hardwood_theme($existing, $type, $theme, $path) {
  402. return [
  403. 'survey_modal' => [
  404. 'template' => 'other/survey_modal',
  405. 'path' => $path . '/templates',
  406. ],
  407. ];
  408. }
  409. /**
  410. * Implements hook_theme_registry_alter().
  411. *
  412. * @param $theme_registry
  413. */
  414. function hardwood_theme_registry_alter(&$theme_registry) {
  415. $path = path_to_theme();
  416. // Tell the theme system to look in the "templates" subdirectory within our theme directory
  417. // Force tripal_blast to use our blast_report template
  418. $theme_registry['show_blast_report']['theme paths'] = [0 => $path . '/templates'];
  419. $theme_registry['show_blast_report']['theme path'] = $path . '/templates';
  420. $theme_registry['show_blast_report']['path'] = $path . '/templates';
  421. $theme_registry['show_blast_report']['template'] = 'blast/blast_report';
  422. $theme_registry['trpdownload_page']['path'] = $path . '/templates';
  423. $theme_registry['trpdownload_page']['template'] = 'other/generic_download_page';
  424. $theme_registry['analysis_expression_plot']['path'] = $path . '/templates';
  425. $theme_registry['analysis_expression_plot']['template'] = 'tripal/tripal_analysis_expression.analysis';
  426. foreach ($theme_registry as $key => $theme) {
  427. if (isset($theme['template']) && strpos($theme['template'],
  428. 'node--chado-generic') !== FALSE) {
  429. $theme_registry[$key]['path'] = $path . '/templates';
  430. $theme_registry[$key]['template'] = 'tripal/node--chado-generic';
  431. }
  432. }
  433. }
  434. /**
  435. * @param $form
  436. * @param $form_state
  437. */
  438. function hardwood_form_website_search_box_form_alter(&$form, &$form_state) {
  439. hardwood_form_tripal_elasticsearch_site_wide_search_form_alter($form, $form_state);
  440. }
  441. /**
  442. * Alter the search form to use Bootstrap fields.
  443. *
  444. * @param $form
  445. * @param $form_state
  446. */
  447. function hardwood_form_tripal_elasticsearch_site_wide_search_form_alter(&$form, &$form_state) {
  448. $input_group_classes = "input-group elasticsearch-search-input";
  449. $height = 38;
  450. if (drupal_is_front_page()) {
  451. $input_group_classes .= ' input-group-lg';
  452. $height = 51;
  453. }
  454. $form['container']['#attributes']['class'][] = $input_group_classes;
  455. $form['container']['search_box']['#theme_wrappers'] = [];
  456. unset($form['container']['search_box']['#size']);
  457. $form['container']['search_box']['#attributes']['placeholder'] = 'Site Wide Search';
  458. $form['container']['search_box']['#attributes']['style'] = "height: {$height}px;";
  459. $form['container']['submit']['#prefix'] = '<div class="input-group-btn">';
  460. $form['container']['submit']['#suffix'] = '</div>';
  461. }
  462. /**
  463. * Add `form-control` class to the category <select> element in the contact
  464. * page.
  465. *
  466. * @param $form
  467. * @param $form_state
  468. */
  469. function hardwood_form_contact_site_form_alter(&$form, &$form_state) {
  470. $form['cid']['#attributes']['class'][] = 'form-control';
  471. }
  472. /**
  473. * Alter or add JS files to pages.
  474. *
  475. * @param $javascript
  476. */
  477. function hardwood_js_alter(&$javascript) {
  478. unset($javascript['misc/collapse.js']);
  479. drupal_add_js(drupal_get_path('theme', 'hardwood') . '/dist/js/collapse.js',
  480. []);
  481. }
  482. /**
  483. * Implements theme_progress_bar()
  484. *
  485. * Changes the progress bar markup to match BS4
  486. *
  487. * @param $variables
  488. *
  489. * @return string
  490. */
  491. function hardwood_progress_bar($variables) {
  492. $output = "<div class=\"progress\">";
  493. $output .= "<div class=\"progress-bar\" role=\"progressbar\" style=\"width:{$variables['percent']}%\" aria-valuenow=\"{$variables['percent']}\" aria-valuemin=\"0\" aria-valuemax=\"100\">{$variables['percent']}%</div>";
  494. $output .= "</div>";
  495. $output .= '<div class="message">' . $variables['message'] . '</div>';
  496. return $output;
  497. }
  498. function hardwood_form_node_form_alter(&$form, &$form_state, $form_id) {
  499. if (!isset($form['#node']->nid)) {
  500. return;
  501. }
  502. $form['theme_options'] = [
  503. '#type' => 'fieldset',
  504. '#title' => t('Theme Options'),
  505. '#access' => TRUE,
  506. '#collapsed' => TRUE,
  507. '#group' => 'additional_settings',
  508. '#tree' => TRUE,
  509. ];
  510. $form['theme_options']['display_card'] = [
  511. '#type' => 'checkbox',
  512. '#title' => t('Display box borders around page content'),
  513. '#default_value' => FALSE,
  514. ];
  515. $nid = $form['#node']->nid;
  516. $cards = variable_get('hardwood_page_cards');
  517. if (is_array($cards)) {
  518. $form['theme_options']['display_card']['#default_value'] = !isset($cards[$nid]);
  519. }
  520. $form['#submit'][] = 'hardwood_from_node_submit';
  521. }
  522. function hardwood_from_node_submit(&$form, &$form_state) {
  523. if (!isset($form['#node']->nid)) {
  524. return;
  525. }
  526. $values = $form_state['values'];
  527. $nid = $form['#node']->nid;
  528. $cards = variable_get('hardwood_page_cards');
  529. if (!$values['theme_options']['display_card']) {
  530. if (is_array($cards)) {
  531. $cards[$nid] = $nid;
  532. }
  533. else {
  534. $cards[$nid] = [$nid];
  535. }
  536. }
  537. else {
  538. if (is_array($cards)) {
  539. if (isset($cards[$nid])) {
  540. unset($cards[$nid]);
  541. }
  542. }
  543. }
  544. variable_set('hardwood_page_cards', $cards);
  545. }
  546. function hardwood_add_help_variables(&$variables) {
  547. if (function_exists('hardwoods_help_get_help_menu_items')) {
  548. $variables['help_items'] = hardwoods_help_get_help_menu_items();
  549. }
  550. }
  551. function hardwood_form_blast_ui_per_blast_program_form_alter(&$form, &$form_state) {
  552. $query_type = arg(2);//strtolower($form_state['build_info']['args'][0]);
  553. if (strstr(strtolower($query_type), 'nucleotide') === FALSE) {
  554. return;
  555. }
  556. $form['B']['DB']['#prefix'] = '<div id="blast-dbs-wrapper">';
  557. $form['B']['DB']['#suffix'] = '</div>';
  558. $options = $form['B']['DB']['SELECT_DB']['#options'];
  559. $transcripts = [];
  560. $scaffolds = [];
  561. foreach ($options as $node_id => $option) {
  562. if ($option === 'select a dataset') {
  563. continue;
  564. }
  565. $option = strtolower($option);
  566. if (strstr($option, 'transcript') !== FALSE || strstr($option, 'unigene') !== FALSE) {
  567. $transcripts[$node_id] = $option;
  568. }
  569. else {
  570. $scaffolds[$node_id] = $option;
  571. }
  572. }
  573. $form['B']['DB']['SELECT_DB']['#options'] = [
  574. 'Full Genomes/Scaffolds' => $scaffolds,
  575. 'Transcripts/Unigenes' => $transcripts,
  576. ];
  577. }