<?php
declare(strict_types=1);
namespace fourtwosix\AttributeBadges;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Content\Property\PropertyGroupDefinition;
use Shopware\Core\Content\Rule\RuleDefinition;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
class fourtwosixAttributeBadges extends Plugin
{
public function install(InstallContext $installContext): void
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRelationRepository = $this->container->get('custom_field_set_relation.repository');
$id = Uuid::randomHex();
$customFieldSetRepository->create([
[
'id' => $id,
'name' => 'attribute_badges_set',
'config' => [
'label' => [
'en-GB' => 'Attribute Badges Set',
'de-DE' => 'Attribut Badges Set',
]
],
'customFields' => [
[
'name' => 'attribute_badges',
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'en-GB' => 'Attribute Badges',
'de-DE' => 'Attribut Badges',
],
'customFieldPosition' => 1,
'type' => 'checkbox',
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
]
]
]
]
], $installContext->getContext());
$customFieldSetRelationRepository->create([
[
'customFieldSetId' => $id,
'entityName' => PropertyGroupDefinition::ENTITY_NAME
]
], $installContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
if (!$uninstallContext->keepUserData()) {
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', 'attribute_badges_set'));
$custumFiledSetId = $customFieldSetRepository->search($criteria, $uninstallContext->getContext())->first()->getId();
$customFieldSetRepository->delete([
[
'id' => $custumFiledSetId
]
], $uninstallContext->getContext());
}
}
}