You are here

public function FieldCollection::getFieldCollectionItem in Field collection 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldType/FieldCollection.php \Drupal\field_collection\Plugin\Field\FieldType\FieldCollection::getFieldCollectionItem()

@TODO

3 calls to FieldCollection::getFieldCollectionItem()
FieldCollection::delete in src/Plugin/Field/FieldType/FieldCollection.php
Defines custom delete behavior for field values.
FieldCollection::isEmpty in src/Plugin/Field/FieldType/FieldCollection.php
Determines whether the data structure is empty.
FieldCollection::preSave in src/Plugin/Field/FieldType/FieldCollection.php
Care about removed field collection items.

File

src/Plugin/Field/FieldType/FieldCollection.php, line 137

Class

FieldCollection
Plugin implementation of the 'field_collection' field type.

Namespace

Drupal\field_collection\Plugin\Field\FieldType

Code

public function getFieldCollectionItem($create = FALSE, $use_reference = TRUE) {
  if ($use_reference && isset($this->entity)) {
    return $this->entity;
  }
  elseif (isset($this->target_id)) {

    // By default always load the default revision, so caches get used.
    $field_collection_item = FieldCollectionItem::load($this->target_id);
    if ($field_collection_item !== NULL && $field_collection_item
      ->getRevisionId() != $this->revision_id) {

      // A non-default revision is a referenced, so load this one.
      $field_collection_item = \Drupal::entityTypeManager()
        ->getStorage('field_collection_item')
        ->loadRevision($this->revision_id);
    }
    return $field_collection_item;
  }
  elseif ($create) {
    $field_collection_item = FieldCollectionItem::create([
      'field_name' => $this
        ->getFieldDefinition()
        ->getName(),
    ]);

    // TODO: Uncomment or delete

    /*
    $field_collection_item->setHostEntity($this->getEntity(), FALSE);
    */
    return $field_collection_item;
  }
  return FALSE;
}