tripal_core.toc.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. <?php
  2. /**
  3. *
  4. */
  5. function tripal_core_node_toc_form($form, &$form_state, $node) {
  6. // Get info about this content type
  7. $all_types = node_type_get_types();
  8. $type_info = $all_types[$node->type];
  9. $form["#tree"] = TRUE;
  10. $form["instructions"] = [
  11. '#type' => 'fieldset',
  12. '#collapsed' => TRUE,
  13. '#collapsible' => TRUE,
  14. '#title' => 'Instructions',
  15. ];
  16. $admin_link = l(
  17. $type_info->name . " TOC administrative page",
  18. "admin/tripal/legacy/" . $type_info->module . "/" . $node->type . "toc",
  19. ['attributes' => ['target' => '_blank']]
  20. );
  21. $form["instructions"]["main"] = [
  22. '#markup' => '<p>' . t("Below is a list of the titles of
  23. content panes that can appear on this page. These titles appear in the
  24. the following order in the Table of Contents (TOC). You may rename
  25. the titles or drag and drop them to change the order. <b>Any changes will
  26. only apply to this page</b>. If you would like to make changes apply to multiple
  27. pages of the same type, please visit the $admin_link. ") . '</p>' .
  28. '<p>' . t('The list below shows all possible content panes that can appear.
  29. However, those without content are automatically hidden and do not
  30. appear in the TOC.' . '</p>'),
  31. ];
  32. $form['node'] = [
  33. '#type' => 'value',
  34. '#value' => $node,
  35. ];
  36. // Get the content array for this node, then pass it through the
  37. // tripal_core_node_view_alter which generates the TOC. After that
  38. // we can use the $build array to build the form. We have to add
  39. // a 'tripal_toc_mode' to the $node because we need to give the mode
  40. // to the tripal_core_node_view_build_toc function.
  41. $node->tripal_toc_mode = 'manage_node';
  42. node_build_content($node);
  43. $build = $node->content;
  44. $build["#node"] = $node;
  45. tripal_core_node_view_alter($build);
  46. // Iterate through the built items and add form elemetns for each one.
  47. foreach (element_children($build) as $key) {
  48. $element = $build[$key];
  49. if (array_key_exists('#tripal_toc_id', $element)) {
  50. $toc_id = $element['#tripal_toc_id'];
  51. $toc_title = $element['#tripal_toc_title'];
  52. $toc_weight = $element['#weight'];
  53. $toc_hide = $element['#hide'];
  54. // If this element is a link then we don't want to allow the user
  55. // to change the title as the link title is changed by using the
  56. // interface that created the link.
  57. $is_link = array_key_exists('#is_link', $element) ? $element['#is_link'] : FALSE;
  58. if (!$is_link) {
  59. $form['toc_items'][$toc_id]['title'] = [
  60. '#type' => 'textfield',
  61. '#default_value' => $toc_title,
  62. ];
  63. }
  64. else {
  65. $form['toc_items'][$toc_id]['title'] = [
  66. '#markup' => '<i>link title:</i> ' . $toc_title,
  67. '#value' => $toc_title,
  68. ];
  69. }
  70. $form['toc_items'][$toc_id]['hide'] = [
  71. '#type' => 'checkbox',
  72. '#default_value' => $toc_hide,
  73. ];
  74. $form['toc_items'][$toc_id]['weight'] = [
  75. '#type' => 'textfield',
  76. '#default_value' => $toc_weight,
  77. '#attributes' => [
  78. 'class' => ['tripal-node-toc-items-weights'],
  79. ],
  80. '#size' => 5,
  81. ];
  82. }
  83. }
  84. $form['toc_items']['#theme'] = 'tripal_node_toc_items_table';
  85. $form['submit'] = [
  86. '#type' => 'submit',
  87. '#name' => 'toc_submit',
  88. '#value' => t('Submit'),
  89. ];
  90. $form['unset'] = [
  91. '#type' => 'submit',
  92. '#name' => 'toc_unset',
  93. '#value' => t('Unset Node Customizations'),
  94. ];
  95. // Check to see if this node's TOC is specifically being managed.
  96. $sql = "SELECT count(*) FROM {tripal_toc} where nid = :nid";
  97. $managed_items = db_query($sql, [':nid' => $node->nid])->fetchField();
  98. if ($managed_items > 0) {
  99. $form['is_managed'] = [
  100. '#markup' => '<p><font color="red">' .
  101. t('This page currently has customiations to the TOC.</font> This means
  102. that any customzations for the content type are overriden. Click the
  103. "Unset Node Customizations" button above to remove page-level
  104. customizations and default to the content type settings.') . '</p>',
  105. ];
  106. }
  107. return $form;
  108. }
  109. /**
  110. *
  111. * @param $variables
  112. */
  113. function theme_tripal_node_toc_items_table($variables) {
  114. $elements = $variables['element'];
  115. $toc_items = [];
  116. // Sort the toc_items using a custom sort function. But we need to include
  117. // only the items we want in the table (exclude renderable stuff).
  118. foreach (element_children($elements) as $key) {
  119. $toc_items[] = $elements[$key];
  120. }
  121. usort($toc_items, 'theme_tripal_node_sort_toc_items');
  122. // Build the table header.
  123. $headers = ['Content Pane Name', 'Hide', 'Weight'];
  124. // Format the form elements as rows in the table.
  125. $rows = [];
  126. foreach ($toc_items as $key => $item) {
  127. $rows[] = [
  128. 'data' => [
  129. drupal_render($item['title']),
  130. drupal_render($item['hide']),
  131. drupal_render($item['weight']),
  132. ],
  133. 'class' => ['draggable'],
  134. ];
  135. }
  136. // Theme and return the table.
  137. $table = [
  138. 'header' => $headers,
  139. 'rows' => $rows,
  140. 'attributes' => ["id" => 'tripal-node-toc-items-table'],
  141. 'sticky' => TRUE,
  142. 'caption' => t('Content Panes Available in the TOC'),
  143. 'colgroups' => [],
  144. 'empty' => t('There are no content panes for this page'),
  145. ];
  146. drupal_add_tabledrag('tripal-node-toc-items-table', 'order', 'sibling', 'tripal-node-toc-items-weights');
  147. return theme_table($table);
  148. }
  149. /**
  150. *
  151. * @param $a
  152. * @param $b
  153. */
  154. function theme_tripal_node_sort_toc_items($a, $b) {
  155. if ($a['weight']['#value'] < $b['weight']['#value']) {
  156. return -1;
  157. }
  158. if ($a['weight']['#value'] > $b['weight']['#value']) {
  159. return 1;
  160. }
  161. if ($a['weight']['#value'] == $b['weight']['#value']) {
  162. return strcmp($a['title']['#value'], $b['title']['#value']);
  163. }
  164. }
  165. /**
  166. * Implements hook_validate for the tripal_core_node_toc_form.
  167. */
  168. function tripal_core_node_toc_form_validate($form, &$form_state) {
  169. $toc_items = $form_state['values']['toc_items'];
  170. // Iterate through the TOC items and validate.
  171. foreach ($toc_items as $toc_id => $item) {
  172. if (array_key_exists('title', $item) and !$item['title']) {
  173. form_set_error('toc_items][' . $toc_id, "Please provide a valid title.");
  174. }
  175. }
  176. }
  177. /**
  178. * Implements hook_submit for the tripal_core_node_toc_form.
  179. */
  180. function tripal_core_node_toc_form_submit($form, &$form_state) {
  181. $toc_items = $form_state['values']['toc_items'];
  182. $node = $form_state['values']['node'];
  183. if ($form_state['clicked_button']['#name'] == "toc_submit") {
  184. $transaction = db_transaction();
  185. try {
  186. // First delete any settings for this node
  187. db_delete('tripal_toc')
  188. ->condition('nid', $node->nid)
  189. ->execute();
  190. // Second add in any new settings for this node
  191. foreach ($toc_items as $toc_id => $item) {
  192. db_insert('tripal_toc')
  193. ->fields([
  194. 'node_type' => $node->type,
  195. 'key' => $toc_id,
  196. 'title' => array_key_exists('title', $item) ? $item['title'] : '',
  197. 'weight' => $item['weight'],
  198. 'nid' => $node->nid,
  199. 'hide' => $item['hide'],
  200. ])
  201. ->execute();
  202. }
  203. drupal_set_message("TOC changes successfully applied to this node only.");
  204. } catch (Exception $e) {
  205. $transaction->rollback();
  206. drupal_set_message("Failed to apply TOC changes.", "error");
  207. }
  208. }
  209. if ($form_state['clicked_button']['#name'] == "toc_unset") {
  210. $transaction = db_transaction();
  211. try {
  212. // First delete any settings for this node
  213. db_delete('tripal_toc')
  214. ->condition('nid', $node->nid)
  215. ->execute();
  216. drupal_set_message("TOC is no longer customized specifically for this page. Now using the content type settings.");
  217. } catch (Exception $e) {
  218. $transaction->rollback();
  219. drupal_set_message("Failed to apply TOC changes.", "error");
  220. }
  221. }
  222. }
  223. /**
  224. * To be called by tripal_core_node_view_alter() to generate the TOC.
  225. *
  226. * @param $build
  227. * The build array passed to hook_node_view_alter()
  228. *
  229. */
  230. function tripal_core_node_view_build_toc(&$build) {
  231. global $theme;
  232. // if this is not a full node view, we do not want to alter
  233. if ($build['#view_mode'] != 'full' OR !array_key_exists('#tripal_generic_node_template', $build)) {
  234. return;
  235. }
  236. $node_type = $build["#node"]->type;
  237. $nid = $build["#node"]->nid;
  238. // The mode alters the format of the build array. There are three types of
  239. // modes: "display", "manage_node", "manage_type". If "display" is provided
  240. // then the build array is formatted for the display of the content.
  241. // If "manage_node" is provided then the build array will contain all
  242. // content panes regardless if the pane should be hidden. This allows
  243. // the management tool to find all content panes and their settings. If
  244. // "manage_type" is provided then node-specific content panes are
  245. // excluded. Node-specific content panes are those that appear only on
  246. // specific nodes and therefore should not be used when managing the
  247. // TOC for a content type.
  248. $mode = isset($build["#node"]->tripal_toc_mode) ? $build["#node"]->tripal_toc_mode : "display";
  249. $cache = cache_get("theme_registry:$theme", 'cache');
  250. $node = $build['#node'];
  251. $toc = [];
  252. $toc_html = '';
  253. // If we are looking at a Tripal node template then we want to
  254. // make some changes to each pane of content so that we can associate
  255. // a table of contents and add administrator and curator messages.
  256. if ($build['#tripal_generic_node_template'] == TRUE) {
  257. // Iterate through all the elements of the $build array and for those
  258. // that are wanting to provide content for this node.
  259. $markup = [];
  260. foreach ($build as $key => $value) {
  261. $value = $build[$key];
  262. // Skip the body element as the Tripal node types do not use it.
  263. if ($key == 'body') {
  264. continue;
  265. }
  266. // Skip the table of contents and links as those will be placed elsewhere.
  267. if (preg_match('/^#/', $key) or $key == 'tripal_toc' or $key == 'links') {
  268. continue;
  269. }
  270. // For backwards compatibility we will handle the content type fields
  271. // named 'field_resource_blocks', 'field_resource_titles', and 'field_resource_links'
  272. // these fields can be added on the Drupal content types page and were
  273. // specifically recoginzed by Tripal v1.1. If the mode type is "manage_type"
  274. // then remove these content panes because they are node specific.
  275. if ($mode == "manage_type" and (
  276. $key == "field_resource_links" or
  277. $key == "field_resource_titles" or
  278. $key == "field_resource_blocks")) {
  279. unset($build[$key]);
  280. continue;
  281. }
  282. if ($key == "field_resource_links") {
  283. // links should just appear on the sidebar as is and not open up a panel
  284. foreach (element_children($build[$key]) as $index) {
  285. $element = $build[$key][$index];
  286. $weight = 0;
  287. $hide = 0;
  288. $toc_item_id = "resource-link-$index";
  289. // Get any overrides for this key.
  290. $overrides = tripal_core_get_toc_overrides($nid, $toc_item_id, $node_type, $mode);
  291. $weight = $overrides['weight'] ? $overrides['weight'] : $weight;
  292. $hide = $overrides['hide'] ? $overrides['hide'] : $hide;
  293. // If the element should be hidden then unset this key the build
  294. // array continue to the next one
  295. if ($mode == "display" and $overrides['hide'] == 1) {
  296. continue;
  297. }
  298. // This field supports tokens, so we need to perform the substitutions
  299. // if one is needed. Get the tokens and format
  300. $base_table = preg_replace('/^chado_(.*)$/', '\1', $node_type);
  301. $tokens = chado_node_generate_tokens($base_table);
  302. $markup = $element['#markup'];
  303. // Determine which tokens were used in the format string
  304. if (preg_match_all('/\[[^]]+\]/', $markup, $used_tokens)) {
  305. // Get the value for each token used
  306. foreach ($used_tokens[0] as $token) {
  307. $token_info = $tokens[$token];
  308. if (!empty($token_info)) {
  309. $value = chado_get_token_value($token_info, $node);
  310. $markup = str_replace($token, $value, $markup);
  311. }
  312. }
  313. $element['#markup'] = $markup;
  314. }
  315. // Add the link to the TOC
  316. $parts = explode("|", $element['#markup']);
  317. if (count($parts) == 2) {
  318. $toc[$weight][$parts[0]] = "<div id=\"$toc_item_id\" class=\"tripal_toc_list_item\">" . l($parts[0], $parts[1], ['attributes' => ['target' => '_blank']]) . "</div>";
  319. }
  320. else {
  321. $toc[$weight][$parts[0]] = "<div id=\"$toc_item_id\" class=\"tripal_toc_list_item\">" . $element['#markup'] . "</div>";
  322. }
  323. // Add to the build array but do not add markup. This way
  324. // when the TOC is managed by the node 'TOC' menu these links can
  325. // be ordered as well.
  326. $build[$toc_item_id]['#toc_handled'] = TRUE;
  327. $build[$toc_item_id]['#tripal_toc_id'] = $toc_item_id;
  328. $build[$toc_item_id]['#tripal_toc_title'] = $parts[0];
  329. $build[$toc_item_id]['#weight'] = $weight;
  330. $build[$toc_item_id]['#hide'] = $hide;
  331. $build[$toc_item_id]['#is_link'] = TRUE;
  332. }
  333. // Remove the orilink from the build array as we've moved it to
  334. // appear in the TOC
  335. unset($build[$key]);
  336. continue;
  337. }
  338. if ($key == "field_resource_titles") {
  339. // ignore these, we will use them in the field_resource_blocks if
  340. // statement below
  341. continue;
  342. }
  343. if ($key == "field_resource_blocks") {
  344. foreach (element_children($build[$key]) as $index) {
  345. // get the details and the title
  346. $weight = 0;
  347. $hide = 0;
  348. $markup = $build[$key][$index]["#markup"];
  349. $toc_item_id = "resource-$index";
  350. // Get any overrides for this key.
  351. $overrides = tripal_core_get_toc_overrides($nid, $toc_item_id, $node_type, $mode);
  352. // If the element should be hidden then unset this key the build
  353. // array continue to the next one
  354. if ($mode == "display" and $overrides['hide'] == 1) {
  355. continue;
  356. }
  357. $toc_item_title = $build["field_resource_titles"][$index]["#markup"];
  358. $toc_item_title = $overrides['title'] ? $overrides['title'] : $toc_item_title;
  359. $weight = $overrides['weight'] ? $overrides['weight'] : $weight;
  360. $hide = $overrides['hide'] ? $overrides['hide'] : $hide;
  361. $updated_markup = "
  362. <div id=\"$toc_item_id-tripal-data-pane\" class=\"tripal-data-pane\">
  363. <div class=\"$toc_item_id-tripal-data-pane-title tripal-data-pane-title\">$toc_item_title</div>
  364. $markup
  365. </div>
  366. </div>
  367. ";
  368. $build[$toc_item_id]['#markup'] = $updated_markup;
  369. $build[$toc_item_id]['#toc_handled'] = TRUE;
  370. $build[$toc_item_id]['#tripal_toc_id'] = $toc_item_id;
  371. $build[$toc_item_id]['#tripal_toc_title'] = $toc_item_title;
  372. $build[$toc_item_id]['#weight'] = $weight;
  373. $build[$toc_item_id]['#hide'] = $hide;
  374. // add the entry to the TOC
  375. $toc_item_link = "
  376. <div class=\"tripal_toc_list_item\">
  377. <a id=\"$toc_item_id\" class=\"tripal_toc_list_item_link\" href=\"?pane=$toc_item_id\">$toc_item_title</a>
  378. </div>
  379. ";
  380. $toc[$weight][$toc_item_title] = $toc_item_link;
  381. }
  382. // Remove the key from the build array. We have have replaced it
  383. unset($build[$key]);
  384. unset($build["field_resource_titles"]);
  385. continue;
  386. } // end if ($mode != "manage_type" and $key == "field_resource_blocks") {
  387. // Skip any keys we may have already handled. This is the case for
  388. // the field_resource_blocks where we removed the old CCK fields
  389. // and added new ones. We don't want these new ones to be processed
  390. // again by the code below.
  391. if (array_key_exists('#toc_handled', $build[$key]) and $build[$key]['#toc_handled'] == TRUE) {
  392. continue;
  393. }
  394. // For all other fields we will handle in the following way.
  395. //-----------------------
  396. // INITIALIZE THE CONTENT VARIABLES
  397. //-----------------------
  398. $toc_item_title = $key;
  399. $toc_item_id = $key;
  400. $toc_item_link = '';
  401. $weight = 0;
  402. $hide = 0;
  403. // get the title for the table of contents. Tripal templates should
  404. // have a '#tripal_toc_title' element in the build array
  405. if (array_key_exists('#tripal_toc_title', $build[$key])) {
  406. $toc_item_title = $build[$key]['#tripal_toc_title'];
  407. }
  408. // other elements in the $build array may just have a '#title' element,
  409. if (array_key_exists('#title', $build[$key])) {
  410. $toc_item_title = $build[$key]['#title'];
  411. }
  412. $toc_item_title = ucwords($toc_item_title);
  413. if (array_key_exists('#weight', $build[$key])) {
  414. $weight = $build[$key]['#weight'];
  415. }
  416. if (array_key_exists('#tripal_toc_id', $build[$key])) {
  417. $toc_item_id = $build[$key]['#tripal_toc_id'];
  418. }
  419. // Get any overrides for this key.
  420. $overrides = tripal_core_get_toc_overrides($nid, $toc_item_id, $node_type, $mode);
  421. // If the element should be hidden then unset this key the build
  422. // array continue to the next one
  423. if ($mode == "display" and $overrides['hide'] == 1) {
  424. unset($build[$key]);
  425. continue;
  426. }
  427. // now override the title, weight, hidden values if a value is set in the tripal_toc table
  428. $toc_item_title = $overrides['title'] ? $overrides['title'] : $toc_item_title;
  429. $weight = $overrides['weight'] ? $overrides['weight'] : $weight;
  430. $hide = $overrides['hide'] ? $overrides['hide'] : $hide;
  431. $toc_item_link = "<div class=\"tripal_toc_list_item\"><a id=\"$toc_item_id\" class=\"tripal_toc_list_item_link\" href=\"?pane=$toc_item_id\">$toc_item_title</a></div>";
  432. //-----------------------
  433. // GET THE MARKUP FOR EACH ELEMENT
  434. //-----------------------
  435. $markup = '';
  436. // find the markup. Some fields will have a '#markup' and others, such
  437. // as CCK elements may have a set of '#markup' elements organized by
  438. // numerical keys.
  439. if (array_key_exists('#markup', $build[$key]) and trim($build[$key]['#markup'])) {
  440. $markup = $build[$key]['#markup'];
  441. }
  442. // For backwards copmatibility we should support the '#value' element as well.
  443. elseif (array_key_exists('#value', $build[$key]) and trim($build[$key]['#value'])) {
  444. $markup = $build[$key]['#markup'];
  445. }
  446. // if we have no '#markup' field then this element has not yet
  447. // been rendered. Let's render it and substitute that for markup
  448. if (!$markup) {
  449. $markup = trim(render($build[$key]));
  450. }
  451. // Setup the content array for this element
  452. $build[$key] = [
  453. '#markup' => $markup,
  454. '#tripal_toc_id' => $toc_item_id,
  455. '#tripal_toc_title' => $toc_item_title,
  456. '#weight' => $weight,
  457. '#hide' => $hide,
  458. ];
  459. // if we still don't have markup then skip this one
  460. if (!$markup) {
  461. continue;
  462. }
  463. //-----------------------
  464. // FIND THE TEMPLATE PATH
  465. //-----------------------
  466. // get the template path so we can put it in an admin message box
  467. $path = '';
  468. if (!array_key_exists('#tripal_template_show', $build[$key]) or
  469. $build[$key]['#tripal_template_show'] == TRUE) {
  470. if ($cache and array_key_exists($key, $cache->data) and array_key_exists('path', $cache->data[$key])) {
  471. $path = $cache->data[$key]['path'] . '/' . $key . '.tpl.php';
  472. $path = tripal_set_message("Administrators, you can
  473. customize the way the content above is presented. Tripal provides a template
  474. file for each pane of content. To customize, copy the template file to your
  475. site's default theme, edit then " .
  476. l('clear the Drupal cache', 'admin/config/development/performance', ['attributes' => ['target' => '_blank']]) . ".
  477. Currently, the content above is provided by this template: <br><br>$path",
  478. TRIPAL_INFO,
  479. ['return_html' => 1]
  480. );
  481. }
  482. }
  483. //-----------------------
  484. // ADD THIS PANE TO THE TOC BY ORDER OF WEIGHT
  485. //-----------------------
  486. // set the weight of the TOC item and add it to our $toc array
  487. // for building of the TOC below
  488. $weight = 0;
  489. if (array_key_exists('#weight', $build[$key])) {
  490. $weight = $build[$key]['#weight'];
  491. }
  492. $toc[$weight][$toc_item_title] = $toc_item_link;
  493. //-----------------------
  494. // CREATE THE CONTENT PANE MARKUP
  495. //-----------------------
  496. // add a surrounding <div> box around the content
  497. $updated_markup = "
  498. <div id=\"$toc_item_id-tripal-data-pane\" class=\"tripal-data-pane\">
  499. <div class=\"$toc_item_id-tripal-data-pane-title tripal-data-pane-title\">$toc_item_title</div>
  500. $markup
  501. $path
  502. </div>
  503. </div>
  504. ";
  505. $build[$key]['#markup'] = $updated_markup;
  506. } // end foreach ($build as $key => $value) {
  507. } // end if ($build['#tripal_generic_node_template'] == TRUE) {
  508. //-----------------------
  509. // BUILD THE TABLE OF CONTENTS LINKS
  510. //-----------------------
  511. // first sort the links numerically by their weight
  512. ksort($toc, SORT_NUMERIC);
  513. $toc_html = '';
  514. foreach ($toc as $weight => $links) {
  515. // for links in the same weight, sort them alphabetically
  516. ksort($links);
  517. foreach ($links as $toc_item_title => $toc_item_link) {
  518. $toc_html .= $toc_item_link;
  519. }
  520. }
  521. $build['tripal_toc']['#markup'] = "<div id=\"$node->type-tripal-toc-pane\" class=\"tripal-toc-pane\">$toc_html</div>";
  522. }
  523. /**
  524. *
  525. * @param $build
  526. */
  527. function tripal_core_get_toc_overrides($nid, $key, $node_type, $mode) {
  528. // Set override defaults
  529. $override_title = '';
  530. $override_weight = '';
  531. $override_hide = 0;
  532. if ($mode != "manage_type") {
  533. // First look to see if the node has customizations for this item.
  534. $toc_item_overrides = db_select('tripal_toc', 'tc')
  535. ->fields('tc', ['title', 'weight', 'hide'])
  536. ->condition('key', $key)
  537. ->condition('nid', $nid)
  538. ->execute()
  539. ->fetchObject();
  540. if ($toc_item_overrides) {
  541. $override_title = $toc_item_overrides->title;
  542. $override_weight = $toc_item_overrides->weight;
  543. $override_hide = $toc_item_overrides->hide;
  544. return [
  545. 'title' => $override_title,
  546. 'weight' => $override_weight,
  547. 'hide' => $override_hide,
  548. ];
  549. }
  550. }
  551. // If there are no specific node customizations then look to see if there
  552. // are customizations for this content type.
  553. $toc_item_overrides = db_select('tripal_toc', 'tc')
  554. ->fields('tc', ['title', 'weight', 'hide'])
  555. ->condition('node_type', $node_type)
  556. ->condition('key', $key)
  557. ->isNull('nid')
  558. ->execute()
  559. ->fetchObject();
  560. if ($toc_item_overrides) {
  561. $override_title = $toc_item_overrides->title;
  562. $override_weight = $toc_item_overrides->weight;
  563. $override_hide = $toc_item_overrides->hide;
  564. }
  565. return [
  566. 'title' => $override_title,
  567. 'weight' => $override_weight,
  568. 'hide' => $override_hide,
  569. ];
  570. }
  571. /**
  572. *
  573. */
  574. function tripal_core_content_type_toc_form($form, &$form_state, $content_type) {
  575. // Get the type details
  576. $all_types = node_type_get_types();
  577. $type_info = $all_types[$content_type];
  578. $form["#tree"] = TRUE;
  579. // Get a single node of this type so we can get all the possible content
  580. // for it.
  581. $sql = "SELECT nid FROM {node} WHERE type = :type LIMIT 1 OFFSET 0";
  582. $nid = db_query($sql, [':type' => $content_type])->fetchField();
  583. if (!$nid) {
  584. $form["not_available"] = [
  585. '#markup' => t('Please sync at least one %type_name record. A node
  586. must exist before customizations to the Table of Contents (TOC) can
  587. be performed.', ['%type_name' => $type_info->name]),
  588. ];
  589. return $form;
  590. }
  591. // Load the node
  592. $node = node_load($nid);
  593. // Get the content array for this node, then pass it through the
  594. // tripal_core_node_view_alter which generates the TOC. After that
  595. // we can use the $build array to build the form. We have to add
  596. // a 'tripal_toc_mode' to the $node because we need to give the mode
  597. // to the tripal_core_node_view_build_toc function.
  598. $node->tripal_toc_mode = 'manage_type';
  599. node_build_content($node);
  600. $build = $node->content;
  601. $build["#node"] = $node;
  602. tripal_core_node_view_alter($build);
  603. $form["instructions"] = [
  604. '#type' => 'fieldset',
  605. '#collapsed' => TRUE,
  606. '#collapsible' => TRUE,
  607. '#title' => 'Instructions',
  608. ];
  609. $form["instructions"]["main"] = [
  610. '#markup' => '</p>' . t('Below is a list of the titles of
  611. content panes that can appear on all %type_name pages. You may rename
  612. the titles or drag and drop them to change the order. Content that appears
  613. only on a single page can not be ordered here, but must be ordered using
  614. the TOC tab on the page itself. If a page has customized TOC settings
  615. then those settings will take precedent over these.',
  616. ['%type_name' => $type_info->name]) . '</p>' .
  617. '<p>' . t('The list below shows all possible content
  618. panes that can appear. However, those without content are automatically
  619. hidden and do not appear in the TOC.' . '</p>'),
  620. ];
  621. $form['content_type'] = [
  622. '#type' => 'value',
  623. '#value' => $content_type,
  624. ];
  625. // Iterate through the built items and add form elemetns for each one.
  626. foreach (element_children($build) as $key) {
  627. $element = $build[$key];
  628. if (array_key_exists('#tripal_toc_id', $element)) {
  629. $toc_id = $element['#tripal_toc_id'];
  630. $toc_title = $element['#tripal_toc_title'];
  631. $toc_weight = $element['#weight'];
  632. $toc_hide = $element['#hide'];
  633. $form['toc_items'][$toc_id]['title'] = [
  634. '#type' => 'textfield',
  635. '#default_value' => $toc_title,
  636. ];
  637. $form['toc_items'][$toc_id]['hide'] = [
  638. '#type' => 'checkbox',
  639. '#default_value' => $toc_hide,
  640. ];
  641. $form['toc_items'][$toc_id]['weight'] = [
  642. '#type' => 'textfield',
  643. '#default_value' => $toc_weight,
  644. '#attributes' => [
  645. 'class' => ['tripal-node-toc-items-weights'],
  646. ],
  647. '#size' => 5,
  648. ];
  649. }
  650. }
  651. $form['toc_items']['#theme'] = 'tripal_node_toc_items_table';
  652. $form['submit'] = [
  653. '#type' => 'submit',
  654. '#name' => 'toc_submit',
  655. '#value' => t('Submit'),
  656. ];
  657. $form['unset'] = [
  658. '#type' => 'submit',
  659. '#name' => 'toc_unset',
  660. '#value' => t('Reset to Defaults'),
  661. ];
  662. return $form;
  663. }
  664. /**
  665. * Implements hook_validate for the tripal_core_node_toc_form.
  666. */
  667. function tripal_core_content_type_toc_form_validate($form, &$form_state) {
  668. $toc_items = $form_state['values']['toc_items'];
  669. // Iterate through the TOC items and validate.
  670. foreach ($toc_items as $toc_id => $item) {
  671. if (!$item['title']) {
  672. form_set_error('toc_items][' . $toc_id, "Please provide a valid title.");
  673. }
  674. }
  675. }
  676. /**
  677. * Implements hook_submit for the tripal_core_node_toc_form.
  678. */
  679. function tripal_core_content_type_toc_form_submit($form, &$form_state) {
  680. $toc_items = $form_state['values']['toc_items'];
  681. $content_type = $form_state['values']['content_type'];
  682. if ($form_state['clicked_button']['#name'] == "toc_submit") {
  683. $transaction = db_transaction();
  684. try {
  685. // First delete any settings for this content type
  686. db_delete('tripal_toc')
  687. ->condition('node_type', $content_type)
  688. ->isNull('nid')
  689. ->execute();
  690. // Second add in any new settings for this node
  691. foreach ($toc_items as $toc_id => $item) {
  692. db_insert('tripal_toc')
  693. ->fields([
  694. 'node_type' => $content_type,
  695. 'key' => $toc_id,
  696. 'title' => $item['title'],
  697. 'weight' => $item['weight'],
  698. 'hide' => $item['hide'],
  699. ])
  700. ->execute();
  701. }
  702. drupal_set_message("TOC changes successfully applied to this content type.");
  703. } catch (Exception $e) {
  704. $transaction->rollback();
  705. drupal_set_message("Failed to apply TOC changes.", "error");
  706. }
  707. }
  708. if ($form_state['clicked_button']['#name'] == "toc_unset") {
  709. $transaction = db_transaction();
  710. try {
  711. // First delete any settings for this node
  712. db_delete('tripal_toc')
  713. ->condition('node_type', $content_type)
  714. ->isNull('nid')
  715. ->execute();
  716. drupal_set_message("The TOC is reset to defaults for this content type.");
  717. } catch (Exception $e) {
  718. $transaction->rollback();
  719. drupal_set_message("Failed to apply TOC changes.", "error");
  720. }
  721. }
  722. }