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