You are here

public function FileIdLookup::transform in Migrate File Entities to Media Entities 8

Throws

\Drupal\migrate\MigrateSkipProcessException

\Drupal\migrate\MigrateException

Overrides MigrationLookup::transform

File

src/Plugin/migrate/process/FileIdLookup.php, line 21

Class

FileIdLookup
Plugin annotation @MigrateProcessPlugin( id = "file_id_lookup" )

Namespace

Drupal\migrate_file_to_media\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  if ($value['target_id'] || $value['fid']) {
    $fid = !empty($value['target_id']) ? $value['target_id'] : $value['fid'];
    $query = \Drupal::database()
      ->select('migrate_file_to_media_mapping', 'map');
    $query
      ->fields('map');
    $query
      ->condition('fid', $fid, '=');
    $result = $query
      ->execute()
      ->fetchObject();
    if ($result) {

      // If the record has an existing media entity, return it.
      if (!empty($result->media_id)) {
        return $result->media_id;
      }
      return parent::transform($result->target_fid, $migrate_executable, $row, $destination_property);
    }
  }
  throw new MigrateSkipRowException();
}