tripal_example.chado_node.inc 34 KB

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