Projet

Général

Profil

Révision a1cafe7e

Ajouté par Assos Assos il y a plus de 4 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/field_collection/field_collection.entity.inc
6 6
class FieldCollectionItemEntity extends Entity {
7 7

  
8 8
  /**
9
   * Field collection field info.
9
   * Field Collection field info.
10 10
   *
11 11
   * @var array
12 12
   */
......
55 55
  public $item_id;
56 56

  
57 57
  /**
58
   * Field collection revision ID.
58
   * Field Collection revision ID.
59 59
   *
60 60
   * @var integer
61 61
   */
......
145 145
      if ($new_label = module_invoke_all('field_collection_item_label', $this, $host, $field, $label)) {
146 146
        return array_pop($new_label);
147 147
      }
148
      elseif ($field['cardinality'] == 1) {
148

  
149
      if ($field['cardinality'] == 1) {
149 150
        return $label;
150 151
      }
151
      elseif ($this->item_id) {
152

  
153
      if ($this->item_id) {
152 154
        return t('!instance_label @count', array('!instance_label' => $label, '@count' => $this->delta() + 1));
153 155
      }
154
      else {
155
        return t('New !instance_label', array('!instance_label' => $label));
156
      }
156

  
157
      return t('New !instance_label', array('!instance_label' => $label));
157 158
    }
158 159
    return t('Unconnected field collection item');
159 160
  }
......
220 221
   *   The entity type of the entity the field collection is attached to.
221 222
   */
222 223
  public function updateHostEntity($entity, $host_entity_type = NULL) {
223
    $this->fetchHostDetails();
224
    $this->fetchHostDetails($entity);
224 225
    // If it isn't possible to retrieve hostEntityType due to the fact that it's
225 226
    // not saved in the DB yet then fill in info about the hostEntity manually.
226 227
    // This happens when creating a new revision of a field collection entity
227 228
    // and it needs to relate to the new revision of the host entity.
228
    if (!$this->hostEntityType) {
229
    if (!$this->hostEntityType || isset($entity->tid)) {
229 230
      $this->hostEntityType = $host_entity_type;
230 231
      $this->hostEntity = $entity;
231 232
      list($this->hostEntityId, $this->hostEntityRevisionId) = entity_extract_ids($this->hostEntityType, $this->hostEntity);
232 233
    }
233 234
    list($recieved_id) = entity_extract_ids($this->hostEntityType, $entity);
234 235

  
235
    if ($this->isInUse() && !empty($this->hostEntityId)) {
236
      $current_id = $this->hostEntityId;
236
    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
      }
237 245
    }
238 246
    else {
239 247
      $current_host = entity_revision_load($this->hostEntityType, $this->hostEntityRevisionId);
240
      list($current_id) = entity_extract_ids($this->hostEntityType, $current_host);
248
      list($current_id) = $current_host ? entity_extract_ids($this->hostEntityType, $current_host) : array($recieved_id);
241 249
    }
242 250

  
243 251
    if ($current_id == $recieved_id) {
......
300 308
    }
301 309
  }
302 310

  
303
  protected function fetchHostDetails() {
311
  /**
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) {
304 318
    if (!isset($this->hostEntityId) || (!$this->hostEntityId && $this->hostEntityRevisionId)) {
305 319
      if ($this->item_id) {
306 320
        // For saved field collections, query the field data to determine the
307 321
        // right host entity.
308 322
        $query = new EntityFieldQuery();
309
        $query->fieldCondition($this->fieldInfo(), 'revision_id', $this->revision_id);
323
        $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
          if (isset($entity_info['entity keys']['language'])) {
334
            $query->propertyCondition('language', $hostEntity->language);
335
          }
336
        }
337
        $query->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
310 338
        if (!$this->isInUse()) {
311 339
          $query->age(FIELD_LOAD_REVISION);
312 340
        }
......
353 381
              $this->langcode = $langcode;
354 382
              return $delta;
355 383
            }
356
            elseif (isset($item['entity']) && $item['entity'] === $this) {
384

  
385
            if (isset($item['entity']) && $item['entity'] === $this) {
357 386
              $this->langcode = $langcode;
358 387
              return $delta;
359 388
            }
......
370 399
                $this->langcode = $langcode;
371 400
                return $delta;
372 401
              }
373
              elseif (isset($item['entity']) && $item['entity'] === $this) {
402

  
403
              if (isset($item['entity']) && $item['entity'] === $this) {
374 404
                $this->langcode = $langcode;
375 405
                return $delta;
376 406
              }
......
385 415
   * Determines the language code under which the item is stored.
386 416
   */
387 417
  public function langcode() {
388
    if ($this->delta() === NULL || empty($this->langcode)) {
418
    if (empty($this->langcode) || $this->delta() === NULL) {
389 419
      $this->langcode = field_collection_entity_language('field_collection_item', $this);
390 420
    }
391 421

  
......
399 429
  /**
400 430
   * Determines whether this field collection item revision is in use.
401 431
   *
402
   * Field collection items may be contained in from non-default host entity
432
   * Field Collection items may be contained in from non-default host entity
403 433
   * revisions. If the field collection item does not appear in the default
404 434
   * host entity revision, the item is actually not used by default and so
405 435
   * marked as 'archived'.
......
432 462
  public function save($skip_host_save = FALSE) {
433 463
    // Make sure we have a host entity during creation.
434 464
    if (!empty($this->is_new) && !(isset($this->hostEntityId) || isset($this->hostEntity) || isset($this->hostEntityRevisionId))) {
435
      throw new Exception("Unable to create a field collection item without a given host entity.");
465
      throw new Exception('Unable to create a field collection item without a given host entity.');
436 466
    }
437 467

  
438 468
    // Copy the values of translatable fields for a new field collection item.
439
    if (field_collection_item_is_translatable() && !empty($this->is_new) && $this->langcode() == LANGUAGE_NONE) {
469
    if (!empty($this->is_new) && field_collection_item_is_translatable() && $this->langcode() == LANGUAGE_NONE) {
440 470
      $this->copyTranslations();
441 471
    }
442 472

  
......
446 476
    if ($skip_host_save) {
447 477
      return entity_get_controller($this->entityType)->save($this);
448 478
    }
449
    else {
450
      $host_entity = $this->hostEntity();
451
      if (!$host_entity) {
452
        throw new Exception("Unable to save a field collection item without a valid reference to a host entity.");
453
      }
454
      // If this is creating a new revision, also do so for the host entity.
455
      if (!empty($this->revision) || !empty($this->is_new_revision)) {
456
        $host_entity->revision = TRUE;
457
        if (!empty($this->default_revision)) {
458
          entity_revision_set_default($this->hostEntityType, $host_entity);
459
        }
460
      }
461
      // Set the host entity reference, so the item will be saved with the host.
462
      // @see field_collection_field_presave()
463
      $delta = $this->delta();
464
      if (isset($delta)) {
465
        $host_entity->{$this->field_name}[$this->langcode()][$delta] = array('entity' => $this);
466
      }
467
      else {
468
        $host_entity->{$this->field_name}[$this->langcode()][] = array('entity' => $this);
469
      }
470 479

  
471
      return entity_save($this->hostEntityType, $host_entity);
480
    $host_entity = $this->hostEntity();
481
    if (!$host_entity) {
482
      throw new Exception('Unable to save a field collection item without a valid reference to a host entity.');
483
    }
484
    // If this is creating a new revision, also do so for the host entity.
485
    if (!empty($this->revision) || !empty($this->is_new_revision)) {
486
      $host_entity->revision = TRUE;
487
      if (!empty($this->default_revision)) {
488
        entity_revision_set_default($this->hostEntityType, $host_entity);
489
      }
490
    }
491
    // Set the host entity reference, so the item will be saved with the host.
492
    // @see field_collection_field_presave()
493
    $delta = $this->delta();
494
    if (isset($delta)) {
495
      $host_entity->{$this->field_name}[$this->langcode()][$delta] = array('entity' => $this);
472 496
    }
497
    else {
498
      $host_entity->{$this->field_name}[$this->langcode()][] = array('entity' => $this);
499
    }
500

  
501
    return entity_save($this->hostEntityType, $host_entity);
473 502
  }
474 503

  
475 504
  /**
......
510 539
    foreach ($fields_instances as $translatable_field) {
511 540
      if ($fields[$translatable_field]['translatable'] == 1) {
512 541
        foreach ($target_languages as $langcode) {
513
          if (isset($this->{$translatable_field}[$source_language])) {
514
            //Source (translatable_field) is set, therefore continue processing.
515
            if(!isset($this->{$translatable_field}[$langcode])) {
516
              //Destination (translatable_field) is not set, therefore safe to copy the translation.
517
              $this->{$translatable_field}[$langcode] = $this->{$translatable_field}[$source_language];
518
            }
542
          // Source (translatable_field) is set, therefore continue
543
          // processing.
544
          if (isset($this->{$translatable_field}[$source_language])
545
            && !isset($this->{$translatable_field}[$langcode])) {
546
            // Destination (translatable_field) is not set, therefore safe to
547
            // copy the translation.
548
            $this->{$translatable_field}[$langcode] = $this->{$translatable_field}[$source_language];
519 549
          }
520 550
        }
521 551
        if ($source_language == LANGUAGE_NONE && count($this->{$translatable_field}) > 1) {
......
580 610
        entity_get_controller('field_collection_item')->resetCache(array($this->item_id));
581 611
        entity_revision_delete('field_collection_item', $this->revision_id);
582 612
      }
583
      if (!$row && !isset($this->hostEntity()->{$this->field_name}[$this->langcode()][$this->delta()])) {
613
      else {
584 614
        // Delete if there is no existing revision or translation to be saved.
585 615
        $this->delete($skip_host_update);
586 616
      }
......
630 660
    return drupal_map_assoc(array_keys($vars));
631 661
  }
632 662

  
633
  /**
634
   * Magic method to invoke setUp() on unserialization.
635
   *
636
   * @todo: Remove this once it appears in a released entity API module version.
637
   */
638
  public function __wakeup() {
639
    $this->setUp();
640
  }
641 663
}

Formats disponibles : Unified diff