tripal_example.chado_node.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. $example = null; // holds the example object record
  132. $example_id = null; // when editing an example record we'll have an example_id
  133. // initialize the defaults for the form fields
  134. $uniquename = '';
  135. $example_type = '';
  136. $organism_id = '';
  137. $description = '';
  138. // if we are editing an existing node then the 'example' record from Chado
  139. // is already part of the node, so we set the defaults from that object
  140. if (property_exists($node, 'example')) {
  141. $example = $node->example;
  142. $example_id = $example->example_id;
  143. $uniquename = $example->uniquename;
  144. $description = $example->description;
  145. $organism_id = $example->organism_id;
  146. // keep track of the example id
  147. $form['example_id'] = array(
  148. '#type' => 'value',
  149. '#value' => $example_id,
  150. );
  151. }
  152. // if we are re constructing the form from a failed validation or ajax callback
  153. // then use the $form_state['values'] values
  154. if (array_key_exists('values', $form_state)) {
  155. $uniquename = $form_state['values']['uniquename'];
  156. $example_type = $form_state['values']['example_type'];
  157. $description = $form_state['values']['description'];
  158. $organism_id = $form_state['values']['organism_id'];
  159. }
  160. // if we are re building the form from after submission (from ajax call) then
  161. // the values are in the $form_state['input'] array
  162. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  163. $uniquename = $form_state['input']['uniquename'];
  164. $example_type = $form_state['input']['example_type'];
  165. $organism_id = $form_state['input']['organism_id'];
  166. }
  167. // FORM ELEMENTS
  168. //---------------------------------------------
  169. $form['uniquename'] = array(
  170. '#type' => 'textfield',
  171. '#title' => t('Unique Name'),
  172. '#required' => TRUE,
  173. '#default_value' => $uniquename,
  174. '#description' => t('Enter a unique name for this example. This name must be unique.'),
  175. '#maxlength' => 255
  176. );
  177. // for the type_id we want to use the default vocabulary so that this
  178. // field can have autocomplete functionality
  179. $type_cv = tripal_get_default_cv('example', 'type_id');
  180. $cv_id = $type_cv->cv_id;
  181. $form['example_type'] = array(
  182. '#title' => t('Example Type'),
  183. '#type' => 'textfield',
  184. '#description' => t("Choose the example type (e.g. Test Type)."),
  185. '#required' => TRUE,
  186. '#default_value' => $example_type,
  187. '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
  188. );
  189. // add a select box of organisms
  190. $organisms = tripal_get_organism_select_options();
  191. $form['organism_id'] = array(
  192. '#title' => t('Organism'),
  193. '#type' => t('select'),
  194. '#description' => t("Choose the organism with which this example is associated"),
  195. '#required' => TRUE,
  196. '#default_value' => $organism_id,
  197. '#options' => $organisms,
  198. );
  199. $form['description'] = array(
  200. '#type' => 'textarea',
  201. '#title' => t('Description'),
  202. '#required' => TRUE,
  203. '#default_value' => $description,
  204. '#description' => t('Enter a description for this example.'),
  205. );
  206. // PROPERTIES FORM
  207. //---------------------------------------------
  208. // If there is a exampleprop table and you want to allow users to add/remove entries
  209. // from it through your node form then add this section to your own node form
  210. $prop_cv = tripal_get_default_cv('exampleprop', 'type_id');
  211. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  212. $details = array(
  213. 'property_table' => 'exampleprop', // the name of the prop table
  214. 'chado_id' => $example_id, // the value of example_id for this record
  215. 'cv_id' => $cv_id // the cv.cv_id of the cv governing exampleprop.type_id
  216. );
  217. // Adds the form elements to your current form
  218. chado_add_node_form_properties($form, $form_state, $details);
  219. // ADDITIONAL DBXREFS FORM
  220. //---------------------------------------------
  221. // If there is a example_dbxref table and you want to allow users to add/remove entries
  222. // from it through your node form then add this section to your own node form
  223. $details = array(
  224. 'linking_table' => 'example_dbxref', // the name of the _dbxref table
  225. 'base_foreign_key' => 'example_id', // the name of the key in your base chado table
  226. 'base_key_value' => $example_id // the value of example_id for this record
  227. );
  228. // Adds the form elements to your current form
  229. chado_add_node_form_dbxrefs($form, $form_state, $details);
  230. // RELATIONSHIPS FORM
  231. //---------------------------------------------
  232. // If there is a example_relationship table and you want to allow users to add/remove entries
  233. // from it through your node form then add this section to your own node form
  234. $rels_cv = tripal_get_default_cv('example_relationship', 'type_id');
  235. $cv_id = $rels_cv ? $rels_cv->cv_id : NULL;
  236. $details = array(
  237. 'relationship_table' => 'example_relationship', // the name of the _relationship table
  238. 'base_table' => 'example', // the name of your chado base table
  239. 'base_foreign_key' => 'example_id', // the name of the key in your base chado table
  240. 'base_key_value' => $example_id, // the value of example_id for this record
  241. 'nodetype' => 'example', // the human-readable name of your node type
  242. 'cv_id' => $cv_id // the cv.cv_id of the cv governing example_relationship.type_id
  243. );
  244. // Adds the form elements to your current form
  245. chado_add_node_form_relationships($form, $form_state, $details);
  246. // return the form
  247. return $form;
  248. }
  249. /**
  250. * Implementation of hook_validate
  251. *
  252. * This function validates a form prior to insert or update. If an error
  253. * is detected, it sets the error using form_set_error() which takes the
  254. * user back to the form to make corrections.
  255. *
  256. * This validation is being used for three activities:
  257. * CASE A: Update a node that exists in both drupal and chado
  258. * CASE B: Synchronizing a node from chado to drupal
  259. * CASE C: Inserting a new node that exists in niether drupal nor chado
  260. *
  261. * @param $node
  262. *
  263. *
  264. * @ingroup tripal_example
  265. */
  266. function chado_example_validate($node, $form, &$form_state) {
  267. // We only want to validate when the node is saved.
  268. // Since this validate can be called on AJAX and Deletion of the node
  269. // we need to make this check to ensure queries are not executed
  270. // without the proper values.
  271. if($node->op != 'Save') {
  272. return;
  273. }
  274. // we are syncing if we do not have a node ID but we do have a example_id. We don't
  275. // need to validate during syncing so just skip it.
  276. if (is_null($node->nid) and property_exists($node, 'example_id') and $node->example_id != 0) {
  277. return;
  278. }
  279. // be sure to always trim text fields
  280. $node->uniquename = trim($node->uniquename);
  281. $node->description = trim($node->description);
  282. // Validating for an update. If the 'nid' property is present in the node then
  283. // this is an update and validation can be different for updates
  284. if (property_exists($node, 'nid')) {
  285. // make sure the feature type is an allowed term
  286. $type_cv = tripal_get_default_cv('example', 'type_id');
  287. $type = tripal_get_cvterm(array(
  288. 'name' => $node->example_type,
  289. 'cv_id' => $type_cv->cv_id,
  290. ));
  291. if (!$type) {
  292. form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
  293. }
  294. // TODO: also we should check that the unique constraint is not invalidated by
  295. // changing either the type_id, organism_id or uniquename.
  296. }
  297. // Validating for an insert
  298. else {
  299. // make sure the feature type is an allowed term
  300. $type_cv = tripal_get_default_cv('example', 'type_id');
  301. $type = tripal_get_cvterm(array(
  302. 'name' => $node->example_type,
  303. 'cv_id' => $type_cv->cv_id,
  304. ));
  305. if (!$type) {
  306. form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
  307. }
  308. // TODO: also we should check that the unique constraint doesn't already exist
  309. }
  310. }
  311. /**
  312. * Implementation of hook_insert(). This function is called after the
  313. * node is inserted into the database. We need it so that we can insert
  314. * appropriate fields as provided by the user into the database. And so that
  315. * we can link the new Drupal node to the data in Chado via the chado_example
  316. * linking table. We can get to this function also during "syncing".
  317. * With syncing, however, the data already exists in Chado and we do not want
  318. * to try to re-add it. But we do need to add an entry to the chado_example table
  319. * to link the Drupal node with the data in the 'example' table of Chado.
  320. *
  321. * This function is not required if the hook_node_info() does not define
  322. * any custom node types.
  323. *
  324. * @ingroup tripal_example
  325. */
  326. function chado_example_insert($node) {
  327. // be sure to always trim text fields
  328. $node->uniquename = trim($node->uniquename);
  329. $node->description = trim($node->description);
  330. // get the example type record
  331. $type_cv = tripal_get_default_cv('example', 'type_id');
  332. $type = tripal_get_cvterm(array(
  333. 'name' => $node->example_type,
  334. 'cv_id' => $type_cv->cv_id,
  335. ));
  336. // if there is an example_id in the $node object then this must be a sync so
  337. // we can skip adding the example as it is already there, although
  338. // we do need to proceed with the rest of the insert
  339. if (!property_exists($node, 'example_id')) {
  340. // perform the insert using the chado_insert_record function();
  341. $values = array(
  342. 'uniquename' => $node->uniquename,
  343. 'description' => $node->description,
  344. 'type_id' => $type->cvterm_id,
  345. 'organism_id' => $node->organism_id,
  346. );
  347. $example = chado_insert_record('example', $values);
  348. if (!$example) {
  349. drupal_set_message(t('Unable to add example.'), 'warning');
  350. tripal_report_error('tripal_example', TRIPAL_WARNING, 'Insert example: Unable to create example where values: %values',
  351. array('%values' => print_r($values, TRUE)));
  352. return;
  353. }
  354. // get the example_id for linking Drupal node with Chado data
  355. $example_id = $example['example_id'];
  356. // Only add to other chado tables if the base record was inserted properly
  357. if ($example_id > 0) {
  358. // If you implemented the properties form in chado_example_form then you need to
  359. // handle inserting these properties into your chado prop table.
  360. $details = array(
  361. 'property_table' => 'exampleprop', // the name of the prop table
  362. 'base_table' => 'example', // the name of your chado base table
  363. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  364. 'foreignkey_value' => $example_id // the value of the example_id key
  365. );
  366. chado_update_node_form_properties($node, $details);
  367. // If you implemented the dbxrefs form in chado_example_form then you need to
  368. // handle inserting these database references into your chado _dbxref table.
  369. $details = array(
  370. 'linking_table' => 'example_dbxref', // the name of your _dbxref table
  371. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  372. 'foreignkey_value' => $example_id // the value of the example_id key
  373. );
  374. chado_update_node_form_dbxrefs($node, $details);
  375. // If you implemented the relationships form in chado_example_form then you need to
  376. // handle inserting these relationships into your chado _relationship table.
  377. $details = array(
  378. 'relationship_table' => 'example_relationship', // name of the _relationship table
  379. 'foreignkey_value' => $example_id // value of the example_id key
  380. );
  381. chado_update_node_form_relationships($node, $details);
  382. }
  383. }
  384. else {
  385. // the node has an example_id so get it for linking Drupal node with Chado data
  386. $example_id = $node->example_id;
  387. }
  388. // Make sure the entry for this example doesn't already exist in the
  389. // chado_example table if it doesn't exist then we want to add it.
  390. $check_org_id = chado_get_id_from_nid('example', $node->nid);
  391. if (!$check_org_id) {
  392. $record = new stdClass();
  393. $record->nid = $node->nid;
  394. $record->vid = $node->vid;
  395. $record->example_id = $example_id;
  396. drupal_write_record('chado_example', $record);
  397. }
  398. }
  399. /**
  400. * Implementation of hook_update(). This function runs after the
  401. * node has been inserted into the Drupal schema and allows us to
  402. * update the record in Chado.
  403. *
  404. * This function is not required if the hook_node_info() does not define
  405. * any custom node types.
  406. *
  407. * @ingroup tripal_example
  408. */
  409. function chado_example_update($node) {
  410. // be sure to always trim text fields
  411. $node->uniquename = trim($node->uniquename);
  412. $node->description = trim($node->description);
  413. // use the chado_update_record() function to update the record
  414. $match = array(
  415. 'example_id' => $example_id,
  416. );
  417. $values = array(
  418. 'uniquename' => $node->uniquename,
  419. );
  420. $options = array('return_record' => TRUE);
  421. $status = chado_update_record('example', $match, $values, $options);
  422. if (!$status) {
  423. drupal_set_message(t('Unable to update example.'), 'warning');
  424. tripal_report_error('tripal_example', TRIPAL_WARNING, 'Update example: Unable to update example where values: %values',
  425. array('%values' => print_r($values, TRUE)));
  426. }
  427. // If you implemented the properties form in chado_example_form then you need to
  428. // handle updating these properties into your chado prop table.
  429. $details = array(
  430. 'property_table' => 'exampleprop', // the name of the prop table
  431. 'base_table' => 'example', // the name of your chado base table
  432. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  433. 'foreignkey_value' => $example_id // the value of the example_id key
  434. );
  435. chado_update_node_form_properties($node, $details);
  436. // If you implemented the dbxrefs form in chado_example_form then you need to
  437. // handle updating these database references into your chado _dbxref table.
  438. $details = array(
  439. 'linking_table' => 'example_dbxref', // the name of your _dbxref table
  440. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  441. 'foreignkey_value' => $example_id // the value of the example_id key
  442. );
  443. chado_update_node_form_dbxrefs($node, $details);
  444. // If you implemented the relationships form in chado_example_form then you need to
  445. // handle updating these relationships into your chado _relationship table.
  446. $details = array(
  447. 'relationship_table' => 'example_relationship', // name of the _relationship table
  448. 'foreignkey_value' => $example_id // value of the example_id key
  449. );
  450. chado_update_node_form_relationships($node, $details);
  451. }
  452. /**
  453. * Implementation of hook_delete(). This function runs after the
  454. * node has been deleted from the Drupal schema and allows us to
  455. * delete the corresponding recrod in Chado.
  456. *
  457. * This function is not required if the hook_node_info() does not define
  458. * any custom node types.
  459. *
  460. * @ingroup tripal_example
  461. */
  462. function chado_example_delete($node) {
  463. // get the example id from the node
  464. $example_id = chado_get_id_from_nid('example', $node->nid);
  465. // if we don't have a example id for this node then this isn't a node of
  466. // type chado_example or the entry in the chado_example table was lost.
  467. if (!$example_id) {
  468. return;
  469. }
  470. // remove the entry in the chado_exapmle table linking the deleted
  471. // Drupal node with the data in chado
  472. $sql_del = "DELETE FROM {chado_example} WHERE nid = :nid AND vid = :vid";
  473. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  474. // Remove data from example tables of chado database. This will
  475. // cause a cascade delete and remove all data in referencing tables
  476. // for this example
  477. chado_query("DELETE FROM {example} WHERE example_id = :example_id", array(':example_id' => $example_id));
  478. // inform the user that the data was deleted
  479. drupal_set_message(t("The example and all associated data were removed from Chado"));
  480. }
  481. /**
  482. * Implementation of hook_load(). This function is necessary to load
  483. * into the $node object the fields of the table form Chado. For example
  484. * for the example table, the chado_example_load() function adds in
  485. * a example object which contains all of the fields and sub objects
  486. * for data in tables with foreign key relationships.
  487. *
  488. * This function is not required if the hook_node_info() does not define
  489. * any custom node types.
  490. *
  491. * @ingroup tripal_example
  492. */
  493. function chado_example_load($nodes) {
  494. // EXPLANATION: when displaying or node or accessing the node in a template
  495. // we need the data from Chado. This fucntion finds the record in Chado that
  496. // this node belongs to and adds the record.
  497. // there may be multiple nodes that get passed in so we have to iterate through
  498. // them all
  499. foreach ($nodes as $nid => $node) {
  500. // find the example and add in the details
  501. $example_id = chado_get_id_from_nid('example', $nid);
  502. // if the nid does not have a matching record then skip this node.
  503. // this can happen with orphaned nodes.
  504. if (!$example_id) {
  505. continue;
  506. }
  507. // build the example variable by using the chado_generate_var() function
  508. $values = array('example_id' => $example_id);
  509. $example = chado_generate_var('example', $values);
  510. // for fields in the table that are of type 'text' you may want to include those
  511. // by default, the chado_generate_var does not include text fields as
  512. // they may be very large and including a large text field can slow the page load.
  513. // If you know a text field will never be large and it is important for the
  514. // other functions that will see the node to have access to a field you can
  515. // include it here using the chado_expand_var() function. In most
  516. // cases it is probably best to let the end-user decide if text fields should
  517. // be included by using this function in the templates.
  518. $example = chado_expand_var($example, 'field', 'example.description');
  519. // If your module is using the Chado Node: Title & Path API to allow custom titles
  520. // for your node type. Every time you want the title of the node, you need to use the
  521. // following API function:
  522. $node->title = chado_get_node_title($node);
  523. // add the new example object to this node.
  524. $nodes[$nid]->example = $example;
  525. }
  526. }
  527. /**
  528. * Implementation of hook_node_presave().
  529. *
  530. * Performs actions on a node object prior to it being saved
  531. *
  532. * @ingroup tripal_example
  533. */
  534. function tripal_example_node_presave($node) {
  535. // EXPLANATION: This node is useful for
  536. // making changes to the node prior to it being saved to the database.
  537. // One useful case for this is to set the title of a node using values
  538. // supplied by the user.
  539. //
  540. // This function is not required. You probably won't need it if you
  541. // don't define a custom node type in the hook_node_info() function. But
  542. // it is node type agnostic, so you can use this function to change the
  543. // contents of any node regardless of it's type.
  544. // set the node title
  545. switch ($node->type) {
  546. // This step is for setting the title for the Drupal node. This title
  547. // is permanent and thus is created to be unique. Title changes provided
  548. // by tokens are generated on the fly dynamically, but the node title
  549. // seen in the content listing needs to be set here. Do not call
  550. // the chado_get_node_title() function here to set the title as the node
  551. // object isn't properly filled out and the function will fail.
  552. case 'chado_example':
  553. // for a form submission the 'uniquename' field will be set,
  554. // for a sync, we must pull from the example object
  555. if (property_exists($node, 'uniquename')) {
  556. // set the title
  557. $node->title = $node->uniquename;
  558. }
  559. else if (property_exists($node, 'example')) {
  560. $node->title = $node->example->uniquename;
  561. }
  562. break;
  563. }
  564. }
  565. /**
  566. * Implementation of hook node_insert().
  567. *
  568. * Performs actions after any node has been inserted.
  569. *
  570. * @ingroup tripal_example
  571. */
  572. function tripal_example_node_insert($node) {
  573. // EXPLANATION: This function is used
  574. // after any a node is inserted into the database. It is different
  575. // from the hook_insert() function above in that it is called after
  576. // any node is saved, regardlesss of it's type. This function is useful
  577. // for making changes to the database after a node is inserted.
  578. // An example comes from the tripal_feature module where the URL alias
  579. // of a node cannot be set in the hook_insert() function. Therefore
  580. // the tripal_feature module uses this function to set the url path
  581. // of a newly inserted example node.
  582. //
  583. // This function is not required. You probably won't need it if you
  584. // don't define a custom node type in the hook_node_info() function. But
  585. // it is node type agnostic, so you can use this function to do any
  586. // activity after insert of any node.
  587. // the Example code below will set the URL path after inserting. We do it
  588. // here because we do not know the example_id in the presave and cannot do
  589. // it in the hook_insert()
  590. switch ($node->type) {
  591. case 'chado_example':
  592. // find the example and add in the details
  593. $example_id = chado_get_id_from_nid('example', $nid);
  594. // build the example variable by using the chado_generate_var() function
  595. $values = array('example_id' => $example_id);
  596. $example = chado_generate_var('example', $values);
  597. $node->example = $example;
  598. // If your module is using the 'Chado Node: Title & Path API' to allow custom titles
  599. // for your node type. Every time you want the title of the node, you need to use the
  600. // following API function:
  601. $example->title = chado_get_node_title($node);
  602. // set the URL for this example page
  603. // see the code in the tripal_feature/includes/tripal_feature.chado_node.inc file
  604. // in the function tripal_feature_node_insert for an example of how that
  605. // module sets the URL. It uses a configuration file to allow the user
  606. // to dynmically build a URL schema and then uses that schema to generate
  607. // a URL string.
  608. break;
  609. }
  610. }
  611. /**
  612. * Implementation of hook node_update().
  613. *
  614. * Performs actions after any node has been updated.
  615. *
  616. */
  617. function tripal_example_node_update($node) {
  618. // EXPLANATION: This function is used
  619. // after any a node is updated in the database. It is different
  620. // from the hook_update() function above in that it is called after
  621. // any node is updated, regardlesss of it's type.
  622. // An example comes from the tripal_feature module where the URL alias
  623. // of a node cannot be set in the hook_update() function. Therefore
  624. // the tripal_feature module uses this function to reset the url path
  625. // of an updated feature node.
  626. //
  627. // This function is not required. You probably won't need it if you
  628. // don't define a custom node type in the hook_node_info() function. But
  629. // it is node type agnostic, so you can use this function to do any
  630. // activity after insert of a node.
  631. // add items to other nodes, build index and search results
  632. switch ($node->type) {
  633. case 'chado_example':
  634. // If your module is using the Chado Node: Title & Path API to allow custom titles
  635. // for your node type. Every time you want the title of the node, you need to use the
  636. // following API function:
  637. $example->title = chado_get_node_title($node);
  638. // set the URL for this example page
  639. // see the code in the tripal_feature/includes/tripal_feature.chado_node.inc file
  640. // in the function tripal_feature_node_insert for an example of how that
  641. // module sets the URL. It uses a configuration file to allow the user
  642. // to dynmically build a URL schema and then uses that schema to generate
  643. // a URL string.
  644. break;
  645. }
  646. }
  647. /**
  648. * Implementation of hook_node_view().
  649. *
  650. * @ingroup tripal_example
  651. */
  652. function tripal_example_node_view($node, $view_mode, $langcode) {
  653. // EXPLANATION: This function defines the content "blocks" that appear
  654. // when thhe node is displayed. It is node type agnostic so we can add
  655. // content to any node type. So, we use this function to add the content
  656. // from all of our theme templates onto our new node type. We will also
  657. // use this function to add content to other node types.
  658. switch ($node->type) {
  659. case 'chado_example':
  660. // there are different ways a node can be viewed. Primarily Tripal
  661. // supports full page view and teaser view.
  662. if ($view_mode == 'full') {
  663. // If you want to use the default Tripal node template then you need to tell
  664. // tripal to generate the Table of Contents. This is done by setting the following
  665. // to TRUE. If your content type follows the chado_<base table> convention
  666. // then this is the default. In this case if you don't want to use the default
  667. // template then you need to set the following to FALSE.
  668. $node->content['#tripal_generic_node_template'] = TRUE;
  669. // There is always a base template. This is the template that
  670. // is first shown when the example node type is first displayed.
  671. // if you are using the default Tripal node template, then you should
  672. // also set two additional items in each array: tripal_toc_id and
  673. // tripal_toc_title. The tripal_tock_id should be a single unqiue
  674. // world that is used to reference the template. This ID is used for
  675. // constructing URLs for the content. The tripal_toc_title contains
  676. // the title that should appear in the table of contents for this
  677. // content. You should only set the '#weight' element for the
  678. // base template (or Overview) to ensure that it appears at the top of
  679. // the list. Otherwise items are sorted alphabetically.
  680. $node->content['tripal_example_base'] = array(
  681. '#theme' => 'tripal_example_base',
  682. '#node' => $node,
  683. '#tripal_toc_id' => 'base',
  684. '#tripal_toc_title' => 'Overview',
  685. '#weight' => -100,
  686. );
  687. // we can add other templates as well for properties, publications,
  688. // dbxrefs, etc...
  689. $node->content['tripal_example_properties'] = array(
  690. '#theme' => 'tripal_example_properties',
  691. '#node' => $node,
  692. '#tripal_toc_id' => 'properties',
  693. '#tripal_toc_title' => 'Properties',
  694. );
  695. $node->content['tripal_example_references'] = array(
  696. '#theme' => 'tripal_example_references',
  697. '#node' => $node,
  698. '#tripal_toc_id' => 'references',
  699. '#tripal_toc_title' => 'Cross References',
  700. );
  701. $node->content['tripal_example_relationships'] = array(
  702. '#theme' => 'tripal_example_relationships',
  703. '#node' => $node,
  704. '#tripal_toc_id' => 'relationships',
  705. '#tripal_toc_title' => 'Relationships',
  706. );
  707. // Note: if you create a template that you do not want a user to
  708. // know where it is (discourage editing of it), you can add the following
  709. // key: '#tripal_template_show' => FALSE. If this key/value is set
  710. // the the administrator message that Tripal provides indicating
  711. // where the template is housed will not be shown.
  712. }
  713. // set the content for the teaser view
  714. if ($view_mode == 'teaser') {
  715. // The teaser is also a required template
  716. $node->content['tripal_example_teaser'] = array(
  717. '#theme' => 'tripal_example_teaser',
  718. '#node' => $node,
  719. );
  720. }
  721. break;
  722. // you can add custom content to any node type by adding
  723. // content to the node in the same way as above.
  724. case 'chado_organism':
  725. if ($view_mode == 'full') {
  726. $node->content['tripal_organism_examples'] = array(
  727. '#theme' => 'tripal_organism_examples',
  728. '#node' => $node,
  729. '#tripal_toc_id' => 'examples',
  730. '#tripal_toc_title' => 'Examples',
  731. );
  732. }
  733. break;
  734. // ... etc
  735. }
  736. }