1 |
31a5a6d6
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* Class for field_collection_item entities.
|
5 |
|
|
*/
|
6 |
|
|
class FieldCollectionItemEntity extends Entity {
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
a1cafe7e
|
Assos Assos
|
* Field Collection field info.
|
10 |
31a5a6d6
|
Assos Assos
|
*
|
11 |
|
|
* @var array
|
12 |
|
|
*/
|
13 |
|
|
protected $fieldInfo;
|
14 |
|
|
|
15 |
|
|
/**
|
16 |
|
|
* The host entity object.
|
17 |
|
|
*
|
18 |
|
|
* @var object
|
19 |
|
|
*/
|
20 |
|
|
protected $hostEntity;
|
21 |
|
|
|
22 |
|
|
/**
|
23 |
|
|
* The host entity ID.
|
24 |
|
|
*
|
25 |
|
|
* @var integer
|
26 |
|
|
*/
|
27 |
|
|
protected $hostEntityId;
|
28 |
|
|
|
29 |
|
|
/**
|
30 |
|
|
* The host entity revision ID if this is not the default revision.
|
31 |
|
|
*
|
32 |
|
|
* @var integer
|
33 |
|
|
*/
|
34 |
|
|
protected $hostEntityRevisionId;
|
35 |
|
|
|
36 |
|
|
/**
|
37 |
|
|
* The host entity type.
|
38 |
|
|
*
|
39 |
|
|
* @var string
|
40 |
|
|
*/
|
41 |
|
|
protected $hostEntityType;
|
42 |
|
|
|
43 |
|
|
/**
|
44 |
|
|
* The language under which the field collection item is stored.
|
45 |
|
|
*
|
46 |
|
|
* @var string
|
47 |
|
|
*/
|
48 |
|
|
protected $langcode = LANGUAGE_NONE;
|
49 |
|
|
|
50 |
|
|
/**
|
51 |
|
|
* Entity ID.
|
52 |
|
|
*
|
53 |
|
|
* @var integer
|
54 |
|
|
*/
|
55 |
|
|
public $item_id;
|
56 |
|
|
|
57 |
|
|
/**
|
58 |
a1cafe7e
|
Assos Assos
|
* Field Collection revision ID.
|
59 |
31a5a6d6
|
Assos Assos
|
*
|
60 |
|
|
* @var integer
|
61 |
|
|
*/
|
62 |
|
|
public $revision_id;
|
63 |
|
|
|
64 |
|
|
/**
|
65 |
|
|
* The name of the field-collection field this item is associated with.
|
66 |
|
|
*
|
67 |
|
|
* @var string
|
68 |
|
|
*/
|
69 |
|
|
public $field_name;
|
70 |
|
|
|
71 |
|
|
/**
|
72 |
|
|
* Whether this revision is the default revision.
|
73 |
|
|
*
|
74 |
|
|
* @var bool
|
75 |
|
|
*/
|
76 |
|
|
public $default_revision = TRUE;
|
77 |
|
|
|
78 |
|
|
/**
|
79 |
|
|
* Whether the field collection item is archived, i.e. not in use.
|
80 |
|
|
*
|
81 |
|
|
* @see FieldCollectionItemEntity::isInUse()
|
82 |
|
|
* @var bool
|
83 |
|
|
*/
|
84 |
|
|
public $archived = FALSE;
|
85 |
|
|
|
86 |
|
|
/**
|
87 |
|
|
* Constructs the entity object.
|
88 |
|
|
*/
|
89 |
|
|
public function __construct(array $values = array(), $entityType = NULL) {
|
90 |
|
|
parent::__construct($values, 'field_collection_item');
|
91 |
|
|
// Workaround issues http://drupal.org/node/1084268 and
|
92 |
|
|
// http://drupal.org/node/1264440:
|
93 |
|
|
// Check if the required property is set before checking for the field's
|
94 |
|
|
// type. If the property is not set, we are hitting a PDO or a core's bug.
|
95 |
|
|
// FIXME: Remove when #1264440 is fixed and the required PHP version is
|
96 |
|
|
// properly identified and documented in the module documentation.
|
97 |
|
|
if (isset($this->field_name)) {
|
98 |
|
|
// Ok, we have the field name property, we can proceed and check the field's type
|
99 |
|
|
$field_info = $this->fieldInfo();
|
100 |
|
|
if (!$field_info || $field_info['type'] != 'field_collection') {
|
101 |
|
|
throw new Exception("Invalid field name given: {$this->field_name} is not a Field Collection field.");
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
/**
|
107 |
|
|
* Provides info about the field on the host entity, which embeds this
|
108 |
|
|
* field collection item.
|
109 |
|
|
*/
|
110 |
|
|
public function fieldInfo() {
|
111 |
|
|
return field_info_field($this->field_name);
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
/**
|
115 |
|
|
* Provides info of the field instance containing the reference to this
|
116 |
|
|
* field collection item.
|
117 |
|
|
*/
|
118 |
|
|
public function instanceInfo() {
|
119 |
|
|
if ($this->fetchHostDetails()) {
|
120 |
|
|
return field_info_instance($this->hostEntityType(), $this->field_name, $this->hostEntityBundle());
|
121 |
|
|
}
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
/**
|
125 |
|
|
* Returns the field instance label translated to interface language.
|
126 |
|
|
*/
|
127 |
|
|
public function translatedInstanceLabel($langcode = NULL) {
|
128 |
|
|
if ($info = $this->instanceInfo()) {
|
129 |
|
|
if (module_exists('i18n_field')) {
|
130 |
5e632cae
|
Assos Assos
|
return i18n_string("field:{$this->field_name}:{$info['bundle']}:label", $info['label'], array('langcode' => $langcode, 'sanitize' => FALSE));
|
131 |
31a5a6d6
|
Assos Assos
|
}
|
132 |
|
|
return $info['label'];
|
133 |
|
|
}
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
/**
|
137 |
|
|
* Specifies the default label, which is picked up by label() by default.
|
138 |
|
|
*/
|
139 |
|
|
public function defaultLabel() {
|
140 |
|
|
if ($this->fetchHostDetails()) {
|
141 |
|
|
$field = $this->fieldInfo();
|
142 |
|
|
$label = $this->translatedInstanceLabel();
|
143 |
|
|
$host = $this->hostEntity();
|
144 |
|
|
|
145 |
|
|
if ($new_label = module_invoke_all('field_collection_item_label', $this, $host, $field, $label)) {
|
146 |
|
|
return array_pop($new_label);
|
147 |
|
|
}
|
148 |
a1cafe7e
|
Assos Assos
|
|
149 |
|
|
if ($field['cardinality'] == 1) {
|
150 |
31a5a6d6
|
Assos Assos
|
return $label;
|
151 |
|
|
}
|
152 |
a1cafe7e
|
Assos Assos
|
|
153 |
|
|
if ($this->item_id) {
|
154 |
31a5a6d6
|
Assos Assos
|
return t('!instance_label @count', array('!instance_label' => $label, '@count' => $this->delta() + 1));
|
155 |
|
|
}
|
156 |
a1cafe7e
|
Assos Assos
|
|
157 |
|
|
return t('New !instance_label', array('!instance_label' => $label));
|
158 |
31a5a6d6
|
Assos Assos
|
}
|
159 |
|
|
return t('Unconnected field collection item');
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
/**
|
163 |
|
|
* Returns the path used to view the entity.
|
164 |
|
|
*/
|
165 |
|
|
public function path() {
|
166 |
|
|
if ($this->item_id) {
|
167 |
|
|
return field_collection_field_get_path($this->fieldInfo()) . '/' . $this->item_id;
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
/**
|
172 |
|
|
* Returns the URI as returned by entity_uri().
|
173 |
|
|
*/
|
174 |
|
|
public function defaultUri() {
|
175 |
|
|
return array(
|
176 |
|
|
'path' => $this->path(),
|
177 |
|
|
);
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
/**
|
181 |
|
|
* Sets the host entity. Only possible during creation of a item.
|
182 |
|
|
*
|
183 |
|
|
* @param $create_link
|
184 |
|
|
* (optional) Whether a field-item linking the host entity to the field
|
185 |
|
|
* collection item should be created.
|
186 |
|
|
*/
|
187 |
|
|
public function setHostEntity($entity_type, $entity, $langcode = LANGUAGE_NONE, $create_link = TRUE) {
|
188 |
|
|
if (!empty($this->is_new)) {
|
189 |
|
|
$this->hostEntityType = $entity_type;
|
190 |
|
|
$this->hostEntity = $entity;
|
191 |
|
|
$this->langcode = $langcode;
|
192 |
|
|
|
193 |
|
|
list($this->hostEntityId, $this->hostEntityRevisionId) = entity_extract_ids($this->hostEntityType, $this->hostEntity);
|
194 |
|
|
// If the host entity is not saved yet, set the id to FALSE. So
|
195 |
|
|
// fetchHostDetails() does not try to load the host entity details.
|
196 |
950416da
|
Assos Assos
|
// Checking value of $this->hostEntityId.
|
197 |
31a5a6d6
|
Assos Assos
|
if (!isset($this->hostEntityId)) {
|
198 |
|
|
$this->hostEntityId = FALSE;
|
199 |
|
|
}
|
200 |
|
|
// We are create a new field collection for a non-default entity, thus
|
201 |
|
|
// set archived to TRUE.
|
202 |
|
|
if (!entity_revision_is_default($entity_type, $entity)) {
|
203 |
|
|
$this->hostEntityId = FALSE;
|
204 |
|
|
$this->archived = TRUE;
|
205 |
|
|
}
|
206 |
|
|
if ($create_link) {
|
207 |
|
|
$entity->{$this->field_name}[$this->langcode][] = array('entity' => $this);
|
208 |
|
|
}
|
209 |
|
|
}
|
210 |
|
|
else {
|
211 |
|
|
throw new Exception('The host entity may be set only during creation of a field collection item.');
|
212 |
|
|
}
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
/**
|
216 |
|
|
* Updates the wrapped host entity object.
|
217 |
|
|
*
|
218 |
|
|
* @param object $entity
|
219 |
|
|
* Host entity.
|
220 |
|
|
* @param string $host_entity_type
|
221 |
|
|
* The entity type of the entity the field collection is attached to.
|
222 |
|
|
*/
|
223 |
|
|
public function updateHostEntity($entity, $host_entity_type = NULL) {
|
224 |
a1cafe7e
|
Assos Assos
|
$this->fetchHostDetails($entity);
|
225 |
31a5a6d6
|
Assos Assos
|
// If it isn't possible to retrieve hostEntityType due to the fact that it's
|
226 |
|
|
// not saved in the DB yet then fill in info about the hostEntity manually.
|
227 |
|
|
// This happens when creating a new revision of a field collection entity
|
228 |
|
|
// and it needs to relate to the new revision of the host entity.
|
229 |
a1cafe7e
|
Assos Assos
|
if (!$this->hostEntityType || isset($entity->tid)) {
|
230 |
31a5a6d6
|
Assos Assos
|
$this->hostEntityType = $host_entity_type;
|
231 |
|
|
$this->hostEntity = $entity;
|
232 |
|
|
list($this->hostEntityId, $this->hostEntityRevisionId) = entity_extract_ids($this->hostEntityType, $this->hostEntity);
|
233 |
|
|
}
|
234 |
|
|
list($recieved_id) = entity_extract_ids($this->hostEntityType, $entity);
|
235 |
|
|
|
236 |
a1cafe7e
|
Assos Assos
|
if (!empty($this->hostEntityId) && $this->isInUse()) {
|
237 |
|
|
if (is_array($this->hostEntityId)) {
|
238 |
|
|
$current_id = in_array($recieved_id, $this->hostEntityId)
|
239 |
|
|
? $recieved_id
|
240 |
|
|
: FALSE;
|
241 |
|
|
}
|
242 |
|
|
else {
|
243 |
|
|
$current_id = $this->hostEntityId;
|
244 |
|
|
}
|
245 |
31a5a6d6
|
Assos Assos
|
}
|
246 |
|
|
else {
|
247 |
|
|
$current_host = entity_revision_load($this->hostEntityType, $this->hostEntityRevisionId);
|
248 |
a1cafe7e
|
Assos Assos
|
list($current_id) = $current_host ? entity_extract_ids($this->hostEntityType, $current_host) : array($recieved_id);
|
249 |
31a5a6d6
|
Assos Assos
|
}
|
250 |
|
|
|
251 |
|
|
if ($current_id == $recieved_id) {
|
252 |
|
|
$this->hostEntity = $entity;
|
253 |
|
|
$delta = $this->delta();
|
254 |
|
|
if (isset($entity->{$this->field_name}[$this->langcode()][$delta]['entity'])) {
|
255 |
|
|
$entity->{$this->field_name}[$this->langcode()][$delta]['entity'] = $this;
|
256 |
|
|
}
|
257 |
|
|
}
|
258 |
|
|
else {
|
259 |
|
|
throw new Exception('The host entity cannot be changed.');
|
260 |
|
|
}
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
/**
|
264 |
|
|
* Returns the host entity, which embeds this field collection item.
|
265 |
|
|
*/
|
266 |
|
|
public function hostEntity() {
|
267 |
|
|
if ($this->fetchHostDetails()) {
|
268 |
|
|
if (!isset($this->hostEntity) && $this->isInUse()) {
|
269 |
|
|
$this->hostEntity = entity_load_single($this->hostEntityType, $this->hostEntityId);
|
270 |
|
|
}
|
271 |
|
|
elseif (!isset($this->hostEntity) && $this->hostEntityRevisionId) {
|
272 |
|
|
$this->hostEntity = entity_revision_load($this->hostEntityType, $this->hostEntityRevisionId);
|
273 |
|
|
}
|
274 |
|
|
return $this->hostEntity;
|
275 |
|
|
}
|
276 |
|
|
}
|
277 |
|
|
|
278 |
|
|
/**
|
279 |
|
|
* Returns the entity type of the host entity, which embeds this
|
280 |
|
|
* field collection item.
|
281 |
|
|
*/
|
282 |
|
|
public function hostEntityType() {
|
283 |
|
|
if ($this->fetchHostDetails()) {
|
284 |
|
|
return $this->hostEntityType;
|
285 |
|
|
}
|
286 |
|
|
}
|
287 |
|
|
|
288 |
|
|
/**
|
289 |
|
|
* Returns the id of the host entity, which embeds this field collection item.
|
290 |
|
|
*/
|
291 |
|
|
public function hostEntityId() {
|
292 |
|
|
if ($this->fetchHostDetails()) {
|
293 |
|
|
if (!$this->hostEntityId && $this->hostEntityRevisionId) {
|
294 |
|
|
$this->hostEntityId = entity_id($this->hostEntityType, $this->hostEntity());
|
295 |
|
|
}
|
296 |
|
|
return $this->hostEntityId;
|
297 |
|
|
}
|
298 |
|
|
}
|
299 |
|
|
|
300 |
|
|
/**
|
301 |
|
|
* Returns the bundle of the host entity, which embeds this field collection
|
302 |
|
|
* item.
|
303 |
|
|
*/
|
304 |
|
|
public function hostEntityBundle() {
|
305 |
|
|
if ($entity = $this->hostEntity()) {
|
306 |
|
|
list($id, $rev_id, $bundle) = entity_extract_ids($this->hostEntityType, $entity);
|
307 |
|
|
return $bundle;
|
308 |
|
|
}
|
309 |
|
|
}
|
310 |
|
|
|
311 |
a1cafe7e
|
Assos Assos
|
/**
|
312 |
|
|
* Collects info about the field collection's host.
|
313 |
|
|
*
|
314 |
|
|
* @param $hostEntity
|
315 |
|
|
* The host entity object. (optional)
|
316 |
|
|
*/
|
317 |
|
|
protected function fetchHostDetails($hostEntity = NULL) {
|
318 |
950416da
|
Assos Assos
|
if (!isset($this->hostEntityId) || (!$this->hostEntityId && $this->hostEntityRevisionId)) {
|
319 |
31a5a6d6
|
Assos Assos
|
if ($this->item_id) {
|
320 |
|
|
// For saved field collections, query the field data to determine the
|
321 |
|
|
// right host entity.
|
322 |
|
|
$query = new EntityFieldQuery();
|
323 |
a1cafe7e
|
Assos Assos
|
$field_info = $this->fieldInfo();
|
324 |
|
|
$query->fieldCondition($field_info, 'revision_id', $this->revision_id);
|
325 |
|
|
if ($hostEntity) {
|
326 |
|
|
$entity_type = key($field_info['bundles']);
|
327 |
|
|
$bundle = current($field_info['bundles'][$entity_type]);
|
328 |
|
|
$entity_info = entity_get_info($entity_type);
|
329 |
|
|
$key = $entity_info['entity keys']['id'];
|
330 |
|
|
$query->entityCondition('entity_type', $entity_type);
|
331 |
|
|
$query->entityCondition('entity_id', $hostEntity->{$key});
|
332 |
|
|
$query->entityCondition('bundle', $bundle);
|
333 |
5e632cae
|
Assos Assos
|
// Only filter by language if this entity type has a language key that
|
334 |
|
|
// has a corresponding field in its base table.
|
335 |
|
|
if (!empty($entity_info['entity keys']['language']) && !empty($entity_info['schema_fields_sql']['base table']) && in_array($entity_info['entity keys']['language'], $entity_info['schema_fields_sql']['base table'], TRUE)) {
|
336 |
|
|
$query->propertyCondition($entity_info['entity keys']['language'], $hostEntity->{$entity_info['entity keys']['language']});
|
337 |
a1cafe7e
|
Assos Assos
|
}
|
338 |
|
|
}
|
339 |
|
|
$query->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
|
340 |
31a5a6d6
|
Assos Assos
|
if (!$this->isInUse()) {
|
341 |
|
|
$query->age(FIELD_LOAD_REVISION);
|
342 |
|
|
}
|
343 |
|
|
$result = $query->execute();
|
344 |
950416da
|
Assos Assos
|
if ($result) {
|
345 |
|
|
$this->hostEntityType = key($result);
|
346 |
|
|
$data = current($result);
|
347 |
|
|
|
348 |
|
|
if ($this->isInUse()) {
|
349 |
|
|
$data_array_keys = array_keys($data);
|
350 |
|
|
$this->hostEntityId = $data ? end($data_array_keys) : FALSE;
|
351 |
|
|
$this->hostEntityRevisionId = FALSE;
|
352 |
|
|
}
|
353 |
|
|
// If we are querying for revisions, we get the revision ID.
|
354 |
|
|
else {
|
355 |
|
|
$data_array_keys = array_keys($data);
|
356 |
|
|
$this->hostEntityId = FALSE;
|
357 |
|
|
$this->hostEntityRevisionId = $data ? end($data_array_keys) : FALSE;
|
358 |
|
|
}
|
359 |
31a5a6d6
|
Assos Assos
|
}
|
360 |
|
|
else {
|
361 |
950416da
|
Assos Assos
|
// No host entity available yet.
|
362 |
31a5a6d6
|
Assos Assos
|
$this->hostEntityId = FALSE;
|
363 |
|
|
}
|
364 |
|
|
}
|
365 |
|
|
else {
|
366 |
|
|
// No host entity available yet.
|
367 |
|
|
$this->hostEntityId = FALSE;
|
368 |
|
|
}
|
369 |
|
|
}
|
370 |
|
|
return !empty($this->hostEntityId) || !empty($this->hostEntity) || !empty($this->hostEntityRevisionId);
|
371 |
|
|
}
|
372 |
|
|
|
373 |
|
|
/**
|
374 |
|
|
* Determines the $delta of the reference pointing to this field collection
|
375 |
|
|
* item.
|
376 |
|
|
*/
|
377 |
|
|
public function delta() {
|
378 |
|
|
if (($entity = $this->hostEntity()) && isset($entity->{$this->field_name})) {
|
379 |
|
|
foreach ($entity->{$this->field_name} as $langcode => &$data) {
|
380 |
|
|
if (!empty($data)) {
|
381 |
|
|
foreach ($data as $delta => $item) {
|
382 |
|
|
if (isset($item['value']) && $item['value'] == $this->item_id) {
|
383 |
|
|
$this->langcode = $langcode;
|
384 |
|
|
return $delta;
|
385 |
|
|
}
|
386 |
a1cafe7e
|
Assos Assos
|
|
387 |
|
|
if (isset($item['entity']) && $item['entity'] === $this) {
|
388 |
31a5a6d6
|
Assos Assos
|
$this->langcode = $langcode;
|
389 |
|
|
return $delta;
|
390 |
|
|
}
|
391 |
|
|
}
|
392 |
|
|
}
|
393 |
|
|
}
|
394 |
|
|
// If we don't find the delta in the current values (cause the item
|
395 |
|
|
// is being deleted, for example), we search the delta in the originalcontent.
|
396 |
|
|
if (!empty($entity->original)) {
|
397 |
|
|
foreach ($entity->original->{$this->field_name} as $langcode => &$data) {
|
398 |
|
|
if (!empty($data)) {
|
399 |
|
|
foreach ($data as $delta => $item) {
|
400 |
|
|
if (isset($item['value']) && $item['value'] == $this->item_id) {
|
401 |
|
|
$this->langcode = $langcode;
|
402 |
|
|
return $delta;
|
403 |
|
|
}
|
404 |
a1cafe7e
|
Assos Assos
|
|
405 |
|
|
if (isset($item['entity']) && $item['entity'] === $this) {
|
406 |
31a5a6d6
|
Assos Assos
|
$this->langcode = $langcode;
|
407 |
|
|
return $delta;
|
408 |
|
|
}
|
409 |
|
|
}
|
410 |
|
|
}
|
411 |
|
|
}
|
412 |
|
|
}
|
413 |
|
|
}
|
414 |
|
|
}
|
415 |
|
|
|
416 |
|
|
/**
|
417 |
|
|
* Determines the language code under which the item is stored.
|
418 |
|
|
*/
|
419 |
|
|
public function langcode() {
|
420 |
a1cafe7e
|
Assos Assos
|
if (empty($this->langcode) || $this->delta() === NULL) {
|
421 |
31a5a6d6
|
Assos Assos
|
$this->langcode = field_collection_entity_language('field_collection_item', $this);
|
422 |
|
|
}
|
423 |
|
|
|
424 |
|
|
if (empty($this->langcode) || ($this->langcode != LANGUAGE_NONE && (!module_exists('entity_translation') || !entity_translation_enabled('field_collection_item')))) {
|
425 |
|
|
$this->langcode = LANGUAGE_NONE;
|
426 |
|
|
}
|
427 |
|
|
|
428 |
|
|
return $this->langcode;
|
429 |
|
|
}
|
430 |
|
|
|
431 |
|
|
/**
|
432 |
|
|
* Determines whether this field collection item revision is in use.
|
433 |
|
|
*
|
434 |
a1cafe7e
|
Assos Assos
|
* Field Collection items may be contained in from non-default host entity
|
435 |
31a5a6d6
|
Assos Assos
|
* revisions. If the field collection item does not appear in the default
|
436 |
|
|
* host entity revision, the item is actually not used by default and so
|
437 |
|
|
* marked as 'archived'.
|
438 |
|
|
* If the field collection item appears in the default revision of the host
|
439 |
|
|
* entity, the default revision of the field collection item is in use there
|
440 |
|
|
* and the collection is not marked as archived.
|
441 |
|
|
*/
|
442 |
|
|
public function isInUse() {
|
443 |
|
|
return $this->default_revision && !$this->archived;
|
444 |
|
|
}
|
445 |
|
|
|
446 |
|
|
/**
|
447 |
|
|
* Save the field collection item.
|
448 |
|
|
*
|
449 |
|
|
* By default, always save the host entity, so modules are able to react
|
450 |
|
|
* upon changes to the content of the host and any 'last updated' dates of
|
451 |
|
|
* entities get updated.
|
452 |
|
|
*
|
453 |
|
|
* For creating an item a host entity has to be specified via setHostEntity()
|
454 |
|
|
* before this function is invoked. For the link between the entities to be
|
455 |
|
|
* fully established, the host entity object has to be updated to include a
|
456 |
|
|
* reference on this field collection item during saving. So do not skip
|
457 |
|
|
* saving the host for creating items.
|
458 |
|
|
*
|
459 |
|
|
* @param $skip_host_save
|
460 |
|
|
* (internal) If TRUE is passed, the host entity is not saved automatically
|
461 |
|
|
* and therefore no link is created between the host and the item or
|
462 |
|
|
* revision updates might be skipped. Use with care.
|
463 |
|
|
*/
|
464 |
|
|
public function save($skip_host_save = FALSE) {
|
465 |
|
|
// Make sure we have a host entity during creation.
|
466 |
|
|
if (!empty($this->is_new) && !(isset($this->hostEntityId) || isset($this->hostEntity) || isset($this->hostEntityRevisionId))) {
|
467 |
a1cafe7e
|
Assos Assos
|
throw new Exception('Unable to create a field collection item without a given host entity.');
|
468 |
31a5a6d6
|
Assos Assos
|
}
|
469 |
|
|
|
470 |
|
|
// Copy the values of translatable fields for a new field collection item.
|
471 |
a1cafe7e
|
Assos Assos
|
if (!empty($this->is_new) && field_collection_item_is_translatable() && $this->langcode() == LANGUAGE_NONE) {
|
472 |
31a5a6d6
|
Assos Assos
|
$this->copyTranslations();
|
473 |
|
|
}
|
474 |
|
|
|
475 |
|
|
// Only save directly if we are told to skip saving the host entity. Else,
|
476 |
|
|
// we always save via the host as saving the host might trigger saving
|
477 |
|
|
// field collection items anyway (e.g. if a new revision is created).
|
478 |
|
|
if ($skip_host_save) {
|
479 |
|
|
return entity_get_controller($this->entityType)->save($this);
|
480 |
|
|
}
|
481 |
|
|
|
482 |
a1cafe7e
|
Assos Assos
|
$host_entity = $this->hostEntity();
|
483 |
|
|
if (!$host_entity) {
|
484 |
|
|
throw new Exception('Unable to save a field collection item without a valid reference to a host entity.');
|
485 |
|
|
}
|
486 |
|
|
// If this is creating a new revision, also do so for the host entity.
|
487 |
|
|
if (!empty($this->revision) || !empty($this->is_new_revision)) {
|
488 |
|
|
$host_entity->revision = TRUE;
|
489 |
|
|
if (!empty($this->default_revision)) {
|
490 |
|
|
entity_revision_set_default($this->hostEntityType, $host_entity);
|
491 |
|
|
}
|
492 |
|
|
}
|
493 |
|
|
// Set the host entity reference, so the item will be saved with the host.
|
494 |
|
|
// @see field_collection_field_presave()
|
495 |
|
|
$delta = $this->delta();
|
496 |
|
|
if (isset($delta)) {
|
497 |
|
|
$host_entity->{$this->field_name}[$this->langcode()][$delta] = array('entity' => $this);
|
498 |
31a5a6d6
|
Assos Assos
|
}
|
499 |
a1cafe7e
|
Assos Assos
|
else {
|
500 |
|
|
$host_entity->{$this->field_name}[$this->langcode()][] = array('entity' => $this);
|
501 |
|
|
}
|
502 |
|
|
|
503 |
|
|
return entity_save($this->hostEntityType, $host_entity);
|
504 |
31a5a6d6
|
Assos Assos
|
}
|
505 |
|
|
|
506 |
|
|
/**
|
507 |
|
|
* Deletes the field collection item and the reference in the host entity.
|
508 |
|
|
*/
|
509 |
950416da
|
Assos Assos
|
public function delete($skip_host_save = FALSE) {
|
510 |
31a5a6d6
|
Assos Assos
|
parent::delete();
|
511 |
950416da
|
Assos Assos
|
if (!$skip_host_save) {
|
512 |
|
|
$this->deleteHostEntityReference();
|
513 |
|
|
}
|
514 |
31a5a6d6
|
Assos Assos
|
}
|
515 |
|
|
|
516 |
|
|
/**
|
517 |
|
|
* Copies text to all languages the collection item has a translation for.
|
518 |
|
|
*
|
519 |
|
|
* @param $source_language
|
520 |
|
|
* Language code to copy the text from.
|
521 |
|
|
*/
|
522 |
|
|
public function copyTranslations($source_language = NULL) {
|
523 |
|
|
// Get a handler for Entity Translation if there is one.
|
524 |
|
|
$host_et_handler = NULL;
|
525 |
|
|
if (module_exists('entity_translation')) {
|
526 |
|
|
$host_et_handler = entity_translation_get_handler($this->hostEntityType(), $this->hostEntity());
|
527 |
|
|
}
|
528 |
|
|
if (is_null($host_et_handler)) {
|
529 |
|
|
return;
|
530 |
|
|
}
|
531 |
|
|
|
532 |
|
|
$host_languages = array_keys($host_et_handler->getTranslations()->data);
|
533 |
|
|
if (empty($host_languages)) {
|
534 |
|
|
$host_languages = array(entity_language($this->hostEntityType(), $this->hostEntity()));
|
535 |
|
|
}
|
536 |
|
|
$source_language = isset($source_language) ? $source_language : $host_et_handler->getLanguage();
|
537 |
|
|
$target_languages = array_diff($host_languages, array($source_language));
|
538 |
|
|
$fields_instances = array_keys(field_info_instances('field_collection_item', $this->field_name));
|
539 |
|
|
$fields = field_info_fields();
|
540 |
|
|
|
541 |
|
|
foreach ($fields_instances as $translatable_field) {
|
542 |
|
|
if ($fields[$translatable_field]['translatable'] == 1) {
|
543 |
|
|
foreach ($target_languages as $langcode) {
|
544 |
a1cafe7e
|
Assos Assos
|
// Source (translatable_field) is set, therefore continue
|
545 |
|
|
// processing.
|
546 |
|
|
if (isset($this->{$translatable_field}[$source_language])
|
547 |
|
|
&& !isset($this->{$translatable_field}[$langcode])) {
|
548 |
|
|
// Destination (translatable_field) is not set, therefore safe to
|
549 |
|
|
// copy the translation.
|
550 |
|
|
$this->{$translatable_field}[$langcode] = $this->{$translatable_field}[$source_language];
|
551 |
31a5a6d6
|
Assos Assos
|
}
|
552 |
|
|
}
|
553 |
|
|
if ($source_language == LANGUAGE_NONE && count($this->{$translatable_field}) > 1) {
|
554 |
|
|
$this->{$translatable_field}[$source_language] = NULL;
|
555 |
|
|
}
|
556 |
|
|
}
|
557 |
|
|
}
|
558 |
|
|
}
|
559 |
|
|
|
560 |
|
|
/**
|
561 |
|
|
* Deletes the host entity's reference of the field collection item.
|
562 |
|
|
*/
|
563 |
|
|
protected function deleteHostEntityReference() {
|
564 |
|
|
$delta = $this->delta();
|
565 |
|
|
if ($this->item_id && isset($delta)) {
|
566 |
|
|
unset($this->hostEntity->{$this->field_name}[$this->langcode()][$delta]);
|
567 |
|
|
// Do not save when the host entity is being deleted. See
|
568 |
|
|
// field_collection_field_delete().
|
569 |
|
|
if (empty($this->hostEntity->field_collection_deleting)) {
|
570 |
|
|
entity_save($this->hostEntityType(), $this->hostEntity());
|
571 |
|
|
}
|
572 |
|
|
}
|
573 |
|
|
}
|
574 |
|
|
|
575 |
|
|
/**
|
576 |
|
|
* Intelligently delete a field collection item revision.
|
577 |
|
|
*
|
578 |
|
|
* If a host entity is revisioned with its field collection items, deleting
|
579 |
|
|
* a field collection item on the default revision of the host should not
|
580 |
|
|
* delete the collection item from archived revisions too. Instead, we delete
|
581 |
|
|
* the current default revision and archive the field collection.
|
582 |
|
|
*/
|
583 |
|
|
public function deleteRevision($skip_host_update = FALSE) {
|
584 |
|
|
if (!$this->revision_id) {
|
585 |
|
|
return;
|
586 |
|
|
}
|
587 |
|
|
|
588 |
|
|
if (!$skip_host_update) {
|
589 |
|
|
// Just remove the item from the host, which cares about deleting the
|
590 |
|
|
// item (depending on whether the update creates a new revision).
|
591 |
|
|
$this->deleteHostEntityReference();
|
592 |
|
|
}
|
593 |
|
|
|
594 |
|
|
if (!$this->isDefaultRevision()) {
|
595 |
|
|
entity_revision_delete('field_collection_item', $this->revision_id);
|
596 |
|
|
}
|
597 |
|
|
// If deleting the default revision, take care!
|
598 |
|
|
else {
|
599 |
|
|
$row = db_select('field_collection_item_revision', 'r')
|
600 |
|
|
->fields('r')
|
601 |
|
|
->condition('item_id', $this->item_id)
|
602 |
|
|
->condition('revision_id', $this->revision_id, '<>')
|
603 |
|
|
->execute()
|
604 |
|
|
->fetchAssoc();
|
605 |
|
|
|
606 |
|
|
if ($row) {
|
607 |
|
|
// Make the other revision the default revision and archive the item.
|
608 |
|
|
db_update('field_collection_item')
|
609 |
|
|
->fields(array('archived' => 1, 'revision_id' => $row['revision_id']))
|
610 |
|
|
->condition('item_id', $this->item_id)
|
611 |
|
|
->execute();
|
612 |
5e632cae
|
Assos Assos
|
|
613 |
|
|
// Let other modules know about the archived item.
|
614 |
|
|
entity_get_controller('field_collection_item')->invoke('archive', $this);
|
615 |
|
|
|
616 |
31a5a6d6
|
Assos Assos
|
entity_get_controller('field_collection_item')->resetCache(array($this->item_id));
|
617 |
|
|
entity_revision_delete('field_collection_item', $this->revision_id);
|
618 |
|
|
}
|
619 |
a1cafe7e
|
Assos Assos
|
else {
|
620 |
31a5a6d6
|
Assos Assos
|
// Delete if there is no existing revision or translation to be saved.
|
621 |
950416da
|
Assos Assos
|
$this->delete($skip_host_update);
|
622 |
31a5a6d6
|
Assos Assos
|
}
|
623 |
|
|
}
|
624 |
|
|
}
|
625 |
|
|
|
626 |
|
|
/**
|
627 |
|
|
* Export the field collection item.
|
628 |
|
|
*
|
629 |
|
|
* Since field collection entities are not directly exportable (i.e., do not
|
630 |
|
|
* have 'exportable' set to TRUE in hook_entity_info()) and since Features
|
631 |
|
|
* calls this method when exporting the field collection as a field attached
|
632 |
|
|
* to another entity, we return the export in the format expected by
|
633 |
|
|
* Features, rather than in the normal Entity::export() format.
|
634 |
|
|
*/
|
635 |
|
|
public function export($prefix = '') {
|
636 |
|
|
// Based on code in EntityDefaultFeaturesController::export_render().
|
637 |
|
|
$export = "entity_import('" . $this->entityType() . "', '";
|
638 |
|
|
$export .= addcslashes(parent::export(), '\\\'');
|
639 |
|
|
$export .= "')";
|
640 |
|
|
return $export;
|
641 |
|
|
}
|
642 |
|
|
|
643 |
|
|
/**
|
644 |
|
|
* Generate an array for rendering the field collection item.
|
645 |
|
|
*/
|
646 |
|
|
public function view($view_mode = 'full', $langcode = NULL, $page = NULL) {
|
647 |
|
|
// Allow modules to change the view mode.
|
648 |
|
|
$view_mode = key(entity_view_mode_prepare($this->entityType, array($this->item_id => $this), $view_mode, $langcode));
|
649 |
|
|
return parent::view($view_mode, $langcode, $page);
|
650 |
|
|
}
|
651 |
|
|
|
652 |
|
|
/**
|
653 |
|
|
* Magic method to only serialize what's necessary.
|
654 |
|
|
*/
|
655 |
|
|
public function __sleep() {
|
656 |
|
|
$vars = get_object_vars($this);
|
657 |
5e632cae
|
Assos Assos
|
unset($vars['entityInfo'], $vars['idKey'], $vars['nameKey'], $vars['statusKey'], $vars['fieldInfo']);
|
658 |
31a5a6d6
|
Assos Assos
|
// Also do not serialize the host entity, but only if it has already an id.
|
659 |
|
|
if ($this->hostEntity && ($this->hostEntityId || $this->hostEntityRevisionId)) {
|
660 |
|
|
unset($vars['hostEntity']);
|
661 |
|
|
}
|
662 |
|
|
|
663 |
|
|
// Also key the returned array with the variable names so the method may
|
664 |
|
|
// be easily overridden and customized.
|
665 |
|
|
return drupal_map_assoc(array_keys($vars));
|
666 |
|
|
}
|
667 |
|
|
|
668 |
|
|
} |