tripal_example.chado_node.inc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. // if this is a delete then don't validate
  268. if($node->op == 'Delete') {
  269. return;
  270. }
  271. // we are syncing if we do not have a node ID but we do have a example_id. We don't
  272. // need to validate during syncing so just skip it.
  273. if (is_null($node->nid) and property_exists($node, 'example_id') and $node->example_id != 0) {
  274. return;
  275. }
  276. // be sure to always trim text fields
  277. $node->uniquename = trim($node->uniquename);
  278. $node->description = trim($node->description);
  279. // Validating for an update. If the 'nid' property is present in the node then
  280. // this is an update and validation can be different for updates
  281. if (property_exists($node, 'nid')) {
  282. // make sure the feature type is an allowed term
  283. $type_cv = tripal_get_default_cv('example', 'type_id');
  284. $type = tripal_get_cvterm(array(
  285. 'name' => $node->example_type,
  286. 'cv_id' => $type_cv->cv_id,
  287. ));
  288. if (!$type) {
  289. form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
  290. }
  291. // TODO: also we should check that the unique constraint is not invalidated by
  292. // changing either the type_id, organism_id or uniquename.
  293. }
  294. // Validating for an insert
  295. else {
  296. // make sure the feature type is an allowed term
  297. $type_cv = tripal_get_default_cv('example', 'type_id');
  298. $type = tripal_get_cvterm(array(
  299. 'name' => $node->example_type,
  300. 'cv_id' => $type_cv->cv_id,
  301. ));
  302. if (!$type) {
  303. form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
  304. }
  305. // TODO: also we should check that the unique constraint doesn't already exist
  306. }
  307. }
  308. /**
  309. * Implementation of hook_insert(). This function is called after the
  310. * node is inserted into the database. We need it so that we can insert
  311. * appropriate fields as provided by the user into the database. And so that
  312. * we can link the new Drupal node to the data in Chado via the chado_example
  313. * linking table. We can get to this function also during "syncing".
  314. * With syncing, however, the data already exists in Chado and we do not want
  315. * to try to re-add it. But we do need to add an entry to the chado_example table
  316. * to link the Drupal node with the data in the 'example' table of Chado.
  317. *
  318. * This function is not required if the hook_node_info() does not define
  319. * any custom node types.
  320. *
  321. * @ingroup tripal_example
  322. */
  323. function chado_example_insert($node) {
  324. // be sure to always trim text fields
  325. $node->uniquename = trim($node->uniquename);
  326. $node->description = trim($node->description);
  327. // get the example type record
  328. $type_cv = tripal_get_default_cv('example', 'type_id');
  329. $type = tripal_get_cvterm(array(
  330. 'name' => $node->example_type,
  331. 'cv_id' => $type_cv->cv_id,
  332. ));
  333. // if there is an example_id in the $node object then this must be a sync so
  334. // we can skip adding the example as it is already there, although
  335. // we do need to proceed with the rest of the insert
  336. if (!property_exists($node, 'example_id')) {
  337. // perform the insert using the chado_insert_record function();
  338. $values = array(
  339. 'uniquename' => $node->uniquename,
  340. 'description' => $node->description,
  341. 'type_id' => $type->cvterm_id,
  342. 'organism_id' => $node->organism_id,
  343. );
  344. $example = chado_insert_record('example', $values);
  345. if (!$example) {
  346. drupal_set_message(t('Unable to add example.'), 'warning');
  347. tripal_report_error('tripal_example', TRIPAL_WARNING, 'Insert example: Unable to create example where values: %values',
  348. array('%values' => print_r($values, TRUE)));
  349. return;
  350. }
  351. // get the example_id for linking Drupal node with Chado data
  352. $example_id = $example['example_id'];
  353. // Only add to other chado tables if the base record was inserted properly
  354. if ($example_id > 0) {
  355. // If you implemented the properties form in chado_example_form then you need to
  356. // handle inserting these properties into your chado prop table.
  357. $details = array(
  358. 'property_table' => 'exampleprop', // the name of the prop table
  359. 'base_table' => 'example', // the name of your chado base table
  360. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  361. 'foreignkey_value' => $example_id // the value of the example_id key
  362. );
  363. chado_update_node_form_properties($node, $details);
  364. // If you implemented the dbxrefs form in chado_example_form then you need to
  365. // handle inserting these database references into your chado _dbxref table.
  366. $details = array(
  367. 'linking_table' => 'example_dbxref', // the name of your _dbxref table
  368. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  369. 'foreignkey_value' => $example_id // the value of the example_id key
  370. );
  371. chado_update_node_form_dbxrefs($node, $details);
  372. // If you implemented the relationships form in chado_example_form then you need to
  373. // handle inserting these relationships into your chado _relationship table.
  374. $details = array(
  375. 'relationship_table' => 'example_relationship', // name of the _relationship table
  376. 'foreignkey_value' => $example_id // value of the example_id key
  377. );
  378. chado_update_node_form_relationships($node, $details);
  379. }
  380. }
  381. else {
  382. // the node has an example_id so get it for linking Drupal node with Chado data
  383. $example_id = $node->example_id;
  384. }
  385. // Make sure the entry for this example doesn't already exist in the
  386. // chado_example table if it doesn't exist then we want to add it.
  387. $check_org_id = chado_get_id_from_nid('example', $node->nid);
  388. if (!$check_org_id) {
  389. $record = new stdClass();
  390. $record->nid = $node->nid;
  391. $record->vid = $node->vid;
  392. $record->example_id = $example_id;
  393. drupal_write_record('chado_example', $record);
  394. }
  395. }
  396. /**
  397. * Implementation of hook_update(). This function runs after the
  398. * node has been inserted into the Drupal schema and allows us to
  399. * update the record in Chado.
  400. *
  401. * This function is not required if the hook_node_info() does not define
  402. * any custom node types.
  403. *
  404. * @ingroup tripal_example
  405. */
  406. function chado_example_update($node) {
  407. // be sure to always trim text fields
  408. $node->uniquename = trim($node->uniquename);
  409. $node->description = trim($node->description);
  410. // use the chado_update_record() function to update the record
  411. $match = array(
  412. 'example_id' => $example_id,
  413. );
  414. $values = array(
  415. 'uniquename' => $node->uniquename,
  416. );
  417. $options = array('return_record' => TRUE);
  418. $status = chado_update_record('example', $match, $values, $options);
  419. if (!$status) {
  420. drupal_set_message(t('Unable to update example.'), 'warning');
  421. tripal_report_error('tripal_example', TRIPAL_WARNING, 'Update example: Unable to update example where values: %values',
  422. array('%values' => print_r($values, TRUE)));
  423. }
  424. // If you implemented the properties form in chado_example_form then you need to
  425. // handle updating these properties into your chado prop table.
  426. $details = array(
  427. 'property_table' => 'exampleprop', // the name of the prop table
  428. 'base_table' => 'example', // the name of your chado base table
  429. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  430. 'foreignkey_value' => $example_id // the value of the example_id key
  431. );
  432. chado_update_node_form_properties($node, $details);
  433. // If you implemented the dbxrefs form in chado_example_form then you need to
  434. // handle updating these database references into your chado _dbxref table.
  435. $details = array(
  436. 'linking_table' => 'example_dbxref', // the name of your _dbxref table
  437. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  438. 'foreignkey_value' => $example_id // the value of the example_id key
  439. );
  440. chado_update_node_form_dbxrefs($node, $details);
  441. // If you implemented the relationships form in chado_example_form then you need to
  442. // handle updating these relationships into your chado _relationship table.
  443. $details = array(
  444. 'relationship_table' => 'example_relationship', // name of the _relationship table
  445. 'foreignkey_value' => $example_id // value of the example_id key
  446. );
  447. chado_update_node_form_relationships($node, $details);
  448. }
  449. /**
  450. * Implementation of hook_delete(). This function runs after the
  451. * node has been deleted from the Drupal schema and allows us to
  452. * delete the corresponding recrod in Chado.
  453. *
  454. * This function is not required if the hook_node_info() does not define
  455. * any custom node types.
  456. *
  457. * @ingroup tripal_example
  458. */
  459. function chado_example_delete($node) {
  460. // get the example id from the node
  461. $example_id = chado_get_id_from_nid('example', $node->nid);
  462. // if we don't have a example id for this node then this isn't a node of
  463. // type chado_example or the entry in the chado_example table was lost.
  464. if (!$example_id) {
  465. return;
  466. }
  467. // remove the entry in the chado_exapmle table linking the deleted
  468. // Drupal node with the data in chado
  469. $sql_del = "DELETE FROM {chado_example} WHERE nid = :nid AND vid = :vid";
  470. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  471. // Remove data from example tables of chado database. This will
  472. // cause a cascade delete and remove all data in referencing tables
  473. // for this example
  474. chado_query("DELETE FROM {example} WHERE example_id = :example_id", array(':example_id' => $example_id));
  475. // inform the user that the data was deleted
  476. drupal_set_message(t("The example and all associated data were removed from Chado"));
  477. }
  478. /**
  479. * Implementation of hook_load(). This function is necessary to load
  480. * into the $node object the fields of the table form Chado. For example
  481. * for the example table, the chado_example_load() function adds in
  482. * a example object which contains all of the fields and sub objects
  483. * for data in tables with foreign key relationships.
  484. *
  485. * This function is not required if the hook_node_info() does not define
  486. * any custom node types.
  487. *
  488. * @ingroup tripal_example
  489. */
  490. function chado_example_load($nodes) {
  491. // EXPLANATION: when displaying or node or accessing the node in a template
  492. // we need the data from Chado. This fucntion finds the record in Chado that
  493. // this node belongs to and adds the record.
  494. // there may be multiple nodes that get passed in so we have to iterate through
  495. // them all
  496. foreach ($nodes as $nid => $node) {
  497. // find the example and add in the details
  498. $example_id = chado_get_id_from_nid('example', $nid);
  499. // if the nid does not have a matching record then skip this node.
  500. // this can happen with orphaned nodes.
  501. if (!$example_id) {
  502. continue;
  503. }
  504. // build the example variable by using the chado_generate_var() function
  505. $values = array('example_id' => $example_id);
  506. $example = chado_generate_var('example', $values);
  507. // for fields in the table that are of type 'text' you may want to include those
  508. // by default, the chado_generate_var does not include text fields as
  509. // they may be very large and including a large text field can slow the page load.
  510. // If you know a text field will never be large and it is important for the
  511. // other functions that will see the node to have access to a field you can
  512. // include it here using the chado_expand_var() function. In most
  513. // cases it is probably best to let the end-user decide if text fields should
  514. // be included by using this function in the templates.
  515. $example = chado_expand_var($example, 'field', 'example.description');
  516. // If your module is using the Chado Node: Title & Path API to allow custom titles
  517. // for your node type. Every time you want the title of the node, you need to use the
  518. // following API function:
  519. $node->title = chado_get_node_title($node);
  520. // add the new example object to this node.
  521. $nodes[$nid]->example = $example;
  522. }
  523. }
  524. /**
  525. * Implementation of hook_node_presave().
  526. *
  527. * Performs actions on a node object prior to it being saved
  528. *
  529. * @ingroup tripal_example
  530. */
  531. function tripal_example_node_presave($node) {
  532. // EXPLANATION: This node is useful for
  533. // making changes to the node prior to it being saved to the database.
  534. // One useful case for this is to set the title of a node. In some cases
  535. // such as for the organism module, the title will be set depending on
  536. // what genus and species is provided. This hook can allow the title to
  537. // be set using user supplied data before the node is saved. In practice
  538. // any change can be made to any fields in the node object.
  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. case 'chado_example':
  547. // for a form submission the 'uniquename' field will be set,
  548. // for a sync, we must pull from the example object
  549. if (property_exists($node, 'uniquename')) {
  550. // set the title
  551. $node->title = $node->uniquename;
  552. }
  553. else if (property_exists($node, 'example')) {
  554. $node->title = $node->example->uniquename;
  555. }
  556. break;
  557. }
  558. }
  559. /**
  560. * Implementation of hook node_insert().
  561. *
  562. * Performs actions after any node has been inserted.
  563. *
  564. * @ingroup tripal_example
  565. */
  566. function tripal_example_node_insert($node) {
  567. // EXPLANATION: This function is used
  568. // after any a node is inserted into the database. It is different
  569. // from the hook_insert() function above in that it is called after
  570. // any node is saved, regardlesss of it's type. This function is useful
  571. // for making changes to the database after a node is inserted.
  572. // An example comes from the tripal_feature module where the URL alias
  573. // of a node cannot be set in the hook_insert() function. Therefore
  574. // the tripal_feature module uses this function to set the url path
  575. // of a newly inserted example node.
  576. //
  577. // This function is not required. You probably won't need it if you
  578. // don't define a custom node type in the hook_node_info() function. But
  579. // it is node type agnostic, so you can use this function to do any
  580. // activity after insert of any node.
  581. // the Example code below will set the URL path after inserting. We do it
  582. // here because we do not know the example_id in the presave and cannot do
  583. // it in the hook_insert()
  584. switch ($node->type) {
  585. case 'chado_example':
  586. // find the example and add in the details
  587. $example_id = chado_get_id_from_nid('example', $nid);
  588. // build the example variable by using the chado_generate_var() function
  589. $values = array('example_id' => $example_id);
  590. $example = chado_generate_var('example', $values);
  591. $node->example = $example;
  592. // If your module is using the 'Chado Node: Title & Path API' to allow custom titles
  593. // for your node type. Every time you want the title of the node, you need to use the
  594. // following API function:
  595. $example->title = chado_get_node_title($node);
  596. // set the URL for this example page
  597. // see the code in the tripal_feature/includes/tripal_feature.chado_node.inc file
  598. // in the function tripal_feature_node_insert for an example of how that
  599. // module sets the URL. It uses a configuration file to allow the user
  600. // to dynmically build a URL schema and then uses that schema to generate
  601. // a URL string.
  602. break;
  603. }
  604. }
  605. /**
  606. * Implementation of hook node_update().
  607. *
  608. * Performs actions after any node has been updated.
  609. *
  610. */
  611. function tripal_example_node_update($node) {
  612. // EXPLANATION: This function is used
  613. // after any a node is updated in the database. It is different
  614. // from the hook_update() function above in that it is called after
  615. // any node is updated, regardlesss of it's type.
  616. // An example comes from the tripal_feature module where the URL alias
  617. // of a node cannot be set in the hook_update() function. Therefore
  618. // the tripal_feature module uses this function to reset the url path
  619. // of an updated feature node.
  620. //
  621. // This function is not required. You probably won't need it if you
  622. // don't define a custom node type in the hook_node_info() function. But
  623. // it is node type agnostic, so you can use this function to do any
  624. // activity after insert of a node.
  625. // add items to other nodes, build index and search results
  626. switch ($node->type) {
  627. case 'chado_example':
  628. // If your module is using the Chado Node: Title & Path API to allow custom titles
  629. // for your node type. Every time you want the title of the node, you need to use the
  630. // following API function:
  631. $example->title = chado_get_node_title($node);
  632. // set the URL for this example page
  633. // see the code in the tripal_feature/includes/tripal_feature.chado_node.inc file
  634. // in the function tripal_feature_node_insert for an example of how that
  635. // module sets the URL. It uses a configuration file to allow the user
  636. // to dynmically build a URL schema and then uses that schema to generate
  637. // a URL string.
  638. break;
  639. }
  640. }
  641. /**
  642. * Implementation of hook_node_view().
  643. *
  644. * @ingroup tripal_example
  645. */
  646. function tripal_example_node_view($node, $view_mode, $langcode) {
  647. // EXPLANATION: This function defines the content "blocks" that appear
  648. // when thhe node is displayed. It is node type agnostic so we can add
  649. // content to any node type. So, we use this function to add the content
  650. // from all of our theme templates onto our new node type. We will also
  651. // use this function to add content to other node types.
  652. switch ($node->type) {
  653. case 'chado_example':
  654. // there are different ways a node can be viewed. Primarily Tripal
  655. // supports full page view and teaser view.
  656. if ($view_mode == 'full') {
  657. // If you want to use the default Tripal node template then you need to tell
  658. // tripal to generate the Table of Contents. This is done by setting the following
  659. // to TRUE. If your content type follows the chado_<base table> convention
  660. // then this is the default. In this case if you don't want to use the default
  661. // template then you need to set the following to FALSE.
  662. $node->content['#tripal_generic_node_template'] = TRUE;
  663. // There is always a base template. This is the template that
  664. // is first shown when the example node type is first displayed.
  665. // if you are using the default Tripal node template, then you should
  666. // also set two additional items in each array: tripal_toc_id and
  667. // tripal_toc_title. The tripal_tock_id should be a single unqiue
  668. // world that is used to reference the template. This ID is used for
  669. // constructing URLs for the content. The tripal_toc_title contains
  670. // the title that should appear in the table of contents for this
  671. // content. You should only set the '#weight' element for the
  672. // base template (or Overview) to ensure that it appears at the top of
  673. // the list. Otherwise items are sorted alphabetically.
  674. $node->content['tripal_example_base'] = array(
  675. '#markup' => theme('tripal_example_base', array('node' => $node)),
  676. '#tripal_toc_id' => 'base',
  677. '#tripal_toc_title' => 'Overview',
  678. '#weight' => -100,
  679. );
  680. // we can add other templates as well for properties, publications,
  681. // dbxrefs, etc...
  682. $node->content['tripal_example_properties'] = array(
  683. '#markup' => theme('tripal_example_properties', array('node' => $node)),
  684. '#tripal_toc_id' => 'properties',
  685. '#tripal_toc_title' => 'Properties',
  686. );
  687. $node->content['tripal_example_references'] = array(
  688. '#markup' => theme('tripal_example_references', array('node' => $node)),
  689. '#tripal_toc_id' => 'references',
  690. '#tripal_toc_title' => 'Cross References',
  691. );
  692. $node->content['tripal_example_relationships'] = array(
  693. '#markup' => theme('tripal_example_relationships', array('node' => $node)),
  694. '#tripal_toc_id' => 'relationships',
  695. '#tripal_toc_title' => 'Relationships',
  696. );
  697. // Note: if you create a template that you do not want a user to
  698. // know where it is (discourage editing of it), you can add the following
  699. // key: '#tripal_template_show' => FALSE. If this key/value is set
  700. // the the administrator message that Tripal provides indicating
  701. // where the template is housed will not be shown.
  702. }
  703. // set the content for the teaser view
  704. if ($view_mode == 'teaser') {
  705. // The teaser is also a required template
  706. $node->content['tripal_example_teaser'] = array(
  707. '#value' => theme('tripal_example_teaser', array('node' => $node)),
  708. );
  709. }
  710. break;
  711. // you can add custom content to any node type by adding
  712. // content to the node in the same way as above.
  713. case 'chado_organism':
  714. if ($view_mode == 'full') {
  715. $node->content['tripal_organism_examples'] = array(
  716. '#markup' => theme('tripal_organism_examples', array('node' => $node)),
  717. '#tripal_toc_id' => 'examples',
  718. '#tripal_toc_title' => 'Examples',
  719. );
  720. }
  721. break;
  722. // ... etc
  723. }
  724. }