tripal.field_storage.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. tripal_load_include_field_class($field_type);
  48. if (class_exists($field_type)) {
  49. $tfield = new $field_type($field);
  50. $tfield->load($entity);
  51. }
  52. } // end: foreach ($fields as $field_id => $ids) {
  53. } // end: foreach ($entities as $id => $entity) {
  54. }
  55. /**
  56. * Implements hook_field_storage_query().
  57. */
  58. function tripal_field_storage_query($query) {
  59. $filter = array();
  60. $entity_ids = array();
  61. // Create the initial query.
  62. $select = db_select('tripal_entity', 'TE');
  63. $select->join('tripal_bundle', 'TB', 'TE.bundle = TB.name');
  64. $select->fields('TE', array('id'));
  65. $select->fields('TB', array('name'));
  66. // Add in any filters to the query.
  67. foreach ($query->fieldConditions as $index => $condition) {
  68. $field = $condition['field'];
  69. // Skip conditions that don't belong to this storage type.
  70. if ($field['storage']['type'] != 'tripal_no_storage') {
  71. continue;
  72. }
  73. $value = $condition['value'];
  74. $operator = $condition['operator'] ? $condition['operator'] : '=';
  75. // Filtering on the content type is filtering on the label.
  76. if ($field['field_name'] == 'content_type') {
  77. $select->condition('TB.label', $value, $operator);
  78. }
  79. }
  80. // Add in any sorting to the query.
  81. foreach ($query->order as $index => $sort) {
  82. $field = $sort['specifier']['field'];
  83. // Skip sorts that don't belong to this storage type.
  84. if ($field['storage']['type'] != 'tripal_no_storage') {
  85. continue;
  86. }
  87. $direction = $sort['direction'];
  88. // Filtering on the content type is a filter using the label
  89. if ($field['field_name'] == 'content_type') {
  90. $select->orderBy('TB.label', $direction);
  91. }
  92. }
  93. // Perform the query and return the results.
  94. $entities = $select->execute();
  95. $result = array(
  96. 'TripalEntity' => array(),
  97. );
  98. while ($entity = $entities->fetchObject()) {
  99. $ids = array($entity->id, '0', $entity->name);
  100. $result['TripalEntity'][$entity->id] = entity_create_stub_entity('TripalEntity', $ids);
  101. }
  102. return $result;
  103. }