Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
internalCreateMediaByFileLink example
// Find the media object and convert it to an product image.
$media
=
$this
->
getManager
(
)
->
find
(
MediaModel::
class
,
(int)
$imageData
[
'mediaId'
]
)
;
if
(
!
$media
)
{
throw
new
CustomValidationException
(
sprintf
(
'Media by id %s not found',
(int)
$imageData
[
'mediaId'
]
)
)
;
}
$image
=
$this
->
getArticleResource
(
)
->
createNewArticleImage
(
$article
,
$media
)
;
}
}
elseif
(
isset
(
$imageData
[
'link'
]
)
)
{
// Check if an url passed and upload the passed image url and create a new product image.
$media
=
$this
->
getMediaResource
(
)
->
internalCreateMediaByFileLink
(
$imageData
[
'link'
]
)
;
$image
=
$this
->
getArticleResource
(
)
->
createNewArticleImage
(
$article
,
$media
)
;
}
else
{
throw
new
CustomValidationException
(
"One of the passed variant images doesn't contain a mediaId or link property!"
)
;
}
$variantImage
=
$this
->
createVariantImage
(
$image
,
$variant
)
;
private
function
prepareMediaData
(
array
$data
, CategoryModel
$categoryModel
)
: array
{
if
(
!
isset
(
$data
[
'media'
]
)
)
{
return
$data
;
}
$media
= null;
if
(
isset
(
$data
[
'media'
]
[
'link'
]
)
)
{
$mediaResource
=
$this
->
getContainer
(
)
->
get
(
Media::
class
)
;
$media
=
$mediaResource
->
internalCreateMediaByFileLink
(
$data
[
'media'
]
[
'link'
]
)
;
}
elseif
(
!
empty
(
$data
[
'media'
]
[
'mediaId'
]
)
)
{
$media
=
$this
->
getManager
(
)
->
find
(
MediaModel::
class
,
(int)
$data
[
'media'
]
[
'mediaId'
]
)
;
if
(
!
(
$media
instanceof MediaModel
)
)
{
throw
new
CustomValidationException
(
sprintf
(
'Media by mediaId %s not found',
$data
[
'media'
]
[
'mediaId'
]
)
)
;
}
}
$categoryModel
->
setMedia
(
$media
)
;
unset
(
$data
[
'media'
]
)
;
$this
->mediaResource->
setManager
(
$modelManager
)
;
}
/** * @param string $assetPath * @param int $albumId * * @return Media */
protected
function
doAssetImport
(
$assetPath
,
$albumId
= -3
)
{
$media
=
$this
->mediaResource->
internalCreateMediaByFileLink
(
$assetPath
,
$albumId
)
;
$this
->mediaResource->
getManager
(
)
->
flush
(
$media
)
;
return
$media
;
}
/** * @param int $id * * @return object|null */
$downloads
=
$this
->
checkDataReplacement
(
$product
->
getDownloads
(
)
,
$data
, 'downloads', true
)
;
foreach
(
$data
[
'downloads'
]
as
$downloadData
)
{
$download
=
$this
->
getOneToManySubElement
(
$downloads
,
$downloadData
,
Download::
class
D;
if
(
isset
(
$downloadData
[
'link'
]
)
)
{
$media
=
$this
->
getMediaResource
(
)
->
internalCreateMediaByFileLink
(
$downloadData
[
'link'
]
,
-6
)
;
if
(
isset
(
$downloadData
[
'name'
]
)
&& !
empty
(
$downloadData
[
'name'
]
)
)
{
$media
->
setDescription
(
$downloadData
[
'name'
]
)
;
}
try
{
// persist the model into the model manager
$this
->
getManager
(
)
->
persist
(
$media
)
;
}
catch
(
ORMException
$e
)
{
throw
new
CustomValidationException
(
sprintf
(
'Some error occurred while loading your image from link "%s"',
$downloadData
[
'link'
]
)
)
;
}
private
function
prepareMediaData
(
array
$data
, ManufacturerModel
$manufacturerModel
)
{
if
(
!
isset
(
$data
[
'image'
]
)
)
{
return
$data
;
}
$media
= null;
if
(
isset
(
$data
[
'image'
]
[
'link'
]
)
)
{
$resource
=
$this
->
getContainer
(
)
->
get
(
Media::
class
)
;
$media
=
$resource
->
internalCreateMediaByFileLink
(
$data
[
'image'
]
[
'link'
]
, Album::ALBUM_SUPPLIER
)
;
}
elseif
(
!
empty
(
$data
[
'image'
]
[
'mediaId'
]
)
)
{
$media
=
$this
->
getManager
(
)
->
find
(
MediaModel::
class
,
(int)
$data
[
'image'
]
[
'mediaId'
]
)
;
if
(
!
(
$media
instanceof MediaModel
)
)
{
throw
new
ApiException\
CustomValidationException
(
sprintf
(
'Media by mediaId %s not found',
$data
[
'image'
]
[
'mediaId'
]
)
)
;
}
}
$manufacturerModel
->
setImage
(
$media
?
$media
->
getPath
(
)
: ''
)
;
unset
(
$data
[
'image'
]
)
;