field_test.storage.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. * Implements hook_field_storage_query().
  215. */
  216. function field_test_field_storage_query($field_id, $conditions, $count, &$cursor = NULL, $age) {
  217. $data = _field_test_storage_data();
  218. $load_current = $age == FIELD_LOAD_CURRENT;
  219. $field = field_info_field_by_id($field_id);
  220. $field_columns = array_keys($field['columns']);
  221. $field_data = $data[$field['id']];
  222. $sub_table = $load_current ? 'current' : 'revisions';
  223. // We need to sort records by entity type and entity id.
  224. usort($field_data[$sub_table], '_field_test_field_storage_query_sort_helper');
  225. // Initialize results array.
  226. $return = array();
  227. $entity_count = 0;
  228. $rows_count = 0;
  229. $rows_total = count($field_data[$sub_table]);
  230. $skip = $cursor;
  231. $skipped = 0;
  232. foreach ($field_data[$sub_table] as $row) {
  233. if ($count != FIELD_QUERY_NO_LIMIT && $entity_count >= $count) {
  234. break;
  235. }
  236. if ($row->field_id == $field['id']) {
  237. $match = TRUE;
  238. $condition_deleted = FALSE;
  239. // Add conditions.
  240. foreach ($conditions as $condition) {
  241. @list($column, $value, $operator) = $condition;
  242. if (empty($operator)) {
  243. $operator = is_array($value) ? 'IN' : '=';
  244. }
  245. switch ($operator) {
  246. case '=':
  247. $match = $match && $row->{$column} == $value;
  248. break;
  249. case '<>':
  250. case '<':
  251. case '<=':
  252. case '>':
  253. case '>=':
  254. eval('$match = $match && ' . $row->{$column} . ' ' . $operator . ' '. $value);
  255. break;
  256. case 'IN':
  257. $match = $match && in_array($row->{$column}, $value);
  258. break;
  259. case 'NOT IN':
  260. $match = $match && !in_array($row->{$column}, $value);
  261. break;
  262. case 'BETWEEN':
  263. $match = $match && $row->{$column} >= $value[0] && $row->{$column} <= $value[1];
  264. break;
  265. case 'STARTS_WITH':
  266. case 'ENDS_WITH':
  267. case 'CONTAINS':
  268. // Not supported.
  269. $match = FALSE;
  270. break;
  271. }
  272. // Track condition on 'deleted'.
  273. if ($column == 'deleted') {
  274. $condition_deleted = TRUE;
  275. }
  276. }
  277. // Exclude deleted data unless we have a condition on it.
  278. if (!$condition_deleted && $row->deleted) {
  279. $match = FALSE;
  280. }
  281. if ($match) {
  282. if (!isset($skip) || $skipped >= $skip) {
  283. $cursor++;
  284. // If querying all revisions and the entity type has revisions, we need
  285. // to key the results by revision_ids.
  286. $entity_type = entity_get_info($row->type);
  287. $id = ($load_current || empty($entity_type['entity keys']['revision'])) ? $row->entity_id : $row->revision_id;
  288. if (!isset($return[$row->type][$id])) {
  289. $return[$row->type][$id] = entity_create_stub_entity($row->type, array($row->entity_id, $row->revision_id, $row->bundle));
  290. $entity_count++;
  291. }
  292. }
  293. else {
  294. $skipped++;
  295. }
  296. }
  297. }
  298. $rows_count++;
  299. // The query is complete if we walked the whole array.
  300. if ($count != FIELD_QUERY_NO_LIMIT && $rows_count >= $rows_total) {
  301. $cursor = FIELD_QUERY_COMPLETE;
  302. }
  303. }
  304. return $return;
  305. }
  306. /**
  307. * Sort helper for field_test_field_storage_query().
  308. *
  309. * Sorts by entity type and entity id.
  310. */
  311. function _field_test_field_storage_query_sort_helper($a, $b) {
  312. if ($a->type == $b->type) {
  313. if ($a->entity_id == $b->entity_id) {
  314. return 0;
  315. }
  316. else {
  317. return $a->entity_id < $b->entity_id ? -1 : 1;
  318. }
  319. }
  320. else {
  321. return $a->type < $b->type ? -1 : 1;
  322. }
  323. }
  324. /**
  325. * Implements hook_field_storage_create_field().
  326. */
  327. function field_test_field_storage_create_field($field) {
  328. if ($field['storage']['type'] == 'field_test_storage_failure') {
  329. throw new Exception('field_test_storage_failure engine always fails to create fields');
  330. }
  331. $data = _field_test_storage_data();
  332. $data[$field['id']] = array(
  333. 'current' => array(),
  334. 'revisions' => array(),
  335. );
  336. _field_test_storage_data($data);
  337. }
  338. /**
  339. * Implements hook_field_storage_delete_field().
  340. */
  341. function field_test_field_storage_delete_field($field) {
  342. $data = _field_test_storage_data();
  343. $field_data = &$data[$field['id']];
  344. foreach (array('current', 'revisions') as $sub_table) {
  345. foreach ($field_data[$sub_table] as &$row) {
  346. $row->deleted = TRUE;
  347. }
  348. }
  349. _field_test_storage_data($data);
  350. }
  351. /**
  352. * Implements hook_field_storage_delete_instance().
  353. */
  354. function field_test_field_storage_delete_instance($instance) {
  355. $data = _field_test_storage_data();
  356. $field = field_info_field($instance['field_name']);
  357. $field_data = &$data[$field['id']];
  358. foreach (array('current', 'revisions') as $sub_table) {
  359. foreach ($field_data[$sub_table] as &$row) {
  360. if ($row->bundle == $instance['bundle']) {
  361. $row->deleted = TRUE;
  362. }
  363. }
  364. }
  365. _field_test_storage_data($data);
  366. }
  367. /**
  368. * Implements hook_field_attach_create_bundle().
  369. */
  370. function field_test_field_attach_create_bundle($bundle) {
  371. // We don't need to do anything here.
  372. }
  373. /**
  374. * Implements hook_field_attach_rename_bundle().
  375. */
  376. function field_test_field_attach_rename_bundle($bundle_old, $bundle_new) {
  377. $data = _field_test_storage_data();
  378. // We need to account for deleted or inactive fields and instances.
  379. $instances = field_read_instances(array('bundle' => $bundle_new), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
  380. foreach ($instances as $field_name => $instance) {
  381. $field = field_info_field_by_id($instance['field_id']);
  382. if ($field['storage']['type'] == 'field_test_storage') {
  383. $field_data = &$data[$field['id']];
  384. foreach (array('current', 'revisions') as $sub_table) {
  385. foreach ($field_data[$sub_table] as &$row) {
  386. if ($row->bundle == $bundle_old) {
  387. $row->bundle = $bundle_new;
  388. }
  389. }
  390. }
  391. }
  392. }
  393. _field_test_storage_data($data);
  394. }
  395. /**
  396. * Implements hook_field_attach_delete_bundle().
  397. */
  398. function field_test_field_attach_delete_bundle($entity_type, $bundle, $instances) {
  399. $data = _field_test_storage_data();
  400. foreach ($instances as $instance) {
  401. $field = field_info_field_by_id($instance['field_id']);
  402. if ($field['storage']['type'] == 'field_test_storage') {
  403. $field_data = &$data[$field['id']];
  404. foreach (array('current', 'revisions') as $sub_table) {
  405. foreach ($field_data[$sub_table] as &$row) {
  406. if ($row->bundle == $bundle) {
  407. $row->deleted = TRUE;
  408. }
  409. }
  410. }
  411. }
  412. }
  413. _field_test_storage_data($data);
  414. }