tripal_example.chado_node.inc

This file should contain all Drupal hooks for interacting with nodes.

File

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