tripal.field_storage.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Implements hook_field_storage_info().
  4. *
  5. * The Tripal module does not provide a storage back-end. But it does provide
  6. * a placeholder when no storage backend is needed but a field
  7. * is still desired. The 'tripal_no_storage' backend is used for the
  8. * content_type field which adds a type field to every entity.
  9. */
  10. function tripal_field_storage_info() {
  11. return array(
  12. 'tripal_no_storage' => array(
  13. 'label' => t('Tripal'),
  14. 'description' => t('The NULL storage is a placeholder for field values
  15. that are not stored in any storage backend (e.g. entity types).'),
  16. 'settings' => array(),
  17. ),
  18. );
  19. }
  20. /**
  21. * Implements hook_field_storage_load().
  22. *
  23. * Responsible for loading the fields from the Chado database and adding
  24. * their values to the entity.
  25. */
  26. function tripal_field_storage_load($entity_type, $entities, $age,
  27. $fields, $options) {
  28. $load_current = $age == FIELD_LOAD_CURRENT;
  29. global $language;
  30. $langcode = $language->language;
  31. foreach ($entities as $id => $entity) {
  32. // Iterate through the entity's fields so we can get the column names
  33. // that need to be selected from each of the tables represented.
  34. $tables = array();
  35. foreach ($fields as $field_id => $ids) {
  36. // By the time this hook runs, the relevant field definitions have been
  37. // populated and cached in FieldInfo, so calling field_info_field_by_id()
  38. // on each field individually is more efficient than loading all fields in
  39. // memory upfront with field_info_field_by_ids().
  40. $field = field_info_field_by_id($field_id);
  41. $field_name = $field['field_name'];
  42. $field_type = $field['type'];
  43. $field_module = $field['module'];
  44. // Allow the creating module to alter the value if desired. The
  45. // module should do this if the field has any other form elements
  46. // that need populationg besides the value which was set above.
  47. module_load_include('inc', $field_module, 'includes/fields/' . $field_type);
  48. if (class_exists($field_type)) {
  49. $field_type::load($field, $entity);
  50. }
  51. } // end: foreach ($fields as $field_id => $ids) {
  52. } // end: foreach ($entities as $id => $entity) {
  53. }
  54. /**
  55. * Implements hook_field_storage_query().
  56. *
  57. * Used by EntityFieldQuery to find the entities having certain field values.
  58. *
  59. * We do not support use of the EntityFieldQuery API for Tripal based fields
  60. * because EFQ doesn't support when multiple storage backends are used. Instead
  61. * use the TripalFieldQuery class and implement the hook_storage_tquery()
  62. * function.
  63. */
  64. function tripal_field_storage_query($query) {
  65. }
  66. /**
  67. * Implements hook_field_storage_tquery().
  68. *
  69. * Used by TripalFieldQuery to find the entities having certain field values.
  70. *
  71. * @param $conditions
  72. */
  73. function tripal_field_storage_tquery($conditions) {
  74. $filter = array();
  75. $entity_ids = array();
  76. foreach ($conditions as $index => $condition) {
  77. $field = $condition['field'];
  78. if ($field['storage']['type'] != 'tripal_no_storage') {
  79. continue;
  80. }
  81. $field_type = $field['type'];
  82. $field_module = $field['module'];
  83. $settings = $field['settings'];
  84. $operator = $condition['operator'];
  85. // This module only supports one field, so we can just perform the filter.
  86. // using the appropriate operator.
  87. $query = db_select('tripal_entity', 'TE');
  88. $query->join('chado_entity', 'CE', 'TE.id = CE.entity_id');
  89. $query->fields('CE', array('entity_id'));
  90. $query->condition('TE.term_id', $condition['value'], $operator);
  91. $results = $query->execute();
  92. $filter_ids = array();
  93. while ($entity_id = $results->fetchField()) {
  94. $filter_ids[] = $entity_id;
  95. }
  96. // Take the intersection of IDs in this filter with those in the
  97. // final $entity_ids;
  98. if (count($entity_ids) == 0) {
  99. $entity_ids = $filter_ids;
  100. }
  101. else {
  102. $entity_ids = array_intersect($entity_ids, $filter_ids);
  103. }
  104. }
  105. return $entity_ids;
  106. }