tripal_entities.field_storage.inc 4.2 KB

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