tripal.field_storage.inc 4.0 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 and adding their values to the entity.
  24. */
  25. function tripal_field_storage_load($entity_type, $entities, $age,
  26. $fields, $options) {
  27. $load_current = $age == FIELD_LOAD_CURRENT;
  28. global $language;
  29. $langcode = $language->language;
  30. foreach ($entities as $id => $entity) {
  31. // Iterate through the entity's fields so we can get the column names
  32. // that need to be selected from each of the tables represented.
  33. $tables = array();
  34. foreach ($fields as $field_id => $ids) {
  35. // By the time this hook runs, the relevant field definitions have been
  36. // populated and cached in FieldInfo, so calling field_info_field_by_id()
  37. // on each field individually is more efficient than loading all fields in
  38. // memory upfront with field_info_field_by_ids().
  39. $field = field_info_field_by_id($field_id);
  40. $field_name = $field['field_name'];
  41. $field_type = $field['type'];
  42. $field_module = $field['module'];
  43. // Get the instnace for this field
  44. $instance = field_info_instance($entity_type, $field_name, $entity->bundle);
  45. // Allow the creating module to alter the value if desired. The
  46. // module should do this if the field has any other form elements
  47. // that need populationg besides the value which was set above.
  48. tripal_load_include_field_class($field_type);
  49. if (class_exists($field_type)) {
  50. $tfield = new $field_type($field, $instance);
  51. $tfield->load($entity);
  52. }
  53. } // end: foreach ($fields as $field_id => $ids) {
  54. } // end: foreach ($entities as $id => $entity) {
  55. }
  56. /**
  57. * Implements hook_field_storage_query().
  58. */
  59. function tripal_field_storage_query($query) {
  60. $filter = array();
  61. $entity_ids = array();
  62. // Create the initial query.
  63. $select = db_select('tripal_entity', 'TE');
  64. $select->join('tripal_bundle', 'TB', 'TE.bundle = TB.name');
  65. $select->fields('TE', array('id'));
  66. $select->fields('TB', array('name'));
  67. // Add in any filters to the query.
  68. foreach ($query->fieldConditions as $index => $condition) {
  69. $field = $condition['field'];
  70. // Skip conditions that don't belong to this storage type.
  71. if ($field['storage']['type'] != 'tripal_no_storage') {
  72. continue;
  73. }
  74. $value = $condition['value'];
  75. $operator = $condition['operator'] ? $condition['operator'] : '=';
  76. // Filtering on the content type is filtering on the label.
  77. if ($field['field_name'] == 'content_type') {
  78. $select->condition('TB.label', $value, $operator);
  79. }
  80. }
  81. // Add in any sorting to the query.
  82. foreach ($query->order as $index => $sort) {
  83. $field = $sort['specifier']['field'];
  84. // Skip sorts that don't belong to this storage type.
  85. if ($field['storage']['type'] != 'tripal_no_storage') {
  86. continue;
  87. }
  88. $direction = $sort['direction'];
  89. // Filtering on the content type is a filter using the label
  90. if ($field['field_name'] == 'content_type') {
  91. $select->orderBy('TB.label', $direction);
  92. }
  93. }
  94. // Perform the query and return the results.
  95. $entities = $select->execute();
  96. $result = array(
  97. 'TripalEntity' => array(),
  98. );
  99. while ($entity = $entities->fetchObject()) {
  100. $ids = array($entity->id, '0', $entity->name);
  101. $result['TripalEntity'][$entity->id] = entity_create_stub_entity('TripalEntity', $ids);
  102. }
  103. return $result;
  104. }