src/Service.php
public function createAsset(EmbeddedAsset $embeddedAsset, VolumeFolder $folder): Asset
	{
		$hasReplaceMb4 = method_exists(StringHelper::class, 'replaceMb4');

		$assetsService = Craft::$app->getAssets();
		$pluginSettings = EmbeddedAssets::$plugin->getSettings();

		$tempFilePath = Assets::tempFilePath();
		$fileContents = Json::encode($embeddedAsset, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

		FileHelper::writeToFile($tempFilePath, $fileContents);

		$assetTitle = $embeddedAsset->title ?: $embeddedAsset->url;

		// Ensure the title contains no emoji
		if ($hasReplaceMb4)
		{
			$assetTitle = StringHelper::replaceMb4($assetTitle, '');
		}
		else if (StringHelper::containsMb4($assetTitle))
		{
			$assetTitle = preg_replace_callback('/./u', function(array $match): string
			{
				return strlen($match[0]) >= 4 ? '' : $match[0];
			}, $assetTitle);
		}

		$fileName = Assets::prepareAssetName($assetTitle, false);
		$fileName = str_replace('.', '', $fileName);
		$fileName = $fileName ?: 'embedded-asset';
		$fileName = StringHelper::safeTruncate($fileName, $pluginSettings->maxFileNameLength) . '.json';
		$fileName = $assetsService->getNameReplacementInFolder($fileName, $folder->id);

		$asset = new Asset();
		$asset->title = StringHelper::safeTruncate($assetTitle, $pluginSettings->maxAssetNameLength);
		$asset->tempFilePath = $tempFilePath;
		$asset->filename = $fileName;
		$asset->newFolderId = $folder->id;
		$asset->volumeId = $folder->volumeId;
		$asset->avoidFilenameConflicts = true;
		$asset->setScenario(Asset::SCENARIO_CREATE);

		return $asset;
	}
public function createAsset(EmbeddedAsset $embeddedAsset, VolumeFolder $folder): Asset
	{
		$hasReplaceMb4 = method_exists(StringHelper::class, 'replaceMb4');

		$assetsService = Craft::$app->getAssets();
		$pluginSettings = EmbeddedAssets::$plugin->getSettings();

		$tempFilePath = Assets::tempFilePath();
		$fileContents = Json::encode($embeddedAsset, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

		FileHelper::writeToFile($tempFilePath, $fileContents);

		$assetTitle = $embeddedAsset->title ?: $embeddedAsset->url;

		// Ensure the title contains no emoji
		if ($hasReplaceMb4)
		{
			$assetTitle = StringHelper::replaceMb4($assetTitle, '');
		}
		else if (StringHelper::containsMb4($assetTitle))
		{
			$assetTitle = preg_replace_callback('/./u', function(array $match): string
			{
				return strlen($match[0]) >= 4 ? '' : $match[0];
			}, $assetTitle);
		}

		$fileName = Assets::prepareAssetName($assetTitle, false);
		$fileName = str_replace('.', '', $fileName);
		$fileName = $fileName ?: 'embedded-asset';
		$fileName = StringHelper::safeTruncate($fileName, $pluginSettings->maxFileNameLength) . '.json';
		$fileName = $assetsService->getNameReplacementInFolder($fileName, $folder->id);

		$asset = new Asset();
		$asset->title = StringHelper::safeTruncate($assetTitle, $pluginSettings->maxAssetNameLength);
		$asset->tempFilePath = $tempFilePath;
		$asset->filename = $fileName;
		$asset->newFolderId = $folder->id;
		$asset->volumeId = $folder->volumeId;
		$asset->avoidFilenameConflicts = true;
		$asset->setScenario(Asset::SCENARIO_CREATE);

		return $asset;
	}
public function createAsset ( EmbeddedAsset $ embeddedAsset , VolumeFolder $ folder ) : Asset { $ hasReplaceMb4 = method_exists ( StringHelper :: class , 'replaceMb4' ) ; $ assetsService = Craft :: $ app -> getAssets ( ) ; $ pluginSettings = EmbeddedAssets :: $ plugin -> getSettings ( ) ; $ tempFilePath = Assets :: tempFilePath ( ) ; $ fileContents = Json :: encode ( $ embeddedAsset , JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT ) ; FileHelper :: writeToFile ( $ tempFilePath , $ fileContents ) ; $ assetTitle = $ embeddedAsset -> title ? : $ embeddedAsset -> url ; // Ensure the title contains no emoji if ( $ hasReplaceMb4 ) { $ assetTitle = StringHelper :: replaceMb4 ( $ assetTitle , '' ) ; } else if ( StringHelper :: containsMb4 ( $ assetTitle ) ) { $ assetTitle = preg_replace_callback ( '/./u' , function ( array $ match ) : string { return strlen ( $ match [ 0 ] ) >= 4 ? '' : $ match [ 0 ] ; } , $ assetTitle ) ; } $ fileName = Assets :: prepareAssetName ( $ assetTitle , false ) ; $ fileName = str_replace ( '.' , '' , $ fileName ) ; $ fileName = $ fileName ? : 'embedded-asset' ; $ fileName = StringHelper :: safeTruncate ( $ fileName , $ pluginSettings -> maxFileNameLength ) . '.json' ; $ fileName = $ assetsService -> getNameReplacementInFolder ( $ fileName , $ folder -> id ) ; $ asset = new Asset ( ) ; $ asset -> title = StringHelper :: safeTruncate ( $ assetTitle , $ pluginSettings -> maxAssetNameLength ) ; $ asset -> tempFilePath = $ tempFilePath ; $ asset -> filename = $ fileName ; $ asset -> newFolderId = $ folder -> id ; $ asset -> volumeId = $ folder -> volumeId ; $ asset -> avoidFilenameConflicts = true ; $ asset -> setScenario ( Asset :: SCENARIO_CREATE ) ; return $ asset ; }
Creates an asset element ready to be saved from an embedded asset model.

@param EmbeddedAsset $embeddedAsset
@param VolumeFolder $folder The folder to save the asset to.
@return Asset
@throws \craft\errors\AssetLogicException
@throws \yii\base\ErrorException
@throws \yii\base\Exception
Creates an asset element ready to be saved from an embedded asset model .