tripal_example.chado_node.inc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. <?php
  2. /**
  3. * @file
  4. * This file should contain all Drupal hooks for interacting with nodes.
  5. *
  6. */
  7. /**
  8. * Implementation of hook_node_info().
  9. *
  10. * This hook provides information to drupal about any node types that are being
  11. * created by this module. If your module does not create any node types then
  12. * this function is not required.
  13. *
  14. * @ingroup tripal_example
  15. */
  16. function tripal_example_node_info() {
  17. $nodes = array();
  18. // EXPLANATION: this array describes all of the node types that are created
  19. // by this module. For many Tripal modules (e.g. tripal_example, tripal_stock,
  20. // tripal_library, tripal_pub, etc.) new node types are created. It is
  21. // customary to name all new node types that interact with data in Chado
  22. // with a 'chado_' prefix.
  23. $nodes['chado_example'] = array(
  24. 'name' => t('Example'),
  25. 'base' => 'chado_example',
  26. 'description' => t('A record from the fake chado example table'),
  27. 'has_title' => TRUE,
  28. 'locked' => TRUE,
  29. // EXPLANATION: This section of the node type array specifies how Tripal will sync the node
  30. // types with data in Chado. When Drupal creates a node it has no way of
  31. // coordinating which node belongs to which record in Chado. Therefore,
  32. // Tripal maintains tables in the Drupal schema that maps Drupal nodes
  33. // to recrords in Chado. Syncing is the process of creating Drupal nodes
  34. // and linking them to the appropriate record.
  35. 'chado_node_api' => array(
  36. 'base_table' => 'example', // the base table name (e.g. example, example, contact)
  37. 'hook_prefix' => 'chado_example',// the node type hook prefix
  38. 'record_type_title' => array(
  39. 'singular' => t('Example'), // how to refer to the record
  40. 'plural' => t('Examples') // how to refer to the record in plurals
  41. ),
  42. 'sync_filters' => array(
  43. 'type_id' => TRUE, // if the record has a type_id set to TRUE
  44. 'organism_id' => TRUE // if the record has an organism_id set to TRUE
  45. ),
  46. )
  47. );
  48. return $nodes;
  49. }
  50. /**
  51. * Implement hook_access(). This hook provides instructions to
  52. * drupal for which users can access the custom content types
  53. * created in the function above. The available permissions
  54. * are set in the chado_example_permissions() hook in the
  55. * tripal_example.module file. This hook is not needed
  56. * if no node types were defined in the hook_node_info() hook.
  57. *
  58. * @return
  59. * This function should return null if it does not specificially
  60. * deny access. This allows for other mechanisms to to deny
  61. * or reject access. If the return value is TRUE then access
  62. * is granted regardless of any other rules that might be implemented
  63. * by other modules.
  64. */
  65. function chado_example_node_access($node, $op, $account) {
  66. $node_type = $node;
  67. if (is_object($node)) {
  68. $node_type = $node->type;
  69. }
  70. // EXPLANATION: in the tripal_example_permissions() function we
  71. // created the permission types that are used here to check for
  72. // access permissions to the 'chado_exmaple' node type.
  73. if($node_type == 'chado_example') {
  74. if ($op == 'create') {
  75. if (!user_access('create chado_example content', $account)) {
  76. return NODE_ACCESS_DENY;
  77. }
  78. return NODE_ACCESS_ALLOW;
  79. }
  80. if ($op == 'update') {
  81. if (!user_access('edit chado_example content', $account)) {
  82. return NODE_ACCESS_DENY;
  83. }
  84. }
  85. if ($op == 'delete') {
  86. if (!user_access('delete chado_example content', $account)) {
  87. return NODE_ACCESS_DENY;
  88. }
  89. }
  90. if ($op == 'view') {
  91. if (!user_access('access chado_example content', $account)) {
  92. return NODE_ACCESS_DENY;
  93. }
  94. }
  95. }
  96. return NODE_ACCESS_IGNORE;
  97. }
  98. /**
  99. * Implementation of hook_form()
  100. *
  101. * Creates the form for editing or inserting a record
  102. *
  103. * @ingroup tripal_example
  104. */
  105. function chado_example_form($node, &$form_state) {
  106. // EXPLANATION: This function should construct a form array that is used
  107. // by Drupal to contruct a form for inserting or editing our new node type.
  108. // See this page for information about the Form API:
  109. // https://api.drupal.org/api/drupal/includes!form.inc/group/form_api/7
  110. //
  111. // The code below is laid out in the following order
  112. // 1) Set default values
  113. // 2) Add form elements used by this node type
  114. // 3) Use the Tripal API to add form elemetns for properties,
  115. // dbxref's and relationships
  116. //
  117. // For the example code below we assume that the fake 'example' table
  118. // only has a uniquename, organism_id, type_id and example_id.
  119. $form = array();
  120. // Default values can come in the following ways:
  121. //
  122. // 1) as elements of the $node object. This occurs when editing an existing example
  123. // 2) in the $form_state['values'] array which occurs on a failed validation or
  124. // ajax callbacks from non submit form elements
  125. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  126. // form elements and the form is being rebuilt
  127. //
  128. // set form field defaults
  129. // SET FORM DEFAULTS
  130. //---------------------------------------------
  131. // if we are editing an existing node then the 'example' record from Chado
  132. // is already part of the node, so we set the defaults from that object
  133. if (property_exists($node, 'example')) {
  134. $example = $node->example;
  135. $example = chado_expand_var($example, 'field', 'example.residues');
  136. $example_id = $example->example_id;
  137. $uniquename = $example->uniquename;
  138. // keep track of the example id
  139. $form['example_id'] = array(
  140. '#type' => 'value',
  141. '#value' => $example_id,
  142. );
  143. }
  144. // if we are re constructing the form from a failed validation or ajax callback
  145. // then use the $form_state['values'] values
  146. if (array_key_exists('values', $form_state)) {
  147. $uniquename = $form_state['values']['uniquename'];
  148. $example_type = $form_state['values']['example_type'];
  149. }
  150. // if we are re building the form from after submission (from ajax call) then
  151. // the values are in the $form_state['input'] array
  152. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  153. $uniquename = $form_state['input']['uniquename'];
  154. $example_type = $form_state['input']['example_type'];
  155. }
  156. // FORM ELEMENTS
  157. //---------------------------------------------
  158. $form['uniquename']= array(
  159. '#type' => 'textfield',
  160. '#title' => t('Unique Name'),
  161. '#required' => TRUE,
  162. '#default_value' => $uniquename,
  163. '#description' => t('Enter a unique name for this example. This name must be unique.'),
  164. '#maxlength' => 255
  165. );
  166. // for the type_id we want to use the default vocabulary so that this
  167. // field can have autocomplete functionality
  168. $type_cv = tripal_get_default_cv('example', 'type_id');
  169. $cv_id = $type_cv->cv_id;
  170. $form['example_type'] = array(
  171. '#title' => t('Feature Type'),
  172. '#type' => 'textfield',
  173. '#description' => t("Choose the example type."),
  174. '#required' => TRUE,
  175. '#default_value' => $example_type,
  176. '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
  177. );
  178. // add a select box of organisms
  179. $organisms = tripal_get_organism_select_options();
  180. $form['organism_id'] = array(
  181. '#title' => t('Organism'),
  182. '#type' => t('select'),
  183. '#description' => t("Choose the organism with which this example is associated"),
  184. '#required' => TRUE,
  185. '#default_value' => $organism_id,
  186. '#options' => $organisms,
  187. );
  188. // PROPERTIES FORM
  189. //---------------------------------------------
  190. // If there is a exampleprop table and you want to allow users to add/remove entries
  191. // from it through your node form then add this section to your own node form
  192. $prop_cv = tripal_get_default_cv('exampleprop', 'type_id');
  193. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  194. $details = array(
  195. 'property_table' => 'exampleprop', // the name of the prop table
  196. 'chado_id' => $example_id, // the value of example_id for this record
  197. 'cv_id' => $cv_id // the cv.cv_id of the cv governing exampleprop.type_id
  198. );
  199. // Adds the form elements to your current form
  200. chado_add_node_form_properties($form, $form_state, $details);
  201. // ADDITIONAL DBXREFS FORM
  202. //---------------------------------------------
  203. // If there is a example_dbxref table and you want to allow users to add/remove entries
  204. // from it through your node form then add this section to your own node form
  205. $details = array(
  206. 'linking_table' => 'example_dbxref', // the name of the _dbxref table
  207. 'base_foreign_key' => 'example_id', // the name of the key in your base chado table
  208. 'base_key_value' => $example_id // the value of example_id for this record
  209. );
  210. // Adds the form elements to your current form
  211. chado_add_node_form_dbxrefs($form, $form_state, $details);
  212. // RELATIONSHIPS FORM
  213. //---------------------------------------------
  214. // If there is a example_relationship table and you want to allow users to add/remove entries
  215. // from it through your node form then add this section to your own node form
  216. $rels_cv = tripal_get_default_cv('example_relationship', 'type_id');
  217. $cv_id = $rels_cv ? $rels_cv->cv_id : NULL;
  218. $details = array(
  219. 'relationship_table' => 'example_relationship', // the name of the _relationship table
  220. 'base_table' => 'example', // the name of your chado base table
  221. 'base_foreign_key' => 'example_id', // the name of the key in your base chado table
  222. 'base_key_value' => $example_id, // the value of example_id for this record
  223. 'nodetype' => 'example', // the human-readable name of your node type
  224. 'cv_id' => $cv_id // the cv.cv_id of the cv governing example_relationship.type_id
  225. );
  226. // Adds the form elements to your current form
  227. chado_add_node_form_relationships($form, $form_state, $details);
  228. // return the form
  229. return $form;
  230. }
  231. /**
  232. * Implementation of hook_validate
  233. *
  234. * This function validates a form prior to insert or update. If an error
  235. * is detected, it sets the error using form_set_error() which takes the
  236. * user back to the form to make corrections.
  237. *
  238. * This validation is being used for three activities:
  239. * CASE A: Update a node that exists in both drupal and chado
  240. * CASE B: Synchronizing a node from chado to drupal
  241. * CASE C: Inserting a new node that exists in niether drupal nor chado
  242. *
  243. * @param $node
  244. *
  245. *
  246. * @ingroup tripal_example
  247. */
  248. function chado_example_validate($node, $form, &$form_state) {
  249. // if this is a delete then don't validate
  250. if($node->op == 'Delete') {
  251. return;
  252. }
  253. // we are syncing if we do not have a node ID but we do have a example_id. We don't
  254. // need to validate during syncing so just skip it.
  255. if (is_null($node->nid) and property_exists($node, 'example_id') and $node->example_id != 0) {
  256. return;
  257. }
  258. // be sure to always trim text fields
  259. $node->uniquename = trim($node->uniquename);
  260. // Validating for an update. If the 'nid' property is present in the node then
  261. // this is an update and validation can be different for updates
  262. if (property_exists($node, 'nid')) {
  263. // if there is a problem with a field then you can set an error on the form
  264. form_set_error('uniquename', t("example update cannot proceed. The example name '$node->uniquename' is not unique for this organism. Please provide a unique name for this example."));
  265. }
  266. // Validating for an insert
  267. else {
  268. // if there is a problem with a field then you can set an error on the form
  269. form_set_error('uniquename', t("example insert cannot proceed. The example name '$node->uniquename' already exists for this organism. Please provide a unique name for this example."));
  270. }
  271. }
  272. /**
  273. * Implementation of hook_insert(). This function is called after the
  274. * node is inserted into the database. We need it so that we can insert
  275. * appropriate fields as provided by the user into the database. And so that
  276. * we can link the new Drupal node to the data in Chado via the chado_example
  277. * linking table. We can get to this function also during "syncing".
  278. * With syncing, however, the data already exists in Chado and we do not want
  279. * to try to re-add it. But we do need to add an entry to the chado_example table
  280. * to link the Drupal node with the data in the 'example' table of Chado.
  281. *
  282. * This function is not required if the hook_node_info() does not define
  283. * any custom node types.
  284. *
  285. * @ingroup tripal_example
  286. */
  287. function chado_example_insert($node) {
  288. // be sure to always trim text fields
  289. $node->uniquename = trim($node->uniquename);
  290. // if there is an example_id in the $node object then this must be a sync so
  291. // we can skip adding the example as it is already there, although
  292. // we do need to proceed with the rest of the insert
  293. if (!property_exists($node, 'example_id')) {
  294. // perform the insert using the tripal_core_chado_insert function();
  295. $values = array(
  296. 'uniquename' => $node->uniquename,
  297. 'residues' => $residues,
  298. );
  299. $example = chado_select_record('example', array('*'), $values);
  300. if (!$example) {
  301. drupal_set_message(t('Unable to add example.'), 'warning');
  302. tripal_report_error('tripal_example', TRIPAL_WARNING, 'Insert example: Unable to create example where values: %values',
  303. array('%values' => print_r($values, TRUE)));
  304. return;
  305. }
  306. // get the example_id for linking Drupal node with Chado data
  307. $example_id = $example->example_id;
  308. // Only add to other chado tables if the base record was inserted properly
  309. if ($example_id > 0) {
  310. // If you implemented the properties form in chado_example_form then you need to
  311. // handle inserting these properties into your chado prop table.
  312. $details = array(
  313. 'property_table' => 'exampleprop', // the name of the prop table
  314. 'base_table' => 'example', // the name of your chado base table
  315. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  316. 'foreignkey_value' => $example_id // the value of the example_id key
  317. );
  318. chado_update_node_form_properties($node, $details);
  319. // If you implemented the dbxrefs form in chado_example_form then you need to
  320. // handle inserting these database references into your chado _dbxref table.
  321. $details = array(
  322. 'linking_table' => 'example_dbxref', // the name of your _dbxref table
  323. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  324. 'foreignkey_value' => $example_id // the value of the example_id key
  325. );
  326. chado_update_node_form_dbxrefs($node, $details);
  327. // If you implemented the relationships form in chado_example_form then you need to
  328. // handle inserting these relationships into your chado _relationship table.
  329. $details = array(
  330. 'relationship_table' => 'example_relationship', // name of the _relationship table
  331. 'foreignkey_value' => $example_id // value of the example_id key
  332. );
  333. chado_update_node_form_relationships($node, $details);
  334. }
  335. }
  336. else {
  337. // the node has an example_id so get it for linking Drupal node with Chado data
  338. $example_id = $node->example_id;
  339. }
  340. // Make sure the entry for this example doesn't already exist in the
  341. // chado_example table if it doesn't exist then we want to add it.
  342. // $check_org_id = chado_get_id_from_nid('example', $node->nid);
  343. if (!$check_org_id) {
  344. $record = new stdClass();
  345. $record->nid = $node->nid;
  346. $record->vid = $node->vid;
  347. $record->example_id = $example_id;
  348. drupal_write_record('chado_example', $record);
  349. }
  350. }
  351. /**
  352. * Implementation of hook_update(). This function runs after the
  353. * node has been inserted into the Drupal schema and allows us to
  354. * update the record in Chado.
  355. *
  356. * This function is not required if the hook_node_info() does not define
  357. * any custom node types.
  358. *
  359. * @ingroup tripal_example
  360. */
  361. function chado_example_update($node) {
  362. // be sure to always trim text fields
  363. $node->uniquename = trim($node->uniquename);
  364. // use the chado_update_record() function to update the record
  365. $match = array(
  366. 'example_id' => $example_id,
  367. );
  368. $values = array(
  369. 'uniquename' => $node->uniquename,
  370. );
  371. $options = array('return_record' => TRUE);
  372. $status = chado_update_record('example', $match, $values, $options);
  373. if (!$status) {
  374. drupal_set_message(t('Unable to update example.'), 'warning');
  375. tripal_report_error('tripal_example', TRIPAL_WARNING, 'Update example: Unable to update example where values: %values',
  376. array('%values' => print_r($values, TRUE)));
  377. }
  378. // If you implemented the properties form in chado_example_form then you need to
  379. // handle updating these properties into your chado prop table.
  380. $details = array(
  381. 'property_table' => 'exampleprop', // the name of the prop table
  382. 'base_table' => 'example', // the name of your chado base table
  383. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  384. 'foreignkey_value' => $example_id // the value of the example_id key
  385. );
  386. chado_update_node_form_properties($node, $details);
  387. // If you implemented the dbxrefs form in chado_example_form then you need to
  388. // handle updating these database references into your chado _dbxref table.
  389. $details = array(
  390. 'linking_table' => 'example_dbxref', // the name of your _dbxref table
  391. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  392. 'foreignkey_value' => $example_id // the value of the example_id key
  393. );
  394. chado_update_node_form_dbxrefs($node, $details);
  395. // If you implemented the relationships form in chado_example_form then you need to
  396. // handle updating these relationships into your chado _relationship table.
  397. $details = array(
  398. 'relationship_table' => 'example_relationship', // name of the _relationship table
  399. 'foreignkey_value' => $example_id // value of the example_id key
  400. );
  401. chado_update_node_form_relationships($node, $details);
  402. }
  403. /**
  404. * Implementation of hook_delete(). This function runs after the
  405. * node has been deleted from the Drupal schema and allows us to
  406. * delete the corresponding recrod in Chado.
  407. *
  408. * This function is not required if the hook_node_info() does not define
  409. * any custom node types.
  410. *
  411. * @ingroup tripal_example
  412. */
  413. function chado_example_delete($node) {
  414. // get the example id from the node
  415. $example_id = chado_get_id_from_nid('example', $node->nid);
  416. // if we don't have a example id for this node then this isn't a node of
  417. // type chado_example or the entry in the chado_example table was lost.
  418. if (!$example_id) {
  419. return;
  420. }
  421. // remove the entry in the chado_exapmle table linking the deleted
  422. // Drupal node with the data in chado
  423. $sql_del = "DELETE FROM {chado_example} WHERE nid = :nid AND vid = :vid";
  424. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  425. // Remove data from example tables of chado database. This will
  426. // cause a cascade delete and remove all data in referencing tables
  427. // for this example
  428. chado_query("DELETE FROM {example} WHERE example_id = :example_id", array(':example_id' => $example_id));
  429. // inform the user that the data was deleted
  430. drupal_set_message(t("The example and all associated data were removed from Chado"));
  431. }
  432. /**
  433. * Implementation of hook_load(). This function is necessary to load
  434. * into the $node object the fields of the table form Chado. For example
  435. * for the example table, the chado_example_load() function adds in
  436. * a example object which contains all of the fields and sub objects
  437. * for data in tables with foreign key relationships.
  438. *
  439. * This function is not required if the hook_node_info() does not define
  440. * any custom node types.
  441. *
  442. * @ingroup tripal_example
  443. */
  444. function chado_example_load($nodes) {
  445. // EXPLANATION: when displaying or node or accessing the node in a template
  446. // we need the data from Chado. This fucntion finds the record in Chado that
  447. // this node belongs to and adds the record.
  448. // there may be multiple nodes that get passed in so we have to iterate through
  449. // them all
  450. foreach ($nodes as $nid => $node) {
  451. // find the example and add in the details
  452. $example_id = chado_get_id_from_nid('example', $nid);
  453. // if the nid does not have a matching record then skip this node.
  454. // this can happen with orphaned nodes.
  455. if (!$example_id) {
  456. continue;
  457. }
  458. // build the example variable by using the chado_generate_var() function
  459. $values = array('example_id' => $example_id);
  460. $example = chado_generate_var('example', $values);
  461. // for fields in the table that are of type 'text' you may want to include those
  462. // by default, the tripal_core_generate_chado_var does not include text fields as
  463. // they may be very large and including a large text field can slow the page load.
  464. // If you know a text field will never be large and it is important for the
  465. // other functions that will see the node to have access to a field you can
  466. // include it here using the chado_expand_var() function. In most
  467. // cases it is probably best to let the end-user decide if text fields should
  468. // be included by using this function in the templates.
  469. $example = chado_expand_var($example, 'field', 'example.residues');
  470. // add the new example object to this node.
  471. $nodes[$nid]->example = $example;
  472. }
  473. }
  474. /**
  475. * Implementation of hook_node_presave().
  476. *
  477. * Performs actions on a node object prior to it being saved
  478. *
  479. * @ingroup tripal_example
  480. */
  481. function tripal_example_node_presave($node) {
  482. // EXPLANATION: This node is useful for
  483. // making changes to the node prior to it being saved to the database.
  484. // One useful case for this is to set the title of a node. In some cases
  485. // such as for the organism module, the title will be set depending on
  486. // what genus and species is provided. This hook can allow the title to
  487. // be set using user supplied data before the node is saved. In practice
  488. // any change can be made to any fields in the node object.
  489. //
  490. // This function is not required. You probably won't need it if you
  491. // don't define a custom node type in the hook_node_info() function. But
  492. // it is node type agnostic, so you can use this function to change the
  493. // contents of any node regardless of it's type.
  494. // set the node title
  495. switch ($node->type) {
  496. case 'chado_example':
  497. // for a form submission the 'uniquename' field will be set,
  498. // for a sync, we must pull from the example object
  499. if (property_exists($node, 'uniquename')) {
  500. // set the title
  501. $node->title = $node->uniquename;
  502. }
  503. else if (property_exists($node, 'example')) {
  504. $node->title = $node->example->uniquename;
  505. }
  506. break;
  507. }
  508. }
  509. /**
  510. * Implementation of hook node_insert().
  511. *
  512. * Performs actions after any node has been inserted.
  513. *
  514. * @ingroup tripal_example
  515. */
  516. function tripal_example_node_insert($node) {
  517. // EXPLANATION: This function is used
  518. // after any a node is inserted into the database. It is different
  519. // from the hook_insert() function above in that it is called after
  520. // any node is saved, regardlesss of it's type. This function is useful
  521. // for making changes to the database after a node is inserted.
  522. // An example comes from the tripal_feature module where the URL alias
  523. // of a node cannot be set in the hook_insert() function. Therefore
  524. // the tripal_feature module uses this function to set the url path
  525. // of a newly inserted example node.
  526. //
  527. // This function is not required. You probably won't need it if you
  528. // don't define a custom node type in the hook_node_info() function. But
  529. // it is node type agnostic, so you can use this function to do any
  530. // activity after insert of any node.
  531. // the Example code below will set the URL path after inserting. We do it
  532. // here because we do not know the example_id in the presave and cannot do
  533. // it in the hook_insert()
  534. switch ($node->type) {
  535. case 'chado_example':
  536. // if we do not have an record ID for this node then get it
  537. if (!$node->example_id) {
  538. $node->example_id = chado_get_id_for_node('example', $node);
  539. }
  540. // set the URL for this example page
  541. // see the code in the tripal_feature/includes/tripal_feature.chado_node.inc file
  542. // in the function tripal_feature_node_insert for an example of how that
  543. // module sets the URL. It uses a configuration file to allow the user
  544. // to dynmically build a URL schema and then uses that schema to generate
  545. // a URL string.
  546. break;
  547. }
  548. }
  549. /**
  550. * Implementation of hook node_update().
  551. *
  552. * Performs actions after any node has been updated.
  553. *
  554. */
  555. function tripal_example_node_update($node) {
  556. // EXPLANATION: This function is used
  557. // after any a node is updated in the database. It is different
  558. // from the hook_update() function above in that it is called after
  559. // any node is updated, regardlesss of it's type.
  560. // An example comes from the tripal_feature module where the URL alias
  561. // of a node cannot be set in the hook_update() function. Therefore
  562. // the tripal_feature module uses this function to reset the url path
  563. // of an updated feature node.
  564. //
  565. // This function is not required. You probably won't need it if you
  566. // don't define a custom node type in the hook_node_info() function. But
  567. // it is node type agnostic, so you can use this function to do any
  568. // activity after insert of a node.
  569. // add items to other nodes, build index and search results
  570. switch ($node->type) {
  571. case 'chado_example':
  572. // set the URL for this example page
  573. // see the code in the tripal_feature/includes/tripal_feature.chado_node.inc file
  574. // in the function tripal_feature_node_insert for an example of how that
  575. // module sets the URL. It uses a configuration file to allow the user
  576. // to dynmically build a URL schema and then uses that schema to generate
  577. // a URL string.
  578. break;
  579. }
  580. }
  581. /**
  582. * Implementation of hook_node_view().
  583. *
  584. * @ingroup tripal_example
  585. */
  586. function tripal_example_node_view($node, $view_mode, $langcode) {
  587. // EXPLANATION: This function defines the content "blocks" that appear
  588. // when thhe node is displayed. It is node type agnostic so we can add
  589. // content to any node type. So, we use this function to add the content
  590. // from all of our theme templates onto our new node type. We will also
  591. // use this function to add content to other node types.
  592. switch ($node->type) {
  593. case 'chado_example':
  594. // there are different ways a node can be viewed. Primarily Tripal
  595. // supports full page view and teaser view.
  596. if ($view_mode == 'full') {
  597. // There is always a base template. This is the template that
  598. // is first shown when the example node type is first displayed.
  599. // if you are using the default Tripal node template, then you should
  600. // also set two additional items in each array: tripal_toc_id and
  601. // tripal_toc_title. The tripal_tock_id should be a single unqiue
  602. // world that is used to reference the template. This ID is used for
  603. // constructing URLs for the content. The tripal_toc_title contains
  604. // the title that should appear in the table of contents for this
  605. // content. You should only set the '#weight' element for the
  606. // base template (or Overview) to ensure that it appears at the top of
  607. // the list. Otherwise items are sorted alphabetically.
  608. $node->content['tripal_example_base'] = array(
  609. '#markup' => theme('tripal_example_base', array('node' => $node)),
  610. '#tripal_toc_id' => 'base',
  611. '#tripal_toc_title' => 'Overview',
  612. '#weight' => -100,
  613. );
  614. // we can add other templates as well for properties, publications,
  615. // dbxrefs, etc...
  616. $node->content['tripal_example_properties'] = array(
  617. '#markup' => theme('tripal_example_properties', array('node' => $node)),
  618. '#tripal_toc_id' => 'properties',
  619. '#tripal_toc_title' => 'Properties',
  620. );
  621. $node->content['tripal_example_references'] = array(
  622. '#markup' => theme('tripal_feature_references', array('node' => $node)),
  623. '#tripal_toc_id' => 'references',
  624. '#tripal_toc_title' => 'Cross References',
  625. );
  626. $node->content['tripal_example_relationships'] = array(
  627. '#markup' => theme('tripal_feature_relationships', array('node' => $node)),
  628. '#tripal_toc_id' => 'relationships',
  629. '#tripal_toc_title' => 'Relationships',
  630. );
  631. }
  632. // set the content for the teaser view
  633. if ($view_mode == 'teaser') {
  634. // The teaser is also a required template
  635. $node->content['tripal_example_teaser'] = array(
  636. '#value' => theme('tripal_example_teaser', array('node' => $node)),
  637. );
  638. }
  639. break;
  640. // you can add custom content to any node type by adding
  641. // content to the node in the same way as above.
  642. case 'chado_organism':
  643. break;
  644. case 'chado_library':
  645. break;
  646. case 'chado_stock':
  647. break;
  648. case 'chado_analysis':
  649. break;
  650. // ... etc
  651. }
  652. }