-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcals_importer.fieldcollections.inc
executable file
·245 lines (210 loc) · 7.28 KB
/
cals_importer.fieldcollections.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
206
207
208
209
210
211
212
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
<?php
/**
* @file
* populates various field collections associated with MARC XML parsing operations
*/
/**
* Populates Field Collection values
*
* @param $node
* the node passed to the parser
* @param $fields
* array of field collection values
*
*/
function _cals_importer_populate_repos_item_fieldcollections(&$node, $fields){
foreach($fields as $field) {
if(is_array($field['values']) && count($field['values']) > 0) {
$values = $field['values'];
foreach($values as $value) {
$e = entity_create('field_collection_item', $value);
// Attach the field_collection entity to the application node.
$e->setHostEntity('node', $node);
// Save the entity.
$e->save();
}
}
}
}
/**
* Deletes Field Collection values
*
* @param $node
* the node passed to the function
* @param $field
* string value corresponding to the drupal field collection field
* @ see also: http://drupal.stackexchange.com/questions/68765/how-to-properly-delete-a-field-collection
*/
function _cals_importer_delete_field_collection(&$node, $field) {
if (isset($node->{$field}[LANGUAGE_NONE]) ) {
//collection field values for multiple deletion!
foreach( $node->{$field}[LANGUAGE_NONE] as $v) {
//$ids[] = $v['value'];
// Only loads a value for field_file_resource type FCs
$fci = field_collection_item_load($v['value']);
if($fci) $fci->delete();
}
unset($node->{$field}[LANGUAGE_NONE]);
}
}
/**
* Deletes field collection values by entity_id
* @param $entity_id
*/
function _cals_importer_delete_field_collection_by_id($entity_id) {
entity_delete('field_collection_item', $entity_id);
}
/**
* @param $settings
* @param $form_state
* @return mixed
*/
function _cals_transfer_file_resource_entity_form($settings, &$form_state) {
$options = array();
foreach ($form_state['selection'] as $key => $nid) {
$node_entity = entity_metadata_wrapper('node', $nid);
$files = $node_entity->field_file_resource->value();
$destinations[$nid] = $nid . ' - ' . $node_entity->title->value();
foreach ($files as $file) {
$fc_entity = entity_metadata_wrapper('field_collection_item', $file);
$fields = $fc_entity->getPropertyInfo();
$format = $fc_entity->field_file_format->label();
$item_id = $fc_entity->item_id->value();
// $field_count = count($fields);
// $total = 0;
// foreach ($fields as $field) {
// if ( $fc_entity->$field->value() ) {
// ++$total;
// }
// }
$options[$item_id] = sprintf('NID: %d - %s - ItemID: %d', $nid, $format,
$item_id);
}
}
$form['file_resources'] = array(
'#type' => 'select',
'#title' => t('Select the File Attachment to transfer '),
'#options' => $options,
'#required' => TRUE,
'#multiple' => TRUE,
'#size' => count($options),
'#description' => t("NB: MSG"),
'#default_value' => isset($options[0]) ? $options[0] : '',
);
$form['destination_node'] = array(
'#type' => 'select',
'#title' => t('Select the destination Repository Item'),
'#options' => $destinations,
'#required' => TRUE,
'#size' => count($destinations),
'#description' => t("NB: MSG"),
'#default_value' => isset($destinations) ? reset($destinations) : NULL,
);
return $form;
}
/**
* @param $form
* @param $form_state
* @return array|bool
*/
function _cals_transfer_file_resource_entity_submit($form, $form_state) {
$return = array();
$return['file_resources'] = $form_state['values']['file_resources'];
$return['destination_node'] = $form_state['values']['destination_node'];
//Do not attempt a same-entity transfer
$selection_source_host = entity_load_single('field_collection_item',
$form_state['values']['file_resources']);
if ($selection_source_host->hostEntity()->nid == $return['destination_node']) {
drupal_set_message("Cannot transfer File Resource to the Repository Item from which it originates.",
'error');
drupal_goto(check_url($_SERVER['REDIRECT_URL'])); //Go back to the record
// set node path
return FALSE;
}
return $return;
}
/**
* @param stdClass $node
* @param array $context
* @return boolean
*/
function _cals_transfer_file_resource_entity(&$node, $context) {
//Only run this once for the group of selected nodes
if ($context['progress']['current'] > 1) {
return FALSE;
}
//Forbidden Drupal Entity Fields
$forbidden = array(
'item_id',
'uuid',
'url',
'feed_nid',
'feed_node',
'feeds_item_guid',
'feeds_item_url',
'host_entity',
'revision_id',
'field_name',
'archived'
);
//Load Destination Node from $context
$dest_node = node_load($context['destination_node']);
//Iterate over the selected transferrable file resources one-to-many
foreach ($context['file_resources'] as $transferrable_id) {
$transfer_fc_wrapped = entity_metadata_wrapper('field_collection_item',
$transferrable_id);
$source_node_wrapped = entity_metadata_wrapper('node', $transfer_fc_wrapped->host_entity->value());
//Get the possible fields from the File Resource field collection,
// excepting those that cannot be written except during construction.
$fields = $transfer_fc_wrapped->getPropertyInfo();
foreach ($forbidden as $forbid) {
unset($fields[$forbid]);
}
//Find our File Resource by item_id in situ
foreach ($source_node_wrapped->field_file_resource->value() as $delta =>
$item) {
//Check for a match on this round
if ($item->item_id == $transferrable_id) {
//The transfer target entity was found
//Create a new File Resource instance, set to Destination Node & wrap-it
$new_entity = entity_create('field_collection_item', array('field_name' => 'field_file_resource'));
$new_entity->setHostEntity('node', $dest_node);
$new_entity_wrapped = entity_metadata_wrapper('field_collection_item',
$new_entity);
//Loop through allowed fields and extract the values
foreach ($fields as $index => $field) {
try {
$new_entity_wrapped->$index->set($transfer_fc_wrapped->$index->value
());
} catch (Exception $exception) {
drupal_set_message($exception->getMessage(), 'error');
}
}
//All done: save Destination Node with new entity referenced.
node_save($dest_node);
//Unset the old instance to avoid duplicates
unset($source_node_wrapped->field_file_resource[$delta]);
entity_save($source_node_wrapped->type(), $source_node_wrapped);
}
}
//Might not have format set for certain items
try {
$format = $transfer_fc_wrapped->field_file_format->label();
} catch (EntityMetadataWrapperException $exception) {
$format = NULL;
watchdog('entity_api', $exception->getMessage());
}
//Build results array for message output
$result_info = array(
$transfer_fc_wrapped->item_id->value(),
$format,
substr($source_node_wrapped->title->value(), 0, 30),
$source_node_wrapped->nid->value(),
substr($dest_node->title, 0, 30),
$dest_node->nid
);
$message = vsprintf("Item ID %d (%s) was transferred from %s... (%d) \r\n to
%s... (%d)", $result_info);
drupal_set_message($message);
} //End selected $transferrable foreach
}