Hallo, versuche gerade für ein Einkaufswelten-Element einen eigenen Handler für Import und Export zu schreiben. Im Grund unterscheidet sich das Element kaum vom Banner. Ich arbeite nach der Doku: Custom shopping world elements
Leider erhalte ich folgender Fehelrmeldung beim Export:
Fatal error: Uncaught Error: Call to a member function getRepository() on null in /home/vagrant/www/shopware/custom/plugins/prefixMyElement/Components/Emotion/Preset/ComponentHandler/MyElementComponentHandler.php on line 136
processElementData($element, $syncData);
}
/**
* {@inheritdoc}
*/
public function export(array $element, ParameterBag $syncData)
{
if (!isset($element['data'])) {
return $element;
}
return $this->prepareElementExport($element, $syncData);
}
/**
* @param array $element
* @param ParameterBag $syncData
*
* @return array
*/
private function processElementData(array $element, ParameterBag $syncData)
{
$data = $element['data'];
$assets = $syncData->get('assets', []); // Contains info about the assets like actual path
$importedAssets = $syncData->get('importedAssets', []); // Contains info about already imported assets
foreach ($data as &$elementData) {
if ($elementData['key'] === self::ELEMENT_DATA_KEY) {
if (!array_key_exists($elementData['value'], $assets)) {
break;
}
if (!array_key_exists($elementData['value'], $importedAssets)) { // Already imported?
$assetPath = $assets[$elementData['value']];
$media = $this->doAssetImport($assetPath); // Import new/unknown asset
$importedAssets[$elementData['value']] = $media->getId();
} else {
$media = $this->getMediaById($importedAssets[$elementData['value']]); // Gather info about already imported asset
}
$elementData['value'] = $media->getPath(); // Set the asset path as value on element data
break;
}
}
unset($elementData);
$syncData->set('importedAssets', $importedAssets); // Store info about handled asset imports
$element['data'] = $data;
return $element; // Return processed element
}
/**
* @param array $element
* @param ParameterBag $syncData
*
* @return array
*/
private function prepareElementExport(array $element, ParameterBag $syncData)
{
$assets = $syncData->get('assets', []);
$data = $element['data'];
foreach ($data as &$elementData) {
if ($elementData['key'] === self::ELEMENT_DATA_KEY) {
$assetPath = $elementData['value'];
$media = $this->getMediaByPath($assetPath);
if ($media) {
$assetHash = md5($media->getId()); // Create hash as unique identifier
$assets[$assetHash] = $this->mediaService->getUrl($assetPath);
$elementData['value'] = $assetHash; // Save hash as value
break;
}
}
}
unset($elementData);
$syncData->set('assets', $assets); // Store asset info globally
$element['data'] = $data;
return $element;
}
/**
* @param int $id
*
* @return null|Media
*/
private function getMediaById($id)
{
return $this->mediaResource->getRepository()->find($id);
}
/**
* @param string $path
*
* @return null|Media
*/
private function getMediaByPath($path)
{
return $this->mediaResource->getRepository()->findOneBy(['path' => $path]);
}
}