exporting_field_settings.rst 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. Exporting Field Settings With Drupal Features
  2. ================================================
  3. This guide will demonstrate using the `Drupal Features <https://www.drupal.org/docs/7/modules/features>`_ to export and import Tripal Bundle Field settings.
  4. Why Drupal Features?
  5. ---------------------
  6. Consider a case where we have a development site where we configure a bundle (let's say the vanilla Analysis bundle) to have a custom set of Tripal Panes, with fields carefully reorganized into the panes. In particular, we attach a Drupal File field to it, so we can host FASTA files easily.
  7. Once we've configured the field settings, how do we get them to the live site? One option is to open our field configuration admin UI on both sites, and copy the details one at a time. This method is `time consuming and error prone <https://www.drupal.org/docs/7/modules/features/features-moving-site-configuration-to-code>`_, although it is relatively safe: we aren't liable to accidentally break our database this way. Alternatively, trying to transfer via writing database exports is dangerous, and liable to accidentally break our site.
  8. Is there a better way? By exporting the field configuration as a feature!
  9. .. note::
  10. Drupal 8 has a `feature designed to handle development deployment <https://www.phase2technology.com/blog/drupal-8-configuration-management>`_: Configuration Management! Drupal Features remains relevant for sharing field configurations across sites, so this guide may remain useful when Tripal moves to Drupal 8.
  11. .. warning::
  12. Note that I'm going to talk about features a lot in this post. **In the context of the Feature module, features are exported configurations within your site**. It's important that we don't confuse this with Chado features, which are genes, mRNAs, polypeptides, etc.
  13. Version control
  14. ~~~~~~~~~~~~~~~~
  15. Because features get exported as their own module, this means you can treat them as such. You can initialize a git repo, store them on GitHub, make discrete versions, and in general benefit from version control for something which otherwise would only be done via the UI.
  16. How many Features?
  17. ~~~~~~~~~~~~~~~~~~~
  18. The features documentation links to a great article about `how to manage multiple features <http://kerasai.com/blog/2014/04/08/organizing-features-configuration-managment>`_. The suggestion that each bundle be its own module is particularly helpful for Tripal, since we have so many bundles and its a reasonable, discrete way to manage and deploy configuration. This means that mRNA and gene should be separate feature modules even though they are both ``chado.feature`` content types.
  19. A Hazard: Mapping bundle IDs
  20. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. The main hurdle to overcome when mapping features is converting the field machine names across site. This is because each field instance is specific to the bundle it's attached to, and bundle machine-names are from the bundle ID. We can't assume bundle IDs are consistent across sites!
  22. So what do we do? Interestingly, roles have a similar problem, and a guide is `available <https://www.drupal.org/docs/7/modules/features/exportables-and-user-role-ids-in-features>`_ for dealing with them.
  23. The general strategy is:
  24. - remove the exported id value from the ``features.inc`` file
  25. - use hook alter to fetch the ID on the target deployment setup
  26. In our example below, the bundle ID's match on our site. For default Tripal bundles, this should generally be the case.
  27. Drupal Features Tutorial
  28. -------------------------
  29. In this tutorial, we'll add a FASTA file field to the Analysis bundle, and export the configuration.
  30. Configuring the bundle fields
  31. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  32. To begin, let's quickly configure a bundle. Navigate to the structure of your site and select Analysis (**Admin ->Structure -> Tripal Content -> Analysis -> Manage Fields**). Scroll to the bottom and add a new field of type File, with a machine name of ``field_fasta_file‎``, and click **Save**. Be sure to change the **Allowed extensions** parameter to accept ``.fasta``, otherwise, it will only allow `.txt` files to be uploaded. You may also want to increase the file size limit, as the default 2MB can be too small for many FASTA files.
  33. We now have to pick a CV term for our field. Because we are providing a FASTA file field, we can use a term such as FASTA `(SWO:0000142 <https://www.ebi.ac.uk/ols/ontologies/ero/terms?iri=http%3A%2F%2Fwww.ebi.ac.uk%2Fefo%2Fswo%2FSWO_0000142>`_. Please see :ref:`adding_a_cvterm` for help loading a term. Once the term is in our DB, we can assign it to this field.
  34. .. image:: ./exporting_field_settings.1.assign_term.png
  35. Now lets configure our field display. Click the **Manage Display Tab** at the top, and create and enable a "Files" Tripal Pane, placing our new field in the Pane.
  36. .. image:: ./exporting_field_settings.2.png
  37. You can verify your new field is enabled and working by creating a new Analysis and inspecting the "FASTA file" field.
  38. .. image:: ./exporting_field_settings.3.png
  39. Exporting the bundle field displays
  40. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  41. Once we are happy with our bundle field configuration, we can export the display settings using the Drupal Features module.
  42. First, we enable the Features module using drush: ``drush pm-enable features -y``. This adds a Features area under **Admin -> Structure**. Navigate there and choose **Create Feature**.
  43. The field information we're looking for is in **Field Bases**, **Field Group**, and **Field Instances**. We can search for FASTA to find the field base and instance, and "files" (the name of our group) to find the field group.
  44. .. note::
  45. Both **Field Bases** and **Field Instances** will contain the machine name of the field you want to export. **Field Bases** contains the site-wide information for a field and **Field Instances** contains the bundle-specific (i.e. Tripal Content Type) settings.
  46. **Field Group** will contain the machine name of the Tripal Pane and allows you to export the grouping settings you set on the **Manage Display Tab**.
  47. I've also specified a custom path to keep all my Tripal features together under advanced options.
  48. .. image:: ./exporting_field_settings.4.png
  49. If we download and unzip our feature module, we can see it includes all the trappings of a Drupal module.
  50. .. image:: ./exporting_field_settings.5.png
  51. .. warning::
  52. As you can see, it makes the assumption that ``bio_data_2``, the bundle ID for Analysis on our source site, is the correct bundle to configure fields for. However, Tripal makes no guarantee that will hold true on our target site. One solution would be to manually relabel ``bio_data_x`` to the correct bundle ID. On a smaller scale, this is a reasonable solution. If you aren't sure what your bundle ID is, look in the URL when configuring the fields for it: my constructed URL for example was ``admin/structure/bio_data/manage/bio_data_2/fields``.
  53. In our case, the site we want to import to has the same Analysis bundle ID, so no further action is necessary.
  54. Importing the feature configuration
  55. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  56. Go to our target site, all we need to do is download and unpack the ``.tar`` file we generated and enable the module (assuming the bundle ID issue is addressed). I downloaded my file to ``/var/www/html/sites/all/modules/custom/analysis_configuration.tar``, decompressed it (``tar -xvf analysis_configuration.tar``), and enabled it (``drush pm-enable tripal_configuration``).
  57. The field should now appear when you go to create a new analysis on your target site. To check for yourself, create a new Analysis with dummy information: you'll be able to upload a file for the new file field.
  58. Unfortunately, the field still gets imported **disabled** due to Tripal preference, so we have to go to the display settings on our target site and enable the tripal pane/field.
  59. .. image:: ./exporting_field_settings.6.png
  60. Drag the disabled Tripal pane/field group out of the disabled area, click save, and re-visit your newly created Analysis. The files pane and uploaded FASTA file will now appear.
  61. .. image:: ./exporting_field_settings.7.png