tripal.field_storage.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. 'tripal_storage_api' => TRUE,
  18. ),
  19. ),
  20. );
  21. }
  22. /**
  23. * Implements hook_field_storage_load().
  24. *
  25. * Responsible for loading the fields and adding their values to the entity.
  26. */
  27. function tripal_field_storage_load($entity_type, $entities, $age,
  28. $fields, $options) {
  29. $load_current = $age == FIELD_LOAD_CURRENT;
  30. global $language;
  31. $langcode = $language->language;
  32. foreach ($entities as $id => $entity) {
  33. // Iterate through the entity's fields so we can get the column names
  34. // that need to be selected from each of the tables represented.
  35. $tables = array();
  36. foreach ($fields as $field_id => $ids) {
  37. // By the time this hook runs, the relevant field definitions have been
  38. // populated and cached in FieldInfo, so calling field_info_field_by_id()
  39. // on each field individually is more efficient than loading all fields in
  40. // memory upfront with field_info_field_by_ids().
  41. $field = field_info_field_by_id($field_id);
  42. $field_name = $field['field_name'];
  43. $field_type = $field['type'];
  44. $field_module = $field['module'];
  45. // Get the instnace for this field
  46. $instance = field_info_instance($entity_type, $field_name, $entity->bundle);
  47. // Allow the creating module to alter the value if desired. The
  48. // module should do this if the field has any other form elements
  49. // that need populationg besides the value which was set above.
  50. tripal_load_include_field_class($field_type);
  51. if (class_exists($field_type)) {
  52. $tfield = new $field_type($field, $instance);
  53. $tfield->load($entity);
  54. }
  55. } // end: foreach ($fields as $field_id => $ids) {
  56. } // end: foreach ($entities as $id => $entity) {
  57. }
  58. /**
  59. * Implements hook_field_storage_query().
  60. */
  61. function tripal_field_storage_query($query) {
  62. $filter = array();
  63. $entity_ids = array();
  64. // Create the initial query.
  65. $select = db_select('tripal_entity', 'TE');
  66. $select->join('tripal_bundle', 'TB', 'TE.bundle = TB.name');
  67. $select->fields('TE', array('id'));
  68. $select->fields('TB', array('name'));
  69. // Apply any entity condition filters.
  70. if ($query->entityConditions) {
  71. if (array_key_exists('bundle', $query->entityConditions)) {
  72. $select->condition('TB.name', $query->entityConditions['bundle']['value']);
  73. }
  74. }
  75. if ($query->relationshipConditions) {
  76. foreach ($query->relationshipConditions as $table_alias => $reldetails) {
  77. $field = $reldetails['field'];
  78. $value = $reldetails['value'];
  79. $op = $reldetails['op'];
  80. $relationship = $query->relationships[$table_alias];
  81. $table = $relationship['table'];
  82. $select->join($table, $table_alias, 'TE.id = ' . $table_alias . '.' . $relationship['field']);
  83. $select->condition($table_alias . '.' . $field, $value, $op);
  84. }
  85. }
  86. // Add in any filters to the query.
  87. foreach ($query->fieldConditions as $index => $condition) {
  88. $field = $condition['field'];
  89. // Skip conditions that don't belong to this storage type.
  90. if ($field['storage']['type'] != 'tripal_no_storage') {
  91. continue;
  92. }
  93. $value = $condition['value'];
  94. $operator = $condition['operator'] ? $condition['operator'] : '=';
  95. // Filtering on the content type is filtering on the label.
  96. if ($field['field_name'] == 'content_type') {
  97. $select->condition('TB.label', $value, $operator);
  98. }
  99. }
  100. // Add in any sorting to the query.
  101. foreach ($query->order as $index => $sort) {
  102. $field = $sort['specifier']['field'];
  103. // Skip sorts that don't belong to this storage type.
  104. if ($field['storage']['type'] != 'tripal_no_storage') {
  105. continue;
  106. }
  107. $direction = $sort['direction'];
  108. // Filtering on the content type is a filter using the label
  109. if ($field['field_name'] == 'content_type') {
  110. $select->orderBy('TB.label', $direction);
  111. }
  112. }
  113. // Add a range of records to retrieve
  114. if ($query->range) {
  115. $select->range($query->range['start'], $query->range['length']);
  116. }
  117. // Only include records that are deleted. Tripal doesn't keep track of
  118. // records that are deleted that need purging separately so we can do nothing
  119. // with this.
  120. if (property_exists($query, 'deleted') and $query->deleted) {
  121. // There won't ever be field data marked as deleted so just created a
  122. // condition that always evaluates to false.
  123. $select->where('1=0');
  124. }
  125. // Perform the query and return the results.
  126. $entities = $select->execute();
  127. if ($query->count) {
  128. return $entities->rowCount();
  129. }
  130. $result = array(
  131. 'TripalEntity' => array(),
  132. );
  133. while ($entity = $entities->fetchObject()) {
  134. $ids = array($entity->id, '0', $entity->name);
  135. $result['TripalEntity'][$entity->id] = entity_create_stub_entity('TripalEntity', $ids);
  136. }
  137. return $result;
  138. }