sio__vocabulary.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. class sio__vocabulary extends ChadoField {
  3. // The default label for this field.
  4. public static $default_label = 'Vocabulary';
  5. // The default description for this field.
  6. public static $description = 'The vocabulary to which this resource is associated.';
  7. // Provide a list of instance specific settings. These can be accessed within
  8. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  9. // then Drupal will automatically change these settings for the instance.
  10. // It is recommended to put settings at the instance level whenever possible.
  11. // If you override this variable in a child class be sure to replicate the
  12. // term_name, term_vocab, term_accession and term_fixed keys as these are
  13. // required for all TripalFields.
  14. public static $default_instance_settings = [
  15. // The short name for the vocabulary (e.g. schema, SO, GO, PATO, etc.).
  16. 'term_vocabulary' => 'SIO',
  17. // The name of the term.
  18. 'term_name' => 'vocabulary',
  19. // The unique ID (i.e. accession) of the term.
  20. 'term_accession' => '001080',
  21. // Set to TRUE if the site admin is allowed to change the term
  22. // type. This will create form elements when editing the field instance
  23. // to allow the site admin to change the term settings above.
  24. 'term_fixed' => FALSE,
  25. ];
  26. // The default widget for this field.
  27. public static $default_widget = 'sio__vocabulary_widget';
  28. // The default formatter for this field.
  29. public static $default_formatter = 'sio__vocabulary_formatter';
  30. /**
  31. * @see TripalField::validate()
  32. */
  33. public function validate($entity_type, $entity, $langcode, $items, &$errors) {
  34. // If we don't have an entity then we don't want to validate. The case
  35. // where this could happen is when a user is editing the field settings
  36. // and trying to set a default value. In that case there's no entity and
  37. // we don't want to validate. There will always be an entity for creation
  38. // and update operations of a content type.
  39. if (!$entity) {
  40. return;
  41. }
  42. $settings = $this->field['settings'];
  43. $field_name = $this->field['field_name'];
  44. $field_type = $this->field['type'];
  45. $field_table = $this->instance['settings']['chado_table'];
  46. $field_column = $this->instance['settings']['chado_column'];
  47. // Get the field values.
  48. foreach ($items as $delta => $values) {
  49. // Get the field values.
  50. $cv_id = $values['chado-' . $field_table . '__cv_id'];
  51. if (!$cv_id or $cv_id == 0) {
  52. $errors[$field_name]['und'][0][] = [
  53. 'message' => t("Please specify a vocabulary."),
  54. 'error' => 'chado-' . $field_table . '__cv_id',
  55. ];
  56. }
  57. }
  58. }
  59. /**
  60. * @see TripalField::load()
  61. */
  62. public function load($entity) {
  63. $record = $entity->chado_record;
  64. $settings = $this->instance['settings'];
  65. $field_name = $this->field['field_name'];
  66. $field_type = $this->field['type'];
  67. $field_table = $this->instance['settings']['chado_table'];
  68. $field_column = $this->instance['settings']['chado_column'];
  69. // Set some defaults for the empty record.
  70. $entity->{$field_name}['und'][0] = [
  71. 'value' => [],
  72. ];
  73. if ($record) {
  74. $cv = $record->cv_id;
  75. $entity->{$field_name}['und'][0]['value'] = $cv->name;
  76. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__cv_id'] = $cv->cv_id;
  77. }
  78. }
  79. /**
  80. * @see ChadoField::query()
  81. */
  82. public function query($query, $condition) {
  83. $alias = $this->field['field_name'];
  84. $operator = $condition['operator'];
  85. $this->queryJoinOnce($query, 'cv', $alias, "base.cv_id = $alias.cv_id");
  86. $query->condition("$alias.name", $condition['value'], $operator);
  87. }
  88. /**
  89. * @see ChadoField::queryOrder()
  90. */
  91. public function queryOrder($query, $order) {
  92. // If the table hasn't yet been joined then add it.
  93. $joins = $query->getTables();
  94. if (!in_array($this->field['field_name'], $joins)) {
  95. $alias = $this->field['field_name'];
  96. $this->queryJoinOnce($query, 'cv', $alias, "base.cv_id = $alias.cv_id");
  97. $query->orderBy("$alias.name", $order['direction']);
  98. }
  99. }
  100. }