Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
setWidth example
$data
=
[
]
;
$data
[
'empty'
]
[
]
=
(
new
SvgIconBuilder
(
)
)
;
$data
[
'empty'
]
[
]
=
[
]
;
$data
[
'empty'
]
[
]
= <<<'EOD' <svg width="125" height="150" class="layout-icon"></svg> EOD;
$data
[
'two_column'
]
[
]
=
(
new
SvgIconBuilder
(
)
)
->
setId
(
'two_column'
)
->
setLabel
(
'Two Column'
)
->
setWidth
(
250
)
->
setHeight
(
300
)
->
setStrokeWidth
(
2
)
;
$data
[
'two_column'
]
[
]
=
[
[
'left', 'right'
]
]
;
$data
[
'two_column'
]
[
]
= <<<'EOD' <svg width="250" height="300" class="layout-icon layout-icon--two-column"><title>Two Column</title> <g><title>left</title> <rect x="1" y="1" width="121" height="298" stroke-width="2" class="layout-icon__region layout-icon__region--left" /> </g> <g><title>right</title> <rect x="128" y="1" width="121" height="298" stroke-width="2" class="layout-icon__region layout-icon__region--right" /> </g> </svg>
$media
->
setFile
(
$this
->mediaService->
getUrl
(
$data
[
'__media_path'
]
)
)
;
}
/* * Live Migration to add width/height to images */
if
(
$this
->
isUpdateRequired
(
$media
,
$data
)
)
{
$data
=
$this
->
updateMedia
(
$data
)
;
}
if
(
isset
(
$data
[
'__media_width'
]
)
)
{
$media
->
setWidth
(
(int)
$data
[
'__media_width'
]
)
;
}
if
(
isset
(
$data
[
'__media_height'
]
)
)
{
$media
->
setHeight
(
(int)
$data
[
'__media_height'
]
)
;
}
if
(
$this
->
shouldAddThumbnails
(
$media
->
getType
(
)
,
$data
)
)
{
$media
->
setThumbnails
(
$this
->
getMediaThumbnails
(
$data
)
)
;
}
$product
->
setUpdatedAt
(
new
DateTime
(
$data
[
'__product_changetime'
]
)
)
;
}
$product
->
setAdditional
(
$data
[
'__variant_additionaltext'
]
)
;
$product
->
setEan
(
$data
[
'__variant_ean'
]
)
;
$product
->
setHeight
(
(float)
$data
[
'__variant_height'
]
)
;
$product
->
setLength
(
(float)
$data
[
'__variant_length'
]
)
;
$product
->
setMinStock
(
(int)
$data
[
'__variant_stockmin'
]
)
;
$product
->
setWeight
(
(float)
$data
[
'__variant_weight'
]
)
;
$product
->
setWidth
(
(float)
$data
[
'__variant_width'
]
)
;
$customerGroups
=
explode
(
'|',
$data
[
'__product_blocked_customer_groups'
]
?? ''
)
;
$customerGroups
=
array_filter
(
$customerGroups
)
;
$product
->
setBlockedCustomerGroupIds
(
$customerGroups
)
;
$product
->
setHasAvailableVariant
(
$data
[
'__product_has_available_variants'
]
> 0
)
;
$product
->
setFallbackPriceCount
(
$data
[
'__product_fallback_price_count'
]
)
;
if
(
\
array_key_exists
(
'__product_custom_price_count',
$data
)
)
{
$product
->
setCustomerPriceCount
(
$data
[
'__product_custom_price_count'
]
)
;
}
else
{
$product
->
setCustomerPriceCount
(
$data
[
'__product_fallback_price_count'
]
)
;
}
if
(
$media
->
getId
(
)
)
{
$eventArgs
->
getEntityManager
(
)
->
getConnection
(
)
->
executeUpdate
(
'UPDATE s_media SET width = :width, height = :height WHERE id = :id',
[
':width' =>
$width
,
':height' =>
$height
,
':id' =>
$media
->
getId
(
)
,
]
)
;
}
$media
->
setWidth
(
$width
)
;
$media
->
setHeight
(
$height
)
;
}
}
}
/** * Test file for supported types */
private
function
isFormatSupported
(
Media
$media
)
: bool
{
return
$media
->
getType
(
)
=== Media::TYPE_IMAGE
'#theme' => 'image',
'#uri' =>
$icon_path
,
'#width' =>
$width
,
'#height' =>
$height
,
'#alt' =>
$this
->
getLabel
(
)
,
]
;
}
elseif
(
$icon_map
=
$this
->
getIconMap
(
)
)
{
$icon_builder
=
$this
->
getIconBuilder
(
)
->
setId
(
$this
->
id
(
)
)
->
setLabel
(
$this
->
getLabel
(
)
)
->
setWidth
(
$width
)
->
setHeight
(
$height
)
;
if
(
$padding
)
{
$icon_builder
->
setPadding
(
$padding
)
;
}
if
(
$stroke_width
)
{
$icon_builder
->
setStrokeWidth
(
$stroke_width
)
;
}
$icon
=
$icon_builder
->
build
(
$icon_map
)
;
}
return
$icon
;
}
$product
->
setEan
(
$listProduct
->
getEan
(
)
)
;
$product
->
setHeight
(
$listProduct
->
getHeight
(
)
)
;
$product
->
setKeywords
(
$listProduct
->
getKeywords
(
)
)
;
$product
->
setLength
(
$listProduct
->
getLength
(
)
)
;
$product
->
setLongDescription
(
$listProduct
->
getLongDescription
(
)
)
;
$product
->
setMinStock
(
$listProduct
->
getMinStock
(
)
)
;
$product
->
setReleaseDate
(
$listProduct
->
getReleaseDate
(
)
)
;
$product
->
setShippingTime
(
$listProduct
->
getShippingTime
(
)
)
;
$product
->
setShortDescription
(
$listProduct
->
getShortDescription
(
)
)
;
$product
->
setStock
(
$listProduct
->
getStock
(
)
)
;
$product
->
setWeight
(
$listProduct
->
getWeight
(
)
)
;
$product
->
setWidth
(
$listProduct
->
getWidth
(
)
)
;
$product
->
setPriceGroup
(
$listProduct
->
getPriceGroup
(
)
)
;
$product
->
setCreatedAt
(
$listProduct
->
getCreatedAt
(
)
)
;
$product
->
setUpdatedAt
(
$listProduct
->
getUpdatedAt
(
)
)
;
$product
->
setPriceRules
(
$listProduct
->
getPriceRules
(
)
)
;
$product
->
setCheapestPriceRule
(
$listProduct
->
getCheapestPriceRule
(
)
)
;
$product
->
setManufacturerNumber
(
$listProduct
->
getManufacturerNumber
(
)
)
;
$product
->
setMetaTitle
(
$listProduct
->
getMetaTitle
(
)
)
;
$product
->
setTemplate
(
$listProduct
->
getTemplate
(
)
)
;
$product
->
setHasConfigurator
(
$listProduct
->
hasConfigurator
(
)
)
;
$product
->
setSales
(
$listProduct
->
getSales
(
)
)
;
$product
->
setHasEsd
(
$listProduct
->
hasEsd
(
)
)
;
public
function
testItAddsThumbnailUrl
(
)
: void
{
$subscriber
=
$this
->
getContainer
(
)
->
get
(
MediaLoadedSubscriber::
class
)
;
$context
= Context::
createDefaultContext
(
)
;
$mediaId
= '34556f108ab14969a0dcf9d9522fd7df';
$mimeType
= 'image/png';
$thumbnailEntity
=
new
MediaThumbnailEntity
(
)
;
$thumbnailEntity
->
setId
(
$mediaId
)
;
$thumbnailEntity
->
setHeight
(
100
)
;
$thumbnailEntity
->
setWidth
(
100
)
;
$mediaEntity
=
new
MediaEntity
(
)
;
$mediaEntity
->
setId
(
$mediaId
)
;
$mediaEntity
->
setMimeType
(
$mimeType
)
;
$mediaEntity
->
setFileExtension
(
'png'
)
;
$mediaEntity
->
setFileName
(
$mediaId
. '-134578345'
)
;
$mediaEntity
->
setThumbnails
(
new
MediaThumbnailCollection
(
[
$thumbnailEntity
]
)
)
;
$mediaLoadedEvent
=
new
EntityLoadedEvent
(
$this
->
getContainer
(
)
->
get
(
MediaDefinition::
class
)
,
[
$mediaEntity
]
,
$context
)
;
$subscriber
->
addUrls
(
$mediaLoadedEvent
)
;
use
PHPUnit\Framework\TestCase;
use
Shopware\Core\Content\Media\Aggregate\MediaThumbnail\MediaThumbnailEntity;
/** * @internal */
class
MediaThumbnailStructTest
extends
TestCase
{
public
function
testGetIdentifier
(
)
: void
{
$thumbnail
=
new
MediaThumbnailEntity
(
)
;
$thumbnail
->
setWidth
(
120
)
;
$thumbnail
->
setHeight
(
100
)
;
static
::
assertEquals
(
'120x100',
$thumbnail
->
getIdentifier
(
)
)
;
}
}
}
$media
->
setExtension
(
$this
->
getExtension
(
$file
)
)
;
$media
->
setFileSize
(
filesize
(
$filePath
)
)
;
$media
->
setCreated
(
new
DateTime
(
)
)
;
if
(
$media
->
getType
(
)
=== Media::TYPE_IMAGE
)
{
$imageSize
=
getimagesize
(
$filePath
)
;
if
(
$imageSize
)
{
$media
->
setWidth
(
$imageSize
[
0
]
)
;
$media
->
setHeight
(
$imageSize
[
1
]
)
;
}
$media
->
removeThumbnails
(
)
;
if
(
$newFileName
)
{
$media
->
setPath
(
$newFileName
)
;
}
$this
->thumbnailManager->
createMediaThumbnail
(
$media
,
$media
->
getDefaultThumbnails
(
)
, true
)
;
$media
->
createAlbumThumbnails
(
$media
->
getAlbum
(
)
)
;
}