You are here

protected function DataStream::getEntity in farmOS 2.x

Creates or loads an entity.

Parameters

\Drupal\migrate\Row $row: The row object.

array $old_destination_id_values: The old destination IDs.

Return value

\Drupal\Core\Entity\EntityInterface The entity we are importing into.

Overrides Entity::getEntity

File

modules/core/data_stream/src/Plugin/migrate/destination/DataStream.php, line 33

Class

DataStream
A destination plugin for data streams that can use a "providing_asset" ID.

Namespace

Drupal\data_stream\Plugin\migrate\destination

Code

protected function getEntity(Row $row, array $old_destination_id_values) {

  // The parent will create or update the entity.
  $entity = parent::getEntity($row, $old_destination_id_values);

  // Bail if the no entity was returned.
  if (empty($entity)) {
    return $entity;
  }

  // Save the entity now so that it has an ID.
  // This means that the EntityValidationRequired feature will not work
  // because we save it now, rather than waiting to see if it is validated.
  $entity
    ->save();

  // Check if a providing_asset ID was provided.
  if ($row
    ->hasDestinationProperty('providing_asset')) {
    $providing_asset = $row
      ->getDestinationProperty('providing_asset');

    /** @var \Drupal\asset\Entity\AssetInterface $asset */
    $asset = \Drupal::entityTypeManager()
      ->getStorage('asset')
      ->load($providing_asset);

    // Update the assets data_stream field if the asset was found
    // and the asset type has the field.
    if (!empty($asset) && $asset
      ->hasField('data_stream')) {
      $asset->data_stream[] = $entity
        ->id();
      $asset
        ->save();
    }
  }

  // Return the entity.
  return $entity;
}