Newer
Older
<?php
// $Id$
define('FIELD_TEST_ELEMENT_ID', 1);
define('FIELD_TEST_BUNDLE', 'test_bundle');
* Implement hook_permission().
function field_test_permission() {
$perms = array(
'access field_test content' => array(
'title' => t('Access field_test content'),
'description' => t('View published field_test content.'),
),
'administer field_test content' => array(
'title' => t('Administer field_test content'),
'description' => t('Manage field_test content'),
),
);
return $perms;
}
/**

Dries Buytaert
committed
* Implement hook_menu().
*/
function field_test_menu() {
$items = array();
$bundles = field_info_bundles('test_entity');
foreach ($bundles as $bundle_name => $bundle_info) {
$bundle_url_str = str_replace('_', '-', $bundle_name);
$items['test-entity/add/' . $bundle_url_str] = array(
'title' => t('Add %bundle test_entity', array('%bundle' => $bundle_info['label'])),
'page callback' => 'field_test_entity_add',
'page arguments' => array(2),
'access arguments' => array('administer field_test content'),
'type' => MENU_NORMAL_ITEM,
);
}
$items['test-entity/%field_test_entity/edit'] = array(
'title' => 'Edit test entity',
'page callback' => 'field_test_entity_edit',
'page arguments' => array(1),
'access arguments' => array('administer field_test content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
*
* 'Field attach' API.
*
*/
/**
* Define a test fieldable entity.
*/

Dries Buytaert
committed
function field_test_entity_info() {
$bundles = variable_get('field_test_bundles', array('test_bundle' => array('label' => 'Test Bundle')));
return array(
'test_entity' => array(
'name' => t('Test Entity'),
'object keys' => array(
'id' => 'ftid',
'revision' => 'ftvid',
'bundle' => 'fttype',
),
'cacheable' => FALSE,
'bundles' => $bundles,

Dries Buytaert
committed
'fieldable' => TRUE,
),
// This entity type doesn't get form handling for now...
'test_cacheable_entity' => array(
'name' => t('Test Entity, cacheable'),
'object keys' => array(
'id' => 'ftid',
'revision' => 'ftvid',
'bundle' => 'fttype',
),

Dries Buytaert
committed
'fieldable' => TRUE,
'cacheable' => TRUE,
'bundles' => $bundles,
),
);
}

Angie Byron
committed
/**
* Implement hook_entity_info_alter().

Angie Byron
committed
*/
function field_test_entity_info_alter(&$entity_info) {
foreach (field_test_entity_info_translatable() as $obj_type => $translatable) {
$entity_info[$obj_type]['translation_handlers']['field_test'] = TRUE;

Angie Byron
committed
}
}

Angie Byron
committed
/**
* Create a new bundle for test_entity objects.
*
* @param $bundle_name

Angie Byron
committed
* The machine-readable name of the bundle.
* @param $text
* The human-readable name of the bundle. If none is provided, the machine
* name will be used.
*/
function field_test_create_bundle($bundle_name, $text = NULL) {
$bundles = variable_get('field_test_bundles', array('test_bundle' => array('label' => 'Test Bundle')));
$bundles += array($bundle_name => array('label' => $text ? $text : $bundle_name));
variable_set('field_test_bundles', $bundles);
field_attach_create_bundle($bundle_name);

Angie Byron
committed
/**
* Rename a bundle for test_entity objects.
*
* @param $bundle_old
* The machine-readable name of the bundle to rename.
* @param $bundle_new
* The new machine-readable name of the bundle.
*/
function field_test_rename_bundle($bundle_old, $bundle_new) {
$bundles = variable_get('field_test_bundles', array('test_bundle' => array('label' => 'Test Bundle')));
$bundles[$bundle_new] = $bundles[$bundle_old];
unset($bundles[$bundle_old]);
variable_set('field_test_bundles', $bundles);
field_attach_rename_bundle($bundle_old, $bundle_new);
}

Angie Byron
committed
/**
* Delete a bundle for test_entity objects.
*
* @param $bundle_name

Angie Byron
committed
* The machine-readable name of the bundle to delete.
*/
function field_test_delete_bundle($bundle_name) {
$bundles = variable_get('field_test_bundles', array('test_bundle' => array('label' => 'Test Bundle')));
unset($bundles[$bundle_name]);
variable_set('field_test_bundles', $bundles);
field_attach_delete_bundle($bundle_name);

Dries Buytaert
committed
* Implement hook_field_build_modes().
*/
function field_test_field_build_modes($obj_type) {
$modes = array();
if ($obj_type == 'test_entity' || $obj_type == 'test_cacheable_entity') {
$modes = array(
'full' => t('Full node'),
'teaser' => t('Teaser'),
);
}
return $modes;
}
/**
* Helper function to create a basic 'test entity' structure.
*
* TODO : do we stil need this now that we can actualy load and save test_entities ?
*/
function field_test_create_stub_entity($id = 1, $vid = 1, $bundle = FIELD_TEST_BUNDLE) {
$entity = new stdClass();
// Only set id and vid properties if they don't come as NULL (creation form).
if (isset($id)) {
$entity->ftid = $id;
}
if (isset($vid)) {
$entity->ftvid = $vid;
}
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
$entity->fttype = $bundle;
return $entity;
}
function field_test_entity_load($ftid, $ftvid = NULL) {
// Load basic strucure.
$query = db_select('test_entity', 'fte', array())
->fields('fte')
->condition('ftid', $ftid);
if ($ftvid) {
$query->condition('ftvid', $ftvid);
}
$entities = $query->execute()->fetchAllAssoc('ftid');
// Attach fields.
if ($ftvid) {
field_attach_load_revision('test_entity', $entities);
}
else {
field_attach_load('test_entity', $entities);
}
return $entities[$ftid];
}
function field_test_entity_save(&$entity) {
field_attach_presave('test_entity', $entity);
if (!isset($entity->is_new)) {
$entity->is_new = empty($entity->ftid);
if (!$entity->is_new && !empty($entity->revision)) {
$entity->old_ftvid = $entity->ftvid;
unset($entity->ftvid);
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
}
$update_entity = TRUE;
if ($entity->is_new) {
drupal_write_record('test_entity', $entity);
drupal_write_record('test_entity_revision', $entity);
$op = 'insert';
}
else {
drupal_write_record('test_entity', $entity, 'ftid');
if (!empty($entity->revision)) {
drupal_write_record('test_entity_revision', $entity);
}
else {
drupal_write_record('test_entity_revision', $entity, 'ftvid');
$update_entity = FALSE;
}
$op = 'update';
}
if ($update_entity) {
db_update('test_entity')
->fields(array('ftvid' => $entity->ftvid))
->condition('ftid', $entity->ftid)
->execute();
}
// Save fields.
$function = "field_attach_$op";
$function('test_entity', $entity);
}
function field_test_entity_add($fttype) {
$fttype = str_replace('-', '_', $fttype);
$entity = (object)array('fttype' => $fttype);
drupal_set_title(t('Create test_entity @bundle', array('@bundle' => $fttype)), PASS_THROUGH);
return drupal_get_form('field_test_entity_form', $entity);
}
function field_test_entity_edit($entity) {
drupal_set_title(t('test_entity @ftid revision @ftvid', array('@ftid' => $entity->ftid, '@ftvid' => $entity->ftvid)), PASS_THROUGH);
return drupal_get_form('field_test_entity_form', $entity);
}
/**
* Form to set the value of fields attached to our entity.
*/
function field_test_entity_form($form, &$form_state, $entity) {
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
if (isset($form_state['test_entity'])) {
$entity = $form_state['test_entity'] + (array)$entity;
}
$entity = (object)$entity;
foreach (array('ftid', 'ftvid', 'fttype') as $key) {
$form[$key] = array(
'#type' => 'value',
'#value' => isset($entity->$key) ? $entity->$key : NULL,
);
}
// Add field widgets.
$form['#builder_function'] = 'field_test_entity_form_submit_builder';
field_attach_form('test_entity', $entity, $form, $form_state);
$form['revision'] = array(
'#access' => user_access('administer field_test content'),
'#type' => 'checkbox',
'#title' => t('Create new revision'),
'#default_value' => FALSE,
'#weight' => 100,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 101,
);
return $form;
}
/**
* Validate handler for field_test_set_field_values().
*/
function field_test_entity_form_validate($form, &$form_state) {

Angie Byron
committed
$entity = field_test_create_stub_entity($form_state['values']['ftid'], $form_state['values']['ftvid'], $form_state['values']['fttype']);
field_attach_form_validate('test_entity', $entity, $form, $form_state);
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
}
/**
* Submit handler for field_test_set_field_values().
*/
function field_test_entity_form_submit($form, &$form_state) {
$entity = field_test_entity_form_submit_builder($form, $form_state);
$insert = empty($entity->ftid);
field_test_entity_save($entity);
$message = $insert ? t('test_entity @id has been created.', array('@id' => $entity->ftid)) : t('test_entity @id has been updated.', array('@id' => $entity->ftid));
drupal_set_message($message);
if ($entity->ftid) {
unset($form_state['rebuild']);
$form_state['redirect'] = 'test-entity/' . $entity->ftid . '/edit';
}
else {
// Error on save.
drupal_set_message(t('The entity could not be saved.'), 'error');
}
}
/**
* Build a test_entity by processing submitted form values and prepare for a form rebuild.
*/
function field_test_entity_form_submit_builder($form, &$form_state) {
$entity = field_test_create_stub_entity($form_state['values']['ftid'], $form_state['values']['ftvid'], $form_state['values']['fttype']);
field_attach_submit('test_entity', $entity, $form, $form_state);
$form_state['test_entity'] = (array)$entity;
$form_state['rebuild'] = TRUE;
return $entity;
}
/**
*
* 'Field type' API.
*
*/

Dries Buytaert
committed
* Implement hook_field_info().
*
* This field provides a textfield which only accepts the value 1.
*/
function field_test_field_info() {
return array(
'test_field' => array(
'label' => t('Test Field'),

Angie Byron
committed
'settings' => array(
'test_field_setting' => 'dummy test string',

Dries Buytaert
committed
'changeable' => 'a changeable field setting',
'unchangeable' => 'an unchangeable field setting',

Angie Byron
committed
),
'instance_settings' => array(
'test_instance_setting' => 'dummy test string',
'test_hook_field_load' => FALSE,
),
'default_widget' => 'test_field_widget',
'default_formatter' => 'field_test_default',
),
);
}

Dries Buytaert
committed
/**
* Implement hook_field_update_forbid().
*/
function field_test_field_update_forbid($field, $prior_field, $has_data) {
if ($field['type'] == 'test_field' && $field['settings']['unchangeable'] != $prior_field['settings']['unchangeable']) {
throw new FieldException("field_test 'unchangeable' setting cannot be changed'");
}
}

Dries Buytaert
committed
* Implement hook_field_schema().
function field_test_field_schema($field) {
return array(
'columns' => array(
'value' => array(
'type' => 'int',
'size' => 'tiny',
'not null' => FALSE,
),
),
'indexes' => array(
'value' => array('value'),
),
/**
* Implement hook_field_load().
*/

Angie Byron
committed
function field_test_field_load($obj_type, $objects, $field, $instances, $langcode, &$items, $age) {
foreach ($items as $id => $item) {
// To keep the test non-intrusive, only act for instances with the
// test_hook_field_load setting explicitly set to TRUE.
if ($instances[$id]['settings']['test_hook_field_load']) {
foreach ($item as $delta => $value) {
// Don't add anything on empty values.
if ($value) {
$items[$id][$delta]['additional_key'] = 'additional_value';
}
}
}
}
}

Dries Buytaert
committed
* Implement hook_field_validate().

Angie Byron
committed
*
* Possible error codes:
* - 'field_test_invalid': The value is invalid.

Angie Byron
committed
function field_test_field_validate($obj_type, $object, $field, $instance, $langcode, $items, &$errors) {

Angie Byron
committed
foreach ($items as $delta => $item) {
if ($item['value'] == -1) {

Angie Byron
committed
$errors[$field['field_name']][$langcode][$delta][] = array(

Angie Byron
committed
'error' => 'field_test_invalid',
'message' => t('%name does not accept the value -1.', array('%name' => $instance['label'])),
);

Dries Buytaert
committed
* Implement hook_field_sanitize().

Angie Byron
committed
function field_test_field_sanitize($obj_type, $object, $field, $instance, $langcode, &$items) {
foreach ($items as $delta => $item) {
$value = check_plain($item['value']);
$items[$delta]['safe'] = $value;
}
}
/**

Dries Buytaert
committed
* Implement hook_field_is_empty().
*/
function field_test_field_is_empty($item, $field) {
return empty($item['value']);
}
/**

Dries Buytaert
committed
* Implement hook_field_widget_info().
* Here we indicate that the field module will handle
* the default value and multiple values for these widgets.
*
* Callbacks can be omitted if default handing is used.
* They're included here just so this module can be used
* as an example for custom modules that might do things
* differently.
*/
function field_test_field_widget_info() {
return array(
'test_field_widget' => array(
'label' => t('Test field'),
'field types' => array('test_field'),
'settings' => array('test_widget_setting' => 'dummy test string'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
),
'test_field_widget_multiple' => array(
'label' => t('Test field 1'),
'field types' => array('test_field'),
'settings' => array('test_widget_setting_multiple' => 'dummy test string'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
'default value' => FIELD_BEHAVIOR_DEFAULT,

Dries Buytaert
committed
* Implement hook_field_widget().
*
* Attach a single form element to the form. It will be built out and

Dries Buytaert
committed
* validated in the callback(s) listed in hook_element_info(). We build it
* out in the callbacks rather than here in hook_widget so it can be
* plugged into any module that can provide it with valid
* $field information.
*
* If there are multiple values for this field, the field module will
* call this function as many times as needed.
*
* @param $form
* the entire form array, $form['#node'] holds node information
* @param $form_state
* the form_state, $form_state['values'][$field['field_name']]
* holds the field's form values.
* @param $field
* The field structure.

Angie Byron
committed
* @param $instance
* the instance array
* @param $items
* array of default values for this field
* @param $delta
* the order of this item in the array of subelements (0, 1, 2, etc)
*
* @return
* the form item for a single element for this field
*/
function field_test_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = 0) {
$element = array(
'value' => array(
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '',
'#required' => $instance['required'],

Angie Byron
committed
/**

Dries Buytaert
committed
* Implement hook_field_widget_error().

Angie Byron
committed
*/
function field_test_field_widget_error($element, $error) {
form_error($element['value'], $error['message']);
}

Dries Buytaert
committed
* Implement hook_field_formatter_info().
*/
function field_test_field_formatter_info() {
return array(
'field_test_default' => array(
'label' => t('Default'),
'description' => t('Default formatter'),
'field types' => array('test_field'),
'settings' => array(
'test_formatter_setting' => 'dummy test string',
),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
'label' => t('Multiple'),
'description' => t('Multiple formatter'),
'field types' => array('test_field'),
'settings' => array(
'test_formatter_setting_multiple' => 'dummy test string',
),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
),
),
);
}
/**

Dries Buytaert
committed
* Implement hook_theme().
*/
function field_test_theme() {
return array(
'field_formatter_field_test_default' => array(
'arguments' => array('element' => NULL),
),
'field_formatter_field_test_multiple' => array(
'arguments' => array('element' => NULL),
),
);
}
/**
* Theme function for 'field_test_default' formatter.
*/

Dries Buytaert
committed
function theme_field_formatter_field_test_default($variables) {
$element = $variables['element'];
$value = $element['#item']['value'];
$settings = $element['#settings'];
return $settings['test_formatter_setting'] . '|' . $value;
}
/**
* Theme function for 'field_test_multiple' formatter.
*/

Dries Buytaert
committed
function theme_field_formatter_field_test_multiple($variables) {
$element = $variables['element'];
$settings = $element['#settings'];
$items = array();
foreach (element_children($element) as $key) {
$items[$key] = $key .':'. $element[$key]['#item']['value'];
}
$output = implode('|', $items);
return $settings['test_formatter_setting_multiple'] . '|' . $output;
}
/**
* Sample function to test default value assignment.
*/
function field_test_default_value($obj_type, $object, $field, $instance) {
return array(array('value' => 99));

Angie Byron
committed
}

Angie Byron
committed
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/**
* Generic op to test _field_invoke behavior.
*/
function field_test_field_test_op($obj_type, $object, $field, $instance, $langcode, &$items) {
return array($langcode => md5(serialize(array($obj_type, $object, $field['field_name'], $langcode, $items))));
}
/**
* Generic op to test _field_invoke_multiple behavior.
*/
function field_test_field_test_op_multiple($obj_type, $objects, $field, $instances, $langcode, &$items) {
$result = array();
foreach ($objects as $id => $object) {
$result[$id] = array($langcode => md5(serialize(array($obj_type, $object, $field['field_name'], $langcode, $items[$id]))));
}
return $result;
}
/**
* Implement hook_field_languages().
*/
function field_test_field_languages($obj_type, $field, &$languages) {
if ($field['settings']['test_hook_in']) {
// Add an unavailable language.
$languages[] = 'xx';
// Remove an available language.
unset($languages[0]);
}
}
/**
* Helper function to enable entity translations.
*/
function field_test_entity_info_translatable($obj_type = NULL, $translatable = NULL) {

Angie Byron
committed
$stored_value = &drupal_static(__FUNCTION__, array());
if (isset($obj_type) && isset($translatable)) {
$stored_value[$obj_type] = $translatable;
_field_info_collate_types(TRUE);
}
return $stored_value;
}

Dries Buytaert
committed
/**
*
* 'Field storage' API.
*
*/
/**
* Implement hook_field_storage_info().
*/
function field_test_field_storage_info() {
return array(
'field_test_storage' => array(
'label' => t('Test storage'),
'description' => t('Dummy test storage backend. Stores field values in the variable table.'),
),
'field_test_storage_failure' => array(
'label' => t('Test storage failure'),
'description' => t('Dummy test storage backend. Always fails to create fields.'),
),
);
}

Dries Buytaert
committed
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/**
* Implement hook_field_storage_details().
*/
function field_test_field_storage_details($field, $instance) {
$details = array();
// Add field columns.
$columns = array();
foreach ((array) $field['columns'] as $column_name => $attributes) {
$columns[$column_name] = $column_name;
}
return array(
'drupal_variables' => array(
'field_test_storage_data[FIELD_LOAD_CURRENT]' => $columns,
'field_test_storage_data[FIELD_LOAD_REVISION]' => $columns,
),
);
}
/**
* Implement hook_field_storage_details_alter().
*
* @see FieldAttachStorageTestCase::testFieldStorageDetailsAlter()
*/
function field_test_field_storage_details_alter(&$details, $field, $instance) {
// For testing, storage details are changed only because of the field name.
if ($field['field_name'] == 'field_test_change_my_details') {
$columns = array();
foreach ((array) $field['columns'] as $column_name => $attributes) {
$columns[$column_name] = $column_name;
}
$details['drupal_variables'] = array(
FIELD_LOAD_CURRENT => array(
'moon' => $columns,
),
FIELD_LOAD_REVISION => array(
'mars' => $columns,
),
);
}
}

Dries Buytaert
committed
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Helper function: store or retrieve data from the 'storage backend'.
*/
function _field_test_storage_data($data = NULL) {
if (is_null($data)) {
return variable_get('field_test_storage_data', array());
}
else {
variable_set('field_test_storage_data', $data);
}
}
/**
* Implement hook_field_storage_load().
*/
function field_test_field_storage_load($obj_type, $objects, $age, $fields, $options) {
$data = _field_test_storage_data();
$load_current = $age == FIELD_LOAD_CURRENT;
foreach ($fields as $field_id => $ids) {
$field = field_info_field_by_id($field_id);
$field_name = $field['field_name'];
$field_data = $data[$field['id']];
$sub_table = $load_current ? 'current' : 'revisions';
$delta_count = array();
foreach ($field_data[$sub_table] as $row) {
if ($row->type == $obj_type && (!$row->deleted || $options['deleted'])) {
if (($load_current && in_array($row->entity_id, $ids)) || (!$load_current && in_array($row->revision_id, $ids))) {
if (in_array($row->language, field_multilingual_available_languages($obj_type, $field))) {
if (!isset($delta_count[$row->entity_id][$row->language])) {
$delta_count[$row->entity_id][$row->language] = 0;
}
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
$item = array();
foreach ($field['columns'] as $column => $attributes) {
$item[$column] = $row->{$column};
}
$objects[$row->entity_id]->{$field_name}[$row->language][] = $item;
$delta_count[$row->entity_id][$row->language]++;
}
}
}
}
}
}
}
/**
* Implement hook_field_storage_write().
*/
function field_test_field_storage_write($obj_type, $object, $op, $fields) {
$data = _field_test_storage_data();
list($id, $vid, $bundle) = field_extract_ids($obj_type, $object);
foreach ($fields as $field_id) {
$field = field_info_field_by_id($field_id);
$field_name = $field['field_name'];
$field_data = &$data[$field_id];
$all_languages = field_multilingual_available_languages($obj_type, $field);
$field_languages = array_intersect($all_languages, array_keys((array) $object->$field_name));
// Delete and insert, rather than update, in case a value was added.
if ($op == FIELD_STORAGE_UPDATE) {
// Delete languages present in the incoming $object->$field_name.
// Delete all languages if $object->$field_name is empty.
$languages = !empty($object->$field_name) ? $field_languages : $all_languages;
if ($languages) {
foreach ($field_data['current'] as $key => $row) {
if ($row->type == $obj_type && $row->entity_id == $id && in_array($row->language, $languages)) {
unset($field_data['current'][$key]);
}
}
if (isset($vid)) {
foreach ($field_data['revisions'] as $key => $row) {
if ($row->type == $obj_type && $row->revision_id == $vid) {
unset($field_data['revisions'][$key]);
}
}
}
}
}
foreach ($field_languages as $langcode) {
$items = (array) $object->{$field_name}[$langcode];
$delta_count = 0;
foreach ($items as $delta => $item) {
$row = (object) array(
'field_id' => $field_id,
'type' => $obj_type,
'entity_id' => $id,
'revision_id' => $vid,
'bundle' => $bundle,
'delta' => $delta,
'deleted' => FALSE,
'language' => $langcode,
);
foreach ($field['columns'] as $column => $attributes) {
$row->{$column} = isset($item[$column]) ? $item[$column] : NULL;
}
$field_data['current'][] = $row;
if (isset($vid)) {
$field_data['revisions'][] = $row;
}
if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) {
break;
}
}
}
}
_field_test_storage_data($data);
}
/**
* Implement hook_field_storage_delete().
*/
function field_test_field_storage_delete($obj_type, $object, $fields) {
list($id, $vid, $bundle) = field_extract_ids($obj_type, $object);
// Note: reusing field_test_storage_purge(), like field_sql_storage.module
// does, is highly inefficient in our case...
foreach (field_info_instances($bundle) as $instance) {
if (isset($fields[$instance['field_id']])) {
$field = field_info_field_by_id($instance['field_id']);
field_test_field_storage_purge($obj_type, $object, $field, $instance);
}
}
}
/**
* Implement hook_field_storage_purge().
*/
function field_test_field_storage_purge($obj_type, $object, $field, $instance) {
$data = _field_test_storage_data();
list($id, $vid, $bundle) = field_extract_ids($obj_type, $object);
$field_data = &$data[$field['id']];
foreach (array('current', 'revisions') as $sub_table) {
foreach ($field_data[$sub_table] as $key => $row) {
if ($row->type == $obj_type && $row->entity_id == $id) {
unset($field_data[$sub_table][$key]);
}
}
}
_field_test_storage_data($data);
}
/**
* Implement hook_field_storage_delete_revision().
*/
function field_test_field_storage_delete_revision($obj_type, $object, $fields) {
$data = _field_test_storage_data();
list($id, $vid, $bundle) = field_extract_ids($obj_type, $object);
foreach ($fields as $field_id) {
$field_data = &$data[$field_id];
foreach (array('current', 'revisions') as $sub_table) {
foreach ($field_data[$sub_table] as $key => $row) {
if ($row->type == $obj_type && $row->entity_id == $id && $row->revision_id == $vid) {
unset($field_data[$sub_table][$key]);
}
}
}
}
_field_test_storage_data($data);
}
/**
* Implement hook_field_storage_query().
*/
function field_test_field_storage_query($field_id, $conditions, $count, &$cursor = NULL, $age) {
$data = _field_test_storage_data();
$load_current = $age == FIELD_LOAD_CURRENT;
$field = field_info_field_by_id($field_id);
$field_columns = array_keys($field['columns']);
$field_data = $data[$field['id']];
$sub_table = $load_current ? 'current' : 'revisions';
// We need to sort records by object type and object id.
usort($field_data[$sub_table], '_field_test_field_storage_query_sort_helper');
// Initialize results array.
$return = array();
$obj_count = 0;
$rows_count = 0;
$rows_total = count($field_data[$sub_table]);
$skip = $cursor;
$skipped = 0;
foreach ($field_data[$sub_table] as $row) {
if ($count != FIELD_QUERY_NO_LIMIT && $obj_count >= $count) {
break;
}
if ($row->field_id == $field['id']) {
$match = TRUE;
$condition_deleted = FALSE;
// Add conditions.
foreach ($conditions as $condition) {
@list($column, $value, $operator) = $condition;
if (empty($operator)) {
$operator = is_array($value) ? 'IN' : '=';
}
switch ($operator) {
case '=':
$match = $match && $row->{$column} == $value;
break;
case '!=':
case '<':
case '<=':
case '>':
case '>=':
eval('$match = $match && '. $row->{$column} . ' ' . $operator . ' '. $value);
break;
case 'IN':
$match = $match && in_array($row->{$column}, $value);
break;
case 'NOT IN':
$match = $match && !in_array($row->{$column}, $value);
break;
case 'BETWEEN':
$match = $match && $row->{$column} >= $value[0] && $row->{$column} <= $value[1];
break;
case 'STARTS_WITH':
case 'ENDS_WITH':
case 'CONTAINS':
// Not supported.
$match = FALSE;
break;
}
// Track condition on 'deleted'.
if ($column == 'deleted') {
$condition_deleted = TRUE;
}
}
// Exclude deleted data unless we have a condition on it.
if (!$condition_deleted && $row->deleted) {
$match = FALSE;
}
if ($match) {
if (is_null($skip) || $skipped >= $skip) {
$cursor++;
// If querying all revisions and the entity type has revisions, we need
// to key the results by revision_ids.
$entity_type = field_info_fieldable_types($row->type);
$id = ($load_current || empty($entity_type['object keys']['revision'])) ? $row->entity_id : $row->revision_id;
if (!isset($return[$row->type][$id])) {
$return[$row->type][$id] = field_create_stub_entity($row->type, array($row->entity_id, $row->revision_id, $row->bundle));
$obj_count++;
}
}
else {
$skipped++;
}
}
}
$rows_count++;
// The query is complete if we walked the whole array.
if ($count != FIELD_QUERY_NO_LIMIT && $rows_count >= $rows_total) {
$cursor = FIELD_QUERY_COMPLETE;
}
}
return $return;
}
/**
* Sort helper for field_test_field_storage_query().
*
* Sort by object type and object id.
*/
function _field_test_field_storage_query_sort_helper($a, $b) {
if ($a->type == $b->type) {
if ($a->entity_id == $b->entity_id) {
return 0;