<?php declare(strict_types=1);
namespace Sven\DasMenu;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class SvenDasMenu extends Plugin
{
public function install(InstallContext $installContext): void
{
parent::install($installContext);
/** @var EntityRepositoryInterface $repository */
$repository = $this->container->get('custom_field_set.repository');
$context = $installContext->getContext();
$criteriaCategorie = new Criteria();
$criteriaCategorie->addFilter(new EqualsFilter('name', 'sven_dasmenu_category'));
$criteriaCategorie->addAssociation('customFields');
$fieldsCategorie = $repository->search($criteriaCategorie, $installContext->getContext())->first();
if ($fieldsCategorie === null) {
$this->createCategoryCustomField($context);
}
}
public function uninstall(UninstallContext $context): void
{
parent::uninstall($context);
if ($context->keepUserData()) {
return;
}
$connection = $this->container->get(Connection::class);
$connection->executeUpdate('DELETE FROM `system_config` WHERE configuration_key LIKE "%SvenDasMenu.config%"');
$connection->executeUpdate('DELETE FROM `custom_field` WHERE name = "sven_dasmenu_category_image"');
$connection->executeUpdate('DELETE FROM `custom_field_set` WHERE name = "sven_dasmenu_category"');
}
private function createCategoryCustomField(Context $context)
{
/** @var EntityRepositoryInterface $repository */
$repository = $this->container->get('custom_field_set.repository');
$repository->upsert( [
[
'name' => 'sven_dasmenu_category',
'config' => [
'label' => [
'de-DE' => 'DasMenu Bild',
'en-GB' => 'DasMenu Image'
],
],
'customFields' => [
[
'name' => 'sven_dasmenu_category_image',
'type' => CustomFieldTypes::MEDIA,
'label' => [
'de-DE' => 'DasMenu Bild',
'en-GB' => 'DasMenu Image'
],
'config' => [
'componentName' => 'sw-media-field',
'customFieldType' => 'media',
'customFieldPosition' => 1
]
],
],
'relations' => [
[
'entityName' => 'category',
],
],
],
], $context);
}
}