sep__protocol.inc

File

tripal_chado/includes/TripalFields/sep__protocol/sep__protocol.inc
View source
  1. <?php
  2. /**
  3. * @class
  4. * Purpose: Provide a field for Protocol (typically the protocol_id column of a
  5. * Chado table).
  6. *
  7. * Data:
  8. * Assumptions:
  9. */
  10. class sep__protocol extends ChadoField {
  11. // The default label for this field.
  12. public static $default_label = 'Protocol';
  13. // The default description for this field.
  14. public static $default_description = 'The protocol followed to generate this resource.';
  15. // The default widget for this field.
  16. public static $default_widget = 'sep__protocol_widget';
  17. // The default formatter for this field.
  18. public static $default_formatter = 'sep__protocol_formatter';
  19. // The module that manages this field.
  20. public static $module = 'tripal_chado';
  21. // A list of global settings. These can be accessed within the
  22. // globalSettingsForm. When the globalSettingsForm is submitted then
  23. // Drupal will automatically change these settings for all fields.
  24. // Once instances exist for a field type then these settings cannot be
  25. // changed.
  26. public static $default_settings = [
  27. 'storage' => 'field_chado_storage',
  28. // It is expected that all fields set a 'value' in the load() function.
  29. // In many cases, the value may be an associative array of key/value pairs.
  30. // In order for Tripal to provide context for all data, the keys should
  31. // be a controlled vocabulary term (e.g. rdfs:type). Keys in the load()
  32. // function that are supported by the query() function should be
  33. // listed here.
  34. 'searchable_keys' => [],
  35. ];
  36. // Provide a list of instance specific settings. These can be access within
  37. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  38. // then Drupal with automatically change these settings for the instance.
  39. // It is recommended to put settings at the instance level whenever possible.
  40. // If you override this variable in a child class be sure to replicate the
  41. // term_name, term_vocab, term_accession and term_fixed keys as these are
  42. // required for all TripalFields.
  43. public static $default_instance_settings = [
  44. // The DATABASE name, as it appears in chado.db. This also builds the link-out url. In most cases this will simply be the CV name. In some cases (EDAM) this will be the SUBONTOLOGY.
  45. 'term_vocabulary' => 'sep',
  46. // The name of the term.
  47. 'term_name' => 'protocol',
  48. // The unique ID (i.e. accession) of the term.
  49. 'term_accession' => '00101',
  50. // Set to TRUE if the site admin is not allowed to change the term
  51. // type, otherwise the admin can change the term mapped to a field.
  52. 'term_fixed' => FALSE,
  53. // Indicates if this field should be automatically attached to display
  54. // or web services or if this field should be loaded separately. This
  55. // is convenient for speed. Fields that are slow should for loading
  56. // should have auto_attach set to FALSE so tha their values can be
  57. // attached asynchronously.
  58. 'auto_attach' => FALSE,
  59. ];
  60. // A boolean specifying that users should not be allowed to create
  61. // fields and instances of this field type through the UI. Such
  62. // fields can only be created programmatically with field_create_field()
  63. // and field_create_instance().
  64. public static $no_ui = FALSE;
  65. // A boolean specifying that the field will not contain any data. This
  66. // should exclude the field from web services or downloads. An example
  67. // could be a quick search field that appears on the page that redirects
  68. // the user but otherwise provides no data.
  69. public static $no_data = FALSE;
  70. /**
  71. * Loads the field values from the underlying data store.
  72. *
  73. * @param $entity
  74. *
  75. * @return
  76. * An array of the following format:
  77. * $entity->{$field_name}['und'][0]['value'] = $value;
  78. * where:
  79. * - $entity is the entity object to which this field is attached.
  80. * - $field_name is the name of this field
  81. * - 'und' is the language code (in this case 'und' == undefined)
  82. * - 0 is the cardinality. Increment by 1 when more than one item is
  83. * available.
  84. * - 'value' is the key indicating the value of this field. It should
  85. * always be set. The value of the 'value' key will be the contents
  86. * used for web services and for downloadable content. The value
  87. * should be of the follow format types: 1) A single value (text,
  88. * numeric, etc.) 2) An array of key value pair. 3) If multiple entries
  89. * then cardinality should incremented and format types 1 and 2 should
  90. * be used for each item.
  91. * The array may contain as many other keys at the same level as 'value'
  92. * but those keys are for internal field use and are not considered the
  93. * value of the field.
  94. *
  95. *
  96. */
  97. public function load($entity) {
  98. parent::load($entity);
  99. $record = $entity->chado_record;
  100. $settings = $this->instance['settings'];
  101. $field_name = $this->field['field_name'];
  102. $field_type = $this->field['type'];
  103. $field_table = $this->instance['settings']['chado_table'];
  104. $field_column = $this->instance['settings']['chado_column'];
  105. $linker_field = 'chado-' . $field_table . '__protocol_id';
  106. // Set some defaults for the empty record.
  107. $entity->{$field_name}['und'][0] = [
  108. 'value' => [],
  109. ];
  110. if (!$record->protocol_id->protocol_id) {
  111. return;
  112. }
  113. $protocol_id = $record->protocol_id->protocol_id;
  114. $protocol_name = $record->protocol_id->name;
  115. $entity_id = $record->entity_id;
  116. $entity->{$field_name}['und'][0]['value'] = [
  117. "protocol_id" => $protocol_id,
  118. "protocol_name" => $protocol_name,
  119. "entity_id" => $entity_id,
  120. ];
  121. // Is there a published entity for this protocol?
  122. if (property_exists($record->{$field_column}, 'entity_id')) {
  123. $entity->{$field_name}['und'][0]['value']['entity_id'] = 'TripalEntity:' . $record->{$field_column}->entity_id;
  124. }
  125. }
  126. }