tripal_entities.field_storage.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Implements hook_field_storage_info().
  4. */
  5. function tripal_entities_field_storage_info() {
  6. return array(
  7. 'tripal_entities_storage' => array(
  8. 'label' => t('Chado storage'),
  9. 'description' => t('Stores fields in the local Chado database.'),
  10. 'settings' => array(),
  11. ),
  12. );
  13. }
  14. /**
  15. * Implements hook_field_storage_write().
  16. */
  17. function tripal_entities_field_storage_write($entity_type, $entity, $op, $fields) {
  18. // Get the IDs for this entity.
  19. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  20. // Find out which table should receive the insert.
  21. $tablename = 'feature';
  22. $type_field = 'type_id';
  23. $schema = chado_get_schema($tablename);
  24. $pkey_field = $schema['primary key'][0];
  25. // Construct the values array that will be used to insert into the table.
  26. $values = array();
  27. foreach ($fields as $field_id) {
  28. $field = field_info_field_by_id($field_id);
  29. $field_name = $field['field_name'];
  30. $matches = array();
  31. if (preg_match('/^' . $tablename . '__(.*)/', $field_name, $matches)) {
  32. $chado_field = $matches[1];
  33. // Currently, we only support one language, but for for the sake of
  34. // thoroughness we'll iterate through all possible languages.
  35. $all_languages = field_available_languages($entity_type, $field);
  36. $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
  37. foreach ($field_languages as $langcode) {
  38. $items = (array) $entity->{$field_name}[$langcode];
  39. // The number of items is related to the cardinatily of the field.
  40. foreach ($items as $delta => $item) {
  41. $values[$chado_field] = $item['value'];
  42. }
  43. }
  44. }
  45. }
  46. // Add in the type_id field.
  47. $values[$type_field] = $entity->cvterm_id;
  48. switch ($op) {
  49. case FIELD_STORAGE_INSERT:
  50. $record = chado_insert_record($tablename, $values);
  51. if ($record === FALSE) {
  52. drupal_set_message('Could not insert Chado record.', 'error');
  53. }
  54. $entity->record_id = $record[$pkey_field];
  55. break;
  56. case FIELD_STORAGE_UPDATE:
  57. $match[$pkey_field] = $entity->record_id;
  58. chado_update_record($tablename, $match, $values);
  59. break;
  60. }
  61. }
  62. /**
  63. * Implements hook_field_storage_load().
  64. *
  65. * Responsible for loading the fields from the Chado database and adding
  66. * their values to the entity.
  67. */
  68. function tripal_entities_field_storage_load($entity_type, $entities, $age, $fields, $options) {
  69. $load_current = $age == FIELD_LOAD_CURRENT;
  70. global $language;
  71. $langcode = $language->language;
  72. foreach ($entities as $id => $entity) {
  73. // Find out which table should receive the insert.
  74. $tablename = 'feature';
  75. $type_field = 'type_id';
  76. $schema = chado_get_schema($tablename);
  77. $pkey_field = $schema['primary key'][0];
  78. $record_id = $entity->record_id;
  79. // Iterate through the field names to get the list of tables and fields
  80. // that should be queried.
  81. $columns = array();
  82. foreach ($fields as $field_id => $ids) {
  83. // By the time this hook runs, the relevant field definitions have been
  84. // populated and cached in FieldInfo, so calling field_info_field_by_id()
  85. // on each field individually is more efficient than loading all fields in
  86. // memory upfront with field_info_field_by_ids().
  87. $field = field_info_field_by_id($field_id);
  88. $field_name = $field['field_name'];
  89. $matches = array();
  90. if (preg_match('/^(.*?)__(.*?)$/', $field_name, $matches)) {
  91. $table = $matches[1];
  92. $field = $matches[2];
  93. $columns[$table][] = $field;
  94. }
  95. }
  96. // Get the record
  97. $record = chado_select_record($tablename, $columns[$tablename], array($pkey_field => $entity->record_id));
  98. // Now set the field values
  99. foreach ($fields as $field_id => $ids) {
  100. $field = field_info_field_by_id($field_id);
  101. $field_name = $field['field_name'];
  102. $matches = array();
  103. if (preg_match('/^(.*?)__(.*?)$/', $field_name, $matches)) {
  104. $table = $matches[1];
  105. $field = $matches[2];
  106. $entity->{$field_name}['und'][] = array('value' => $record[0]->$field);
  107. }
  108. }
  109. }
  110. }