You are here

public function FieldCollectionItem::getHostRevisionId in Field collection 8.3

Returns the revision_id of the host entity for this field collection item.

To provide correct record id, we need to use revision ids instead of id.

Return value

int

Overrides FieldCollectionItemInterface::getHostRevisionId

1 call to FieldCollectionItem::getHostRevisionId()
FieldCollectionItem::getHost in src/Entity/FieldCollectionItem.php
Returns the host entity of this field collection item.

File

src/Entity/FieldCollectionItem.php, line 306

Class

FieldCollectionItem
Defines the field collection item entity class.

Namespace

Drupal\field_collection\Entity

Code

public function getHostRevisionId() {
  $host_type = $this->host_type->value;
  $entity_info = $this
    ->entityTypeManager()
    ->getDefinition($host_type, TRUE);
  if (!isset($this->host_revision_id) && $entity_info
    ->isRevisionable()) {

    /** @var SqlContentEntityStorage $storage */
    $storage = $this
      ->entityTypeManager()
      ->getStorage($host_type);

    // generate revision table name of the current field
    $field_storage = FieldStorageConfig::loadByName($host_type, $this
      ->bundle());
    $table = $storage
      ->getTableMapping()
      ->getDedicatedRevisionTableName($field_storage);

    // fetch entity + revision id of the related host
    $query = \Drupal::database()
      ->select($table, 'base');
    $query
      ->addExpression('base.entity_id', 'entity_id');
    $query
      ->addExpression('base.revision_id', 'revision_id');
    $query
      ->condition('base.' . $this
      ->bundle() . '_target_id', $this
      ->id());
    $query
      ->condition('base.' . $this
      ->bundle() . '_revision_id', $this
      ->getRevisionId());
    $query
      ->range(0, 1);
    $result = $query
      ->execute()
      ->fetch();
    $this->host_revision_id = $result->revision_id;
    if ($this->host_revision_id) {
      $this->host_id = $result->entity_id;
    }
  }
  return $this->host_revision_id;
}