field_test.storage.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /**
  3. * @file
  4. * Defines a field storage backend.
  5. */
  6. /**
  7. * Implements hook_field_storage_info().
  8. */
  9. function field_test_field_storage_info() {
  10. return array(
  11. 'field_test_storage' => array(
  12. 'label' => t('Test storage'),
  13. 'description' => t('Dummy test storage backend. Stores field values in the variable table.'),
  14. ),
  15. 'field_test_storage_failure' => array(
  16. 'label' => t('Test storage failure'),
  17. 'description' => t('Dummy test storage backend. Always fails to create fields.'),
  18. ),
  19. );
  20. }
  21. /**
  22. * Implements hook_field_storage_details().
  23. */
  24. function field_test_field_storage_details($field) {
  25. $details = array();
  26. // Add field columns.
  27. $columns = array();
  28. foreach ((array) $field['columns'] as $column_name => $attributes) {
  29. $columns[$column_name] = $column_name;
  30. }
  31. return array(
  32. 'drupal_variables' => array(
  33. 'field_test_storage_data[FIELD_LOAD_CURRENT]' => $columns,
  34. 'field_test_storage_data[FIELD_LOAD_REVISION]' => $columns,
  35. ),
  36. );
  37. }
  38. /**
  39. * Implements hook_field_storage_details_alter().
  40. *
  41. * @see FieldAttachStorageTestCase::testFieldStorageDetailsAlter()
  42. */
  43. function field_test_field_storage_details_alter(&$details, $field) {
  44. // For testing, storage details are changed only because of the field name.
  45. if ($field['field_name'] == 'field_test_change_my_details') {
  46. $columns = array();
  47. foreach ((array) $field['columns'] as $column_name => $attributes) {
  48. $columns[$column_name] = $column_name;
  49. }
  50. $details['drupal_variables'] = array(
  51. FIELD_LOAD_CURRENT => array(
  52. 'moon' => $columns,
  53. ),
  54. FIELD_LOAD_REVISION => array(
  55. 'mars' => $columns,
  56. ),
  57. );
  58. }
  59. }
  60. /**
  61. * Helper function: stores or retrieves data from the 'storage backend'.
  62. */
  63. function _field_test_storage_data($data = NULL) {
  64. if (!isset($data)) {
  65. return variable_get('field_test_storage_data', array());
  66. }
  67. else {
  68. variable_set('field_test_storage_data', $data);
  69. }
  70. }
  71. /**
  72. * Implements hook_field_storage_load().
  73. */
  74. function field_test_field_storage_load($entity_type, $entities, $age, $fields, $options) {
  75. $data = _field_test_storage_data();
  76. $load_current = $age == FIELD_LOAD_CURRENT;
  77. foreach ($fields as $field_id => $ids) {
  78. $field = field_info_field_by_id($field_id);
  79. $field_name = $field['field_name'];
  80. $field_data = $data[$field['id']];
  81. $sub_table = $load_current ? 'current' : 'revisions';
  82. $delta_count = array();
  83. foreach ($field_data[$sub_table] as $row) {
  84. if ($row->type == $entity_type && (!$row->deleted || $options['deleted'])) {
  85. if (($load_current && in_array($row->entity_id, $ids)) || (!$load_current && in_array($row->revision_id, $ids))) {
  86. if (in_array($row->language, field_available_languages($entity_type, $field))) {
  87. if (!isset($delta_count[$row->entity_id][$row->language])) {
  88. $delta_count[$row->entity_id][$row->language] = 0;
  89. }
  90. if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
  91. $item = array();
  92. foreach ($field['columns'] as $column => $attributes) {
  93. $item[$column] = $row->{$column};
  94. }
  95. $entities[$row->entity_id]->{$field_name}[$row->language][] = $item;
  96. $delta_count[$row->entity_id][$row->language]++;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * Implements hook_field_storage_write().
  106. */
  107. function field_test_field_storage_write($entity_type, $entity, $op, $fields) {
  108. $data = _field_test_storage_data();
  109. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  110. foreach ($fields as $field_id) {
  111. $field = field_info_field_by_id($field_id);
  112. $field_name = $field['field_name'];
  113. $field_data = &$data[$field_id];
  114. $all_languages = field_available_languages($entity_type, $field);
  115. $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
  116. // Delete and insert, rather than update, in case a value was added.
  117. if ($op == FIELD_STORAGE_UPDATE) {
  118. // Delete languages present in the incoming $entity->$field_name.
  119. // Delete all languages if $entity->$field_name is empty.
  120. $languages = !empty($entity->$field_name) ? $field_languages : $all_languages;
  121. if ($languages) {
  122. foreach ($field_data['current'] as $key => $row) {
  123. if ($row->type == $entity_type && $row->entity_id == $id && in_array($row->language, $languages)) {
  124. unset($field_data['current'][$key]);
  125. }
  126. }
  127. if (isset($vid)) {
  128. foreach ($field_data['revisions'] as $key => $row) {
  129. if ($row->type == $entity_type && $row->revision_id == $vid) {
  130. unset($field_data['revisions'][$key]);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. foreach ($field_languages as $langcode) {
  137. $items = (array) $entity->{$field_name}[$langcode];
  138. $delta_count = 0;
  139. foreach ($items as $delta => $item) {
  140. $row = (object) array(
  141. 'field_id' => $field_id,
  142. 'type' => $entity_type,
  143. 'entity_id' => $id,
  144. 'revision_id' => $vid,
  145. 'bundle' => $bundle,
  146. 'delta' => $delta,
  147. 'deleted' => FALSE,
  148. 'language' => $langcode,
  149. );
  150. foreach ($field['columns'] as $column => $attributes) {
  151. $row->{$column} = isset($item[$column]) ? $item[$column] : NULL;
  152. }
  153. $field_data['current'][] = $row;
  154. if (isset($vid)) {
  155. $field_data['revisions'][] = $row;
  156. }
  157. if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) {
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. _field_test_storage_data($data);
  164. }
  165. /**
  166. * Implements hook_field_storage_delete().
  167. */
  168. function field_test_field_storage_delete($entity_type, $entity, $fields) {
  169. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  170. // Note: reusing field_test_storage_purge(), like field_sql_storage.module
  171. // does, is highly inefficient in our case...
  172. foreach (field_info_instances($bundle) as $instance) {
  173. if (isset($fields[$instance['field_id']])) {
  174. $field = field_info_field_by_id($instance['field_id']);
  175. field_test_field_storage_purge($entity_type, $entity, $field, $instance);
  176. }
  177. }
  178. }
  179. /**
  180. * Implements hook_field_storage_purge().
  181. */
  182. function field_test_field_storage_purge($entity_type, $entity, $field, $instance) {
  183. $data = _field_test_storage_data();
  184. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  185. $field_data = &$data[$field['id']];
  186. foreach (array('current', 'revisions') as $sub_table) {
  187. foreach ($field_data[$sub_table] as $key => $row) {
  188. if ($row->type == $entity_type && $row->entity_id == $id) {
  189. unset($field_data[$sub_table][$key]);
  190. }
  191. }
  192. }
  193. _field_test_storage_data($data);
  194. }
  195. /**
  196. * Implements hook_field_storage_delete_revision().
  197. */
  198. function field_test_field_storage_delete_revision($entity_type, $entity, $fields) {
  199. $data = _field_test_storage_data();
  200. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  201. foreach ($fields as $field_id) {
  202. $field_data = &$data[$field_id];
  203. foreach (array('current', 'revisions') as $sub_table) {
  204. foreach ($field_data[$sub_table] as $key => $row) {
  205. if ($row->type == $entity_type && $row->entity_id == $id && $row->revision_id == $vid) {
  206. unset($field_data[$sub_table][$key]);
  207. }
  208. }
  209. }
  210. }
  211. _field_test_storage_data($data);
  212. }
  213. /**
  214. * Sort helper for field_test_field_storage_query().
  215. *
  216. * Sorts by entity type and entity id.
  217. */
  218. function _field_test_field_storage_query_sort_helper($a, $b) {
  219. if ($a->type == $b->type) {
  220. if ($a->entity_id == $b->entity_id) {
  221. return 0;
  222. }
  223. else {
  224. return $a->entity_id < $b->entity_id ? -1 : 1;
  225. }
  226. }
  227. else {
  228. return $a->type < $b->type ? -1 : 1;
  229. }
  230. }
  231. /**
  232. * Implements hook_field_storage_create_field().
  233. */
  234. function field_test_field_storage_create_field($field) {
  235. if ($field['storage']['type'] == 'field_test_storage_failure') {
  236. throw new Exception('field_test_storage_failure engine always fails to create fields');
  237. }
  238. $data = _field_test_storage_data();
  239. $data[$field['id']] = array(
  240. 'current' => array(),
  241. 'revisions' => array(),
  242. );
  243. _field_test_storage_data($data);
  244. }
  245. /**
  246. * Implements hook_field_storage_delete_field().
  247. */
  248. function field_test_field_storage_delete_field($field) {
  249. $data = _field_test_storage_data();
  250. $field_data = &$data[$field['id']];
  251. foreach (array('current', 'revisions') as $sub_table) {
  252. foreach ($field_data[$sub_table] as &$row) {
  253. $row->deleted = TRUE;
  254. }
  255. }
  256. _field_test_storage_data($data);
  257. }
  258. /**
  259. * Implements hook_field_storage_delete_instance().
  260. */
  261. function field_test_field_storage_delete_instance($instance) {
  262. $data = _field_test_storage_data();
  263. $field = field_info_field($instance['field_name']);
  264. $field_data = &$data[$field['id']];
  265. foreach (array('current', 'revisions') as $sub_table) {
  266. foreach ($field_data[$sub_table] as &$row) {
  267. if ($row->bundle == $instance['bundle']) {
  268. $row->deleted = TRUE;
  269. }
  270. }
  271. }
  272. _field_test_storage_data($data);
  273. }
  274. /**
  275. * Implements hook_field_attach_create_bundle().
  276. */
  277. function field_test_field_attach_create_bundle($bundle) {
  278. // We don't need to do anything here.
  279. }
  280. /**
  281. * Implements hook_field_attach_rename_bundle().
  282. */
  283. function field_test_field_attach_rename_bundle($bundle_old, $bundle_new) {
  284. $data = _field_test_storage_data();
  285. // We need to account for deleted or inactive fields and instances.
  286. $instances = field_read_instances(array('bundle' => $bundle_new), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
  287. foreach ($instances as $field_name => $instance) {
  288. $field = field_info_field_by_id($instance['field_id']);
  289. if ($field['storage']['type'] == 'field_test_storage') {
  290. $field_data = &$data[$field['id']];
  291. foreach (array('current', 'revisions') as $sub_table) {
  292. foreach ($field_data[$sub_table] as &$row) {
  293. if ($row->bundle == $bundle_old) {
  294. $row->bundle = $bundle_new;
  295. }
  296. }
  297. }
  298. }
  299. }
  300. _field_test_storage_data($data);
  301. }
  302. /**
  303. * Implements hook_field_attach_delete_bundle().
  304. */
  305. function field_test_field_attach_delete_bundle($entity_type, $bundle, $instances) {
  306. $data = _field_test_storage_data();
  307. foreach ($instances as $instance) {
  308. $field = field_info_field_by_id($instance['field_id']);
  309. if ($field['storage']['type'] == 'field_test_storage') {
  310. $field_data = &$data[$field['id']];
  311. foreach (array('current', 'revisions') as $sub_table) {
  312. foreach ($field_data[$sub_table] as &$row) {
  313. if ($row->bundle == $bundle) {
  314. $row->deleted = TRUE;
  315. }
  316. }
  317. }
  318. }
  319. }
  320. _field_test_storage_data($data);
  321. }