custom_data_loader.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. Creating Custom Data Loaders
  2. ==============================
  3. .. note::
  4. This documentation is currently being developed.
  5. Youtube link for this tutorial:
  6. The `TripalImporter` class can be extended to create your own data loader.
  7. Static Variables
  8. -----------------
  9. Your importer should overwrite any of the ``public static`` variables that should be different from the default.
  10. .. note::
  11. For the sake of simplicity, we do not override many of the default settings, and we do not include the full inline code documentation. Please see the class documentation for a full list of options.
  12. .. code-block:: php
  13. /**
  14. * @see TripalImporter
  15. */
  16. public static $name = 'Example TST File Importer';
  17. public static $machine_name = 'tripal_tst_loader';
  18. public static $description = 'Loads TST files';
  19. public static $file_types = ['txt', 'tst', 'csv'];
  20. public static $upload_description = 'TST is a fictional format. Its a 2-column, CSV file. The columns should be of the form featurename, and text';
  21. public static $methods = [
  22. // Allow the user to upload a file to the server.
  23. 'file_upload' => TRUE,
  24. // Allow the user to provide the path on the Tripal server for the file.
  25. 'file_local' => TRUE,
  26. // Allow the user to provide a remote URL for the file.
  27. 'file_remote' => TRUE,
  28. ];
  29. The variables that are ``private static`` **should not** be changed.
  30. Form Components
  31. -----------------
  32. There are three standard Drupal form hooks: ``form``, ``form_validate``, ``form_submit``. The TripalImporter deals with these for us as ``form`` and ``formValidate``: typically the base class's ``formSubmit`` does not need to be modified.
  33. .. note::
  34. Please see the Drupal documentation for the Form API reference, available `here for Drupal 7 <https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x>`_. This tutorial will only scratch the surface of the Form API.
  35. form
  36. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  37. This function will provide all of the input widgets required for the user to run the form. The global settings above ::ref:`<Static Variables>`_ provide some elements "out of the box". A totally empty TripalImporter class can provide the tow below components: the **files** section, and an **analysis** selector.
  38. The **File Upload** area lets users choose to upload a file manually using the interface, or, to provide a **Server path** or **Remote path** for the file.
  39. .. image:: ./custom_data_loader.1.oob_file_interface.png
  40. .. image:: ./custom_data_loader.2.oob_analysis_select.png
  41. Our overly simplistic TST reader example only needs to do one thing: let the user pick a CVterm. The importer will then read the file, split it into feature and values, and insert into featureprop using the ``type_id`` the user specified in the form.
  42. Our form might therefore be something as simple as this:
  43. .. code-block:: php
  44. :name: ExampleImporter::form
  45. public function form($form, &$form_state) {
  46. $options = [];
  47. #an array of random sequence ontology terms the user can select from.
  48. $terms = [array('id' => 'SO:0000235'), ['id' => 'SO:0000238'], ['id' => 'SO:0000248'] ];
  49. $options[0] = '--please select an option--';
  50. foreach ($terms as $term){
  51. $term_object = chado_get_cvterm($term);
  52. $id = $term_object->cvterm_id;
  53. $options[$id] = $term_object->name;
  54. }
  55. $form['pick_cvterm'] = [
  56. '#title' => 'CVterm',
  57. '#description' => 'Please pick a CVterm. The loaded TST file will associate the values with this term as a feature property.',
  58. '#type' => 'select',
  59. '#default_value' => '0',
  60. '#options' => $options
  61. ];
  62. return $form;
  63. }
  64. Our form now has a select box!
  65. .. image:: ./custom_data_loader.3.cvterm_select.png
  66. What about responsive form elements?
  67. """""""""""""""""""""""""""""""""""""
  68. .. note::
  69. This section coming soon. For now, check out the Drupal AJAX guide https://api.drupal.org/api/drupal/includes%21ajax.inc/group/ajax/7.x
  70. formValidate
  71. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  72. This function is responsible for verifying that required fields are filled out, and that supplied values are valid. If something is invalid, use ``form_set_error()`` provide an error message and Drupal will mark that piece of the form in red.
  73. In our example code, we should check that the user picked a CVterm in the ``pick_cvterm`` element.
  74. .. code-block:: php
  75. public function formValidate($form, &$form_state) {
  76. parent::formValidate($form, $form_state);
  77. $chosen_cvterm = $form_state['values']['pick_cvterm'];
  78. if ($chosen_cvterm == 0) {
  79. form_set_error('pick_cvterm', 'Please choose a CVterm.');
  80. }
  81. }
  82. This very simple validation function looks for the ``pick_cvterm`` element of the ``$form_state`` and ensures the user selected something. Your own validation may be more complex (for example, ensuring a regular expression is valid, or that a term exists in the database) but the principle will be the same.
  83. Importer Logic
  84. ---------------
  85. run
  86. ^^^^^^^^^^^^
  87. If ``formValidate`` did not encounter any ``form_set_error``, the importers ``run`` function will execute. Between the ``formValidate`` and the ``run``, other things have happened: for example, the file was downloaded if a remote URL was given.
  88. The run function should collect the arguments from the importer, and perform the logic of loading your file.
  89. .. code-block:: php
  90. /**
  91. * @see TripalImporter::run()
  92. */
  93. public function run() {
  94. $arguments = $this->arguments['run_args'];
  95. $file_path = $this->arguments['files'][0]['file_path'];
  96. $analysis_id = $arguments['analysis_id'];
  97. $cvterm = $arguments['pick_cvterm'];
  98. $this->loadMyFile($analysis_id, $file_path, $cvterm);
  99. }
  100. Loading the File
  101. ^^^^^^^^^^^^^^^^^^
  102. Testing Importers
  103. ------------------